Merge pull request #586 from hwhw/l10n

Added a pure Lua gettext implementation.
Tested and working.

The translations are rather lacking, though!
pull/591/head v2014.05.24-nightly
Markismus 10 years ago
commit 423177cb97

@ -12,7 +12,7 @@ VERSION=$(shell git describe HEAD)
INSTALL_DIR=koreader-$(MACHINE)
# files to link from main directory
INSTALL_FILES=reader.lua frontend resources defaults.lua \
INSTALL_FILES=reader.lua frontend resources defaults.lua l10n \
git-rev README.md COPYING
# for gettext
@ -20,10 +20,9 @@ DOMAIN=koreader
TEMPLATE_DIR=l10n/templates
KOREADER_MISC_TOOL=../misc
XGETTEXT_BIN=$(KOREADER_MISC_TOOL)/gettext/lua_xgettext.py
MO_DIR=$(INSTALL_DIR)/koreader/i18n
all: $(KOR_BASE)/$(OUTPUT_DIR)/luajit po mo
all: $(KOR_BASE)/$(OUTPUT_DIR)/luajit po
$(MAKE) -C $(KOR_BASE)
echo $(VERSION) > git-rev
mkdir -p $(INSTALL_DIR)/koreader
@ -126,12 +125,3 @@ pot:
po:
$(MAKE) -i -C l10n bootstrap update
mo:
for po in `find l10n -iname '*.po'`; do \
resource=`basename $$po .po` ; \
lingua=`dirname $$po | xargs basename` ; \
mkdir -p $(MO_DIR)/$$lingua/LC_MESSAGES/ ; \
msgfmt -o $(MO_DIR)/$$lingua/LC_MESSAGES/$$resource.mo $$po ; \
done

@ -1,16 +1,96 @@
lua_gettext.init("./i18n", "koreader")
local DEBUG = require("dbg")
local GetText = {}
local GetText_mt = {}
local GetText = {
translation = {},
current_lang = "C",
dirname = "l10n",
textdomain = "koreader"
}
local GetText_mt = {
__index = {}
}
function GetText_mt.__call(gettext, string)
return lua_gettext.translate(string)
return gettext.translation[string] or string
end
local function c_escape(what)
if what == "\n" then return ""
elseif what == "a" then return "\a"
elseif what == "b" then return "\b"
elseif what == "f" then return "\f"
elseif what == "n" then return "\n"
elseif what == "r" then return "\r"
elseif what == "t" then return "\t"
elseif what == "v" then return "\v"
elseif what == "0" then return "\0" -- shouldn't happen, though
else
return what
end
end
function GetText.changeLang(new_lang)
lua_gettext.change_lang(new_lang)
-- for PO file syntax, see
-- https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html
-- we only implement a sane subset for now
function GetText_mt.__index.changeLang(new_lang)
GetText.translation = {}
GetText.current_lang = "C"
-- the "C" locale disables localization alltogether
if new_lang == "C" then return end
local file = GetText.dirname .. "/" .. new_lang .. "/" .. GetText.textdomain .. ".po"
local po = io.open(file, "r")
if not po then
DEBUG("cannot open translation file " .. file)
return
end
local data = {}
local what = nil
while true do
local line = po:read("*l")
if line == nil or line == "" then
if data.msgid and data.msgstr and data.msgstr ~= "" then
GetText.translation[data.msgid] = string.gsub(data.msgstr, "\\(.)", c_escape)
end
-- stop at EOF:
if line == nil then break end
data = {}
what = nil
else
-- comment
if not line:match("^#") then
-- new data item (msgid, msgstr, ...
local w, s = line:match("^%s*(%a+)%s+\"(.*)\"%s*$")
if w then
what = w
else
-- string continuation
s = line:match("^%s*\"(.*)\"%s*$")
end
if what and s then
data[what] = (data[what] or "") .. s
end
end
end
end
GetText.current_lang = new_lang
end
setmetatable(GetText, GetText_mt)
if os.getenv("LANGUAGE") then
GetText.changeLang(os.getenv("LANGUAGE"))
elseif os.getenv("LC_ALL") then
GetText.changeLang(os.getenv("LC_ALL"))
elseif os.getenv("LC_MESSAGES") then
GetText.changeLang(os.getenv("LC_MESSAGES"))
elseif os.getenv("LANG") then
GetText.changeLang(os.getenv("LANG"))
end
return GetText

Loading…
Cancel
Save