Add Selected gettext

This commit is contained in:
BrunoMine 2017-07-13 14:23:08 -03:00
parent 7b818cb4af
commit 34c6e8bab4
6 changed files with 123 additions and 9 deletions

View File

@ -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")

View File

@ -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

View File

@ -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")))

45
intltest/locale/de.po Normal file
View File

@ -0,0 +1,45 @@
# I18N Test Mod.
# Copyright (C) 2013-2017 Diego Martínez <kaeza@users.sf.net>
# This file is distributed under the same license as the intllib mod.
# Diego Martínez <kaeza@users.sf.net>, 2013-2017.
# Diego Martnez <kaeza@users.sf.net>, 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 <kaeza@users.sf.net>\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] ""

45
intltest/locale/pt.po Normal file
View File

@ -0,0 +1,45 @@
# I18N Test Mod.
# Copyright (C) 2013-2017 Diego Martínez <kaeza@users.sf.net>
# This file is distributed under the same license as the intllib mod.
# Diego Martínez <kaeza@users.sf.net>, 2013-2017.
# Diego Martnez <kaeza@users.sf.net>, 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 <kaeza@users.sf.net>\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."

View File

@ -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