From da26ea7cf8f15b5959fe27739f2687e0a738b693 Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Sun, 16 Jul 2017 12:37:31 -0300 Subject: [PATCH 1/7] Add priority language for load_catalogs method --- gettext.lua | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gettext.lua b/gettext.lua index 26a2d73..5f3c79f 100644 --- a/gettext.lua +++ b/gettext.lua @@ -209,9 +209,19 @@ local function load_catalog(filename) return data end -function M.load_catalogs(path) - local langs = intllib.get_detected_languages() - +function M.load_catalogs(path, first_lang) + + -- add selected first lang + if first_lang then + langs = {} + table.insert(langs, first_lang) + for _, lang in ipairs(intllib.get_detected_languages()) do + langs[#langs+1] = lang + end + else + langs = intllib.get_detected_languages() + end + local cats = { } for _, lang in ipairs(langs) do local cat = load_catalog(path.."/"..lang..".po") From a5cc2639f284e3393b7d5ffb8fd06806cbd22139 Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Sun, 16 Jul 2017 12:40:15 -0300 Subject: [PATCH 2/7] Add variant methods for gettext and ngettext for selected language --- init.lua | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 0778a73..a874cc4 100644 --- a/init.lua +++ b/init.lua @@ -178,8 +178,21 @@ function intllib.make_gettext_pair(modname) or getter(msgid)) return do_replacements(msgstr, ...) end - gettext_getters[modname] = { gettext_func, ngettext_func } - return gettext_func, ngettext_func + -- Get string of Selected language + local function sgettext_func(lang, msgid, ...) + local scatalogs = gettext.load_catalogs(localedir, lang) + local msgstr = (catgettext(scatalogs, msgid) + or getter(msgid)) + return do_replacements(msgstr, ...) + end + local function sngettext_func(lang, msgid, msgid_plural, n, ...) + local scatalogs = gettext.load_catalogs(localedir, lang) + local msgstr = (catngettext(scatalogs, msgid, msgid_plural, n) + or getter(msgid)) + return do_replacements(msgstr, ...) + end + gettext_getters[modname] = { gettext_func, ngettext_func, sgettext_func, sngettext_func } + return gettext_func, ngettext_func, sgettext_func, sngettext_func end From 8f92aed533e04c9f2f071f74a7fed1bd4541800a Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Sun, 16 Jul 2017 12:42:10 -0300 Subject: [PATCH 3/7] Update lib script for sgettext and sngettext --- lib/intllib.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/intllib.lua b/lib/intllib.lua index 6669d72..a047365 100644 --- a/lib/intllib.lua +++ b/lib/intllib.lua @@ -21,11 +21,11 @@ local function format(str, ...) return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl)) end -local gettext, ngettext +local gettext, ngettext, sgettext, sngettext if minetest.get_modpath("intllib") then if intllib.make_gettext_pair then -- New method using gettext. - gettext, ngettext = intllib.make_gettext_pair() + gettext, ngettext, sgettext, sngettext = intllib.make_gettext_pair() else -- Old method using text files. gettext = intllib.Getter() @@ -42,4 +42,12 @@ ngettext = ngettext or function(msgid, msgid_plural, n, ...) return format(n==1 and msgid or msgid_plural, ...) end -return gettext, ngettext +sgettext = sgettext or function(lang, msgid, ...) + return format(msgid, ...) +end + +sngettext = sngettext or function(lang, msgid, msgid_plural, n, ...) + return format(n==1 and msgid or msgid_plural, ...) +end + +return gettext, ngettext, sgettext, sngettext From ee8ea4b5ff0bcdbbbb7c4ecb431e4127938eaae0 Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Sun, 16 Jul 2017 12:57:21 -0300 Subject: [PATCH 4/7] Fix and Add support for SS and SNS xgettext tools --- tools/xgettext.bat | 3 +-- tools/xgettext.sh | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/xgettext.bat b/tools/xgettext.bat index 18403db..9ca8d46 100644 --- a/tools/xgettext.bat +++ b/tools/xgettext.bat @@ -17,8 +17,7 @@ set msgmerge=%gtprefix%msgmerge.exe md locale > nul 2>&1 echo Generating template... 1>&2 -echo %xgettext% --from-code=UTF-8 -kS -kNS:1,2 -k_ -o locale/template.pot %* -%xgettext% --from-code=UTF-8 -kS -kNS:1,2 -k_ -o locale/template.pot %* +echo %xgettext% --from-code=UTF-8 -kS -kSS:2 -kNS:1,2 -kSNS:2,3 -k_ -o locale/template.pot %* if %ERRORLEVEL% neq 0 goto done cd locale diff --git a/tools/xgettext.sh b/tools/xgettext.sh index 1504f42..6aebe58 100755 --- a/tools/xgettext.sh +++ b/tools/xgettext.sh @@ -11,7 +11,9 @@ mkdir -p locale; echo "Generating template..." >&2; xgettext --from-code=UTF-8 \ --keyword=S \ + --keyword=SS:2 \ --keyword=NS:1,2 \ + --keyword=SNS:2,3 \ --keyword=N_ \ --add-comments='Translators:' \ --add-location=file \ From 4e848b03a99b3a2c76e82ab895fd6a429d5e1a47 Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Sun, 16 Jul 2017 12:58:35 -0300 Subject: [PATCH 5/7] Update docs for use SS and SNS methods --- doc/developer.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/developer.md b/doc/developer.md index 551f188..15c0d53 100644 --- a/doc/developer.md +++ b/doc/developer.md @@ -9,7 +9,7 @@ boilerplate code in files needing localization: -- Load support for intllib. local MP = minetest.get_modpath(minetest.get_current_modname()) - local S, NS = dofile(MP.."/intllib.lua") + local S, NS, SS, SNS = dofile(MP.."/intllib.lua") You will also need to optionally depend on intllib, to do so add `intllib?` to an empty line in your `depends.txt`. Also note that if intllib is not @@ -36,6 +36,14 @@ string to be translated has singular and plural forms. For example: -- The second `count` is the actual replacement. print(NS("You have one item.", "You have @1 items.", count, count)) +The `SS` and `SNS` are equivalent to `S` and `SNS` respectively, +but the first argument is the selected language code. If not find it, +works the same as `S` or `NS` normally. + + -- The `"es"` is a language code + print(SS("es", "You are welcome.") + print(SNS("es", "You have one item.", "You have @1 items.", count, count)) + ## Generating and updating catalogs This is the basic workflow for working with [gettext][gettext] From 43ca3f2c90945bc99f71fa048db6030ad8ea5a55 Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Sun, 16 Jul 2017 12:59:05 -0300 Subject: [PATCH 6/7] Update intltest mod for use SS and SNS methods --- intltest/init.lua | 17 ++++++++++- intltest/locale/de.po | 55 ++++++++++++++++++++++++++++++++++++ intltest/locale/es.po | 16 +++++++++-- intltest/locale/pt.po | 55 ++++++++++++++++++++++++++++++++++++ intltest/locale/template.pot | 12 +++++++- 5 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 intltest/locale/de.po create mode 100644 intltest/locale/pt.po diff --git a/intltest/init.lua b/intltest/init.lua index e855621..0051174 100644 --- a/intltest/init.lua +++ b/intltest/init.lua @@ -1,7 +1,7 @@ -- Load support for intllib. local MP = minetest.get_modpath(minetest.get_current_modname()) -local S, NS = dofile(MP.."/intllib.lua") +local S, NS, SS, SNS = dofile(MP.."/intllib.lua") local use_count = 0 @@ -26,3 +26,18 @@ minetest.register_craftitem("intltest:test", { minetest.chat_send_player(user:get_player_name(), message) end, }) + +minetest.log("action", "Testing SS method") +for _,lang in ipairs({nil, "es", "pt", "de"}) do + minetest.log("action", "("..dump(lang)..")"..SS(lang, "Test: @1 @2", SS(lang, "Green"), SS(lang, "Car"))) +end + +minetest.log("action", "Testing SNS method") +for n=1, 2 do + minetest.log("action", "(nil)"..SNS(nil, "Hello, @1 world!", "Hello, @1 worlds!", n, n)) +end +for _,lang in ipairs({"es", "pt", "de"}) do + for n=1, 2 do + minetest.log("action", "("..lang..")"..SNS(lang, "Hello, @1 world!", "Hello, @1 worlds!", n, n)) + end +end diff --git a/intltest/locale/de.po b/intltest/locale/de.po new file mode 100644 index 0000000..231953d --- /dev/null +++ b/intltest/locale/de.po @@ -0,0 +1,55 @@ +# I18N Test Mod. +# Copyright (C) 2013-2017 Diego Martínez +# This file is distributed under the same license as the intllib mod. +# Diego Martínez , 2013-2017. +# Diego Martnez , 2017. +# +msgid "" +msgstr "" +"Project-Id-Version: I18N Test Mod 0.1.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-07-16 11:41-0300\n" +"PO-Revision-Date: 2017-07-16 11:45-0300\n" +"Last-Translator: Diego Martnez \n" +"Language-Team: Spanish\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Gtranslator 2.91.7\n" + +#: init.lua +msgid "Hello, world!" +msgstr "Hallo Welt!" + +#. Translators: @1 is color, @2 is object. +#: init.lua +msgid "Blue" +msgstr "Blau" + +#: init.lua +msgid "Car" +msgstr "Auto" + +#. Translators: @1 is color, @2 is object. +#: init.lua +msgid "Test: @1 @2" +msgstr "Test: @2 @1" + +#. Translators: @1 is use count. +#: init.lua +msgid "Item has been used @1 time." +msgid_plural "Item has been used @1 times." +msgstr[0] "" +msgstr[1] "" + +#: init.lua +msgid "Green" +msgstr "Grün" + +#: init.lua +msgid "Hello, @1 world!" +msgid_plural "Hello, @1 worlds!" +msgstr[0] "Hallo Welt!" +msgstr[1] "Hallo @1 Welten!" diff --git a/intltest/locale/es.po b/intltest/locale/es.po index cd51b8f..9552e1b 100644 --- a/intltest/locale/es.po +++ b/intltest/locale/es.po @@ -2,13 +2,14 @@ # Copyright (C) 2013-2017 Diego Martínez # This file is distributed under the same license as the intllib mod. # Diego Martínez , 2013-2017. +# Diego Martnez , 2017. # msgid "" msgstr "" "Project-Id-Version: I18N Test Mod 0.1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-02-25 20:40-0300\n" -"PO-Revision-Date: 2017-01-23 17:36-0300\n" +"POT-Creation-Date: 2017-07-16 11:41-0300\n" +"PO-Revision-Date: 2017-07-16 11:45-0300\n" "Last-Translator: Diego Martnez \n" "Language-Team: Spanish\n" "Language: es\n" @@ -16,6 +17,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Gtranslator 2.91.7\n" #: init.lua msgid "Hello, world!" @@ -41,3 +43,13 @@ msgid "Item has been used @1 time." msgid_plural "Item has been used @1 times." msgstr[0] "El objeto ha sido usado @1 vez." msgstr[1] "El objeto ha sido usado @1 veces." + +#: init.lua +msgid "Green" +msgstr "Verde" + +#: init.lua +msgid "Hello, @1 world!" +msgid_plural "Hello, @1 worlds!" +msgstr[0] "¡Hola, mundo!" +msgstr[1] "¡Hola, @1 mundos!" diff --git a/intltest/locale/pt.po b/intltest/locale/pt.po new file mode 100644 index 0000000..94b1440 --- /dev/null +++ b/intltest/locale/pt.po @@ -0,0 +1,55 @@ +# I18N Test Mod. +# Copyright (C) 2013-2017 Diego Martínez +# This file is distributed under the same license as the intllib mod. +# Diego Martínez , 2013-2017. +# Diego Martnez , 2017. +# +msgid "" +msgstr "" +"Project-Id-Version: I18N Test Mod 0.1.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-07-16 11:41-0300\n" +"PO-Revision-Date: 2017-07-16 11:45-0300\n" +"Last-Translator: Diego Martnez \n" +"Language-Team: Spanish\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Gtranslator 2.91.7\n" + +#: init.lua +msgid "Hello, world!" +msgstr "Ola, mundo!" + +#. Translators: @1 is color, @2 is object. +#: init.lua +msgid "Blue" +msgstr "Azul" + +#: init.lua +msgid "Car" +msgstr "Carro" + +#. Translators: @1 is color, @2 is object. +#: init.lua +msgid "Test: @1 @2" +msgstr "Teste: @2 @1" + +#. Translators: @1 is use count. +#: init.lua +msgid "Item has been used @1 time." +msgid_plural "Item has been used @1 times." +msgstr[0] "Esse objeto foi usado @1 vez." +msgstr[1] "Esse objeto foi usado @2 vezes." + +#: init.lua +msgid "Green" +msgstr "Verde" + +#: init.lua +msgid "Hello, @1 world!" +msgid_plural "Hello, @1 worlds!" +msgstr[0] "Ola, mundo!" +msgstr[1] "Ola, @1 mundos!" diff --git a/intltest/locale/template.pot b/intltest/locale/template.pot index 2090d61..a8f887b 100644 --- a/intltest/locale/template.pot +++ b/intltest/locale/template.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-02-25 20:40-0300\n" +"POT-Creation-Date: 2017-07-16 11:41-0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -42,3 +42,13 @@ msgid "Item has been used @1 time." msgid_plural "Item has been used @1 times." msgstr[0] "" msgstr[1] "" + +#: init.lua +msgid "Green" +msgstr "" + +#: init.lua +msgid "Hello, @1 world!" +msgid_plural "Hello, @1 worlds!" +msgstr[0] "" +msgstr[1] "" From bca5a72f783c0f623bf6923569b34ea13cbcd21d Mon Sep 17 00:00:00 2001 From: BrunoMine Date: Wed, 2 Aug 2017 17:49:37 -0300 Subject: [PATCH 7/7] Fix "SNS" world to "NS" --- doc/developer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/developer.md b/doc/developer.md index 15c0d53..92c4a65 100644 --- a/doc/developer.md +++ b/doc/developer.md @@ -36,7 +36,7 @@ string to be translated has singular and plural forms. For example: -- The second `count` is the actual replacement. print(NS("You have one item.", "You have @1 items.", count, count)) -The `SS` and `SNS` are equivalent to `S` and `SNS` respectively, +The `SS` and `SNS` are equivalent to `S` and `NS` respectively, but the first argument is the selected language code. If not find it, works the same as `S` or `NS` normally.