This commit is contained in:
BrunoMine 2021-07-05 05:30:32 -04:00 committed by GitHub
commit 21eecfaf3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 202 additions and 15 deletions

View File

@ -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 `NS` 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]

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

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

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

@ -0,0 +1,55 @@
# 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-07-16 11:41-0300\n"
"PO-Revision-Date: 2017-07-16 11:45-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] ""
#: 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!"

View File

@ -2,13 +2,14 @@
# 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-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 <kaeza@users.sf.net>\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!"

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

@ -0,0 +1,55 @@
# 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-07-16 11:41-0300\n"
"PO-Revision-Date: 2017-07-16 11:45-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."
#: 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!"

View File

@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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] ""

View File

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

View File

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

View File

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