From cde5c46e449e49e1435ac649bf344234ccd61f80 Mon Sep 17 00:00:00 2001 From: LNJ Date: Tue, 11 Apr 2017 13:02:31 +0200 Subject: [PATCH] Add i18n support using intllib --- Makefile | 4 ++++ depends.txt | 1 + init.lua | 13 ++++++++----- intllib.lua | 45 ++++++++++++++++++++++++++++++++++++++++++++ locale/template.pot | 46 +++++++++++++++++++++++++++++++++++++++++++++ lua/api.lua | 12 ++++++++---- lua/helpers.lua | 9 ++++++--- lua/visual.lua | 8 ++++++-- tools/updatepo.sh | 22 ++++++++++++++++++++++ 9 files changed, 146 insertions(+), 14 deletions(-) create mode 100644 Makefile create mode 100644 intllib.lua create mode 100644 locale/template.pot create mode 100755 tools/updatepo.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e3555ec --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +PROJECT = drawers + +updatepo: + ./tools/updatepo.sh diff --git a/depends.txt b/depends.txt index 9d76291..eef798d 100755 --- a/depends.txt +++ b/depends.txt @@ -2,3 +2,4 @@ default? mcl_core? screwdriver? pipeworks? +intllib? diff --git a/init.lua b/init.lua index ac2ca27..6c30c42 100755 --- a/init.lua +++ b/init.lua @@ -24,6 +24,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] +-- Load support for intllib. +local MP = core.get_modpath(core.get_current_modname()) +local S, NS = dofile(MP.."/intllib.lua") + drawers = {} drawers.drawer_visuals = {} @@ -51,10 +55,9 @@ drawers.enable_2x2 = not core.setting_getbool("drawers_disable_2x2") -- Load files -- -local modpath = core.get_modpath("drawers") -dofile(modpath .. "/lua/helpers.lua") -dofile(modpath .. "/lua/visual.lua") -dofile(modpath .. "/lua/api.lua") +dofile(MP .. "/lua/helpers.lua") +dofile(MP .. "/lua/visual.lua") +dofile(MP .. "/lua/api.lua") -- @@ -62,7 +65,7 @@ dofile(modpath .. "/lua/api.lua") -- drawers.register_drawer("drawers:wood", { - description = "Wooden", + description = S("Wooden"), tiles1 = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png", "drawers_wood.png", "drawers_wood.png", "drawers_wood_front_1.png"}, tiles2 = {"drawers_wood.png", "drawers_wood.png", "drawers_wood.png", diff --git a/intllib.lua b/intllib.lua new file mode 100644 index 0000000..6669d72 --- /dev/null +++ b/intllib.lua @@ -0,0 +1,45 @@ + +-- Fallback functions for when `intllib` is not installed. +-- Code released under Unlicense . + +-- Get the latest version of this file at: +-- https://raw.githubusercontent.com/minetest-mods/intllib/master/lib/intllib.lua + +local function format(str, ...) + local args = { ... } + local function repl(escape, open, num, close) + if escape == "" then + local replacement = tostring(args[tonumber(num)]) + if open == "" then + replacement = replacement..close + end + return replacement + else + return "@"..open..num..close + end + end + return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl)) +end + +local gettext, ngettext +if minetest.get_modpath("intllib") then + if intllib.make_gettext_pair then + -- New method using gettext. + gettext, ngettext = intllib.make_gettext_pair() + else + -- Old method using text files. + gettext = intllib.Getter() + end +end + +-- Fill in missing functions. + +gettext = gettext or function(msgid, ...) + return format(msgid, ...) +end + +ngettext = ngettext or function(msgid, msgid_plural, n, ...) + return format(n==1 and msgid or msgid_plural, ...) +end + +return gettext, ngettext diff --git a/locale/template.pot b/locale/template.pot new file mode 100644 index 0000000..c52cb76 --- /dev/null +++ b/locale/template.pot @@ -0,0 +1,46 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-04-11 12:56+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: lua/helpers.lua +msgid "@1 (@2% full)" +msgstr "" + +#: lua/helpers.lua +msgid "@1 @2 (@3% full)" +msgstr "" + +#: lua/api.lua init.lua +msgid "Wooden" +msgstr "" + +#: lua/api.lua +msgid "@1 Drawer" +msgstr "" + +#: lua/api.lua +msgid "@1 Drawers (1x2)" +msgstr "" + +#: lua/api.lua +msgid "@1 Drawers (2x2)" +msgstr "" + +#: lua/visual.lua +msgid "Empty" +msgstr "" diff --git a/lua/api.lua b/lua/api.lua index f49035b..9d54f53 100755 --- a/lua/api.lua +++ b/lua/api.lua @@ -25,6 +25,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] +-- Load support for intllib. +local MP = core.get_modpath(core.get_current_modname()) +local S, NS = dofile(MP.."/intllib.lua") + drawers.node_box_simple = { {-0.5, -0.5, -0.4375, 0.5, 0.5, 0.5}, {-0.5, -0.5, -0.5, -0.4375, 0.5, -0.4375}, @@ -125,7 +129,7 @@ function drawers.drawer_insert_object(pos, node, stack, direction) end function drawers.register_drawer(name, def) - def.description = def.description or "Wooden" + def.description = def.description or S("Wooden") def.drawtype = "nodebox" def.node_box = {type = "fixed", fixed = drawers.node_box_simple} def.collision_box = {type = "regular"} @@ -160,7 +164,7 @@ function drawers.register_drawer(name, def) if drawers.enable_1x1 then -- normal drawer 1x1 = 1 local def1 = table.copy(def) - def1.description = def.description .. " Drawer" + def1.description = S("@1 Drawer", def.description) def1.tiles = def.tiles or def.tiles1 def1.tiles1 = nil def1.tiles2 = nil @@ -173,7 +177,7 @@ function drawers.register_drawer(name, def) if drawers.enable_1x2 then -- 1x2 = 2 local def2 = table.copy(def) - def2.description = def.description .. " Drawers (1x2)" + def2.description = S("@1 Drawers (1x2)", def.description) def2.tiles = def.tiles2 def2.tiles1 = nil def2.tiles2 = nil @@ -185,7 +189,7 @@ function drawers.register_drawer(name, def) if drawers.enable_2x2 then -- 2x2 = 4 local def4 = table.copy(def) - def4.description = def.description .. " Drawers (2x2)" + def4.description = S("@1 Drawers (2x2)", def.description) def4.tiles = def.tiles4 def4.tiles1 = nil def4.tiles2 = nil diff --git a/lua/helpers.lua b/lua/helpers.lua index 12997d1..fa1087a 100755 --- a/lua/helpers.lua +++ b/lua/helpers.lua @@ -25,6 +25,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] +-- Load support for intllib. +local MP = core.get_modpath(core.get_current_modname()) +local S, NS = dofile(MP.."/intllib.lua") + function drawers.gen_info_text(basename, count, factor, stack_max) local maxCount = stack_max * factor local percent = count / maxCount * 100 @@ -32,10 +36,9 @@ function drawers.gen_info_text(basename, count, factor, stack_max) percent = math.floor(percent + 0.5) if count == 0 then - return basename .. " (" .. tostring(percent) .. "% full)" + return S("@1 (@2% full)", basename, tostring(percent)) else - return tostring(count) .. " " .. basename .. " (" .. - tostring(percent) .. "% full)" + return S("@1 @2 (@3% full)", tostring(count), basename, tostring(percent)) end end diff --git a/lua/visual.lua b/lua/visual.lua index 178b15f..e0e99de 100755 --- a/lua/visual.lua +++ b/lua/visual.lua @@ -24,6 +24,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] +-- Load support for intllib. +local MP = core.get_modpath(core.get_current_modname()) +local S, NS = dofile(MP.."/intllib.lua") + core.register_entity("drawers:visual", { initial_properties = { hp_max = 1, @@ -179,7 +183,7 @@ core.register_entity("drawers:visual", { self.itemName = "" meta:set_string("name"..self.visualId, self.itemName) self.texture = "blank.png" - itemDescription = "Empty" + itemDescription = S("Empty") end local infotext = drawers.gen_info_text(itemDescription, @@ -243,7 +247,7 @@ core.register_entity("drawers:visual", { if core.registered_items[self.itemName] then itemDescription = core.registered_items[self.itemName].description else - itemDescription = "Empty" + itemDescription = S("Empty") end local infotext = drawers.gen_info_text(itemDescription, self.count, self.stackMaxFactor, self.itemStackMax) diff --git a/tools/updatepo.sh b/tools/updatepo.sh new file mode 100755 index 0000000..74332e0 --- /dev/null +++ b/tools/updatepo.sh @@ -0,0 +1,22 @@ +#! /bin/bash + +# To create a new translation: +# msginit --locale=ll_CC -o locale/ll_CC.po -i locale/template.pot + +cd "$(dirname "${BASH_SOURCE[0]}")/.."; + +# Extract translatable strings. +xgettext --from-code=UTF-8 \ + --keyword=S \ + --keyword=NS:1,2 \ + --keyword=N_ \ + --add-comments='Translators:' \ + --add-location=file \ + -o locale/template.pot \ + $(find . -name '*.lua') + +# Update translations. +find locale -name '*.po' | while read -r file; do + echo $file + msgmerge --update $file locale/template.pot; +done