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") diff --git a/init.lua b/init.lua index 0778a73..ab7e53e 100644 --- a/init.lua +++ b/init.lua @@ -178,8 +178,15 @@ 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 + gettext_getters[modname] = { gettext_func, ngettext_func, sgettext_func } + return gettext_func, ngettext_func, sgettext_func end diff --git a/intltest/init.lua b/intltest/init.lua index e855621..89735c6 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 = dofile(MP.."/intllib.lua") local use_count = 0 @@ -26,3 +26,8 @@ minetest.register_craftitem("intltest:test", { minetest.chat_send_player(user:get_player_name(), message) end, }) + +minetest.log("action", "(nil)"..SS(nil, "Test: @1 @2", SS(nil, "Blue"), SS(nil, "Car"))) +minetest.log("action", "(es) "..SS("es", "Test: @1 @2", SS("es", "Blue"), SS("es", "Car"))) +minetest.log("action", "(pt) "..SS("pt", "Test: @1 @2", SS("pt", "Blue"), SS("pt", "Car"))) +minetest.log("action", "(de) "..SS("de", "Test: @1 @2", SS("de", "Blue"), SS("de", "Car"))) diff --git a/intltest/locale/de.po b/intltest/locale/de.po new file mode 100644 index 0000000..ec772f1 --- /dev/null +++ b/intltest/locale/de.po @@ -0,0 +1,45 @@ +# 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-02-25 20:40-0300\n" +"PO-Revision-Date: 2017-07-13 13:36-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] "" diff --git a/intltest/locale/pt.po b/intltest/locale/pt.po new file mode 100644 index 0000000..6f6b323 --- /dev/null +++ b/intltest/locale/pt.po @@ -0,0 +1,45 @@ +# 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-02-25 20:40-0300\n" +"PO-Revision-Date: 2017-07-13 13:35-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." diff --git a/lib/intllib.lua b/lib/intllib.lua index 6669d72..e1705e9 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 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 = intllib.make_gettext_pair() else -- Old method using text files. gettext = intllib.Getter() @@ -42,4 +42,6 @@ ngettext = ngettext or function(msgid, msgid_plural, n, ...) return format(n==1 and msgid or msgid_plural, ...) end -return gettext, ngettext +sgettext = sgettext or gettext + +return gettext, ngettext, sgettext