Skip to content

Add Selected gettext method for instant translation #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion doc/developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
16 changes: 13 additions & 3 deletions gettext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 15 additions & 2 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
17 changes: 16 additions & 1 deletion intltest/init.lua
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 changes: 55 additions & 0 deletions intltest/locale/de.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# I18N Test Mod.
# Copyright (C) 2013-2017 Diego Martínez <[email protected]>
# This file is distributed under the same license as the intllib mod.
# Diego Martínez <[email protected]>, 2013-2017.
# Diego Martnez <[email protected]>, 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 <[email protected]>\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!"
16 changes: 14 additions & 2 deletions intltest/locale/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
# Copyright (C) 2013-2017 Diego Martínez <[email protected]>
# This file is distributed under the same license as the intllib mod.
# Diego Martínez <[email protected]>, 2013-2017.
# Diego Martnez <[email protected]>, 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 <[email protected]>\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!"
Expand All @@ -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 changes: 55 additions & 0 deletions intltest/locale/pt.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# I18N Test Mod.
# Copyright (C) 2013-2017 Diego Martínez <[email protected]>
# This file is distributed under the same license as the intllib mod.
# Diego Martínez <[email protected]>, 2013-2017.
# Diego Martnez <[email protected]>, 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 <[email protected]>\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!"
12 changes: 11 additions & 1 deletion intltest/locale/template.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
Expand Down Expand Up @@ -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] ""
14 changes: 11 additions & 3 deletions lib/intllib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
3 changes: 1 addition & 2 deletions tools/xgettext.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tools/xgettext.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down