I’ve coded my own localization module based on .po files that contain the translations. I can’t post the whole module, but I can briefly explain the concept.
First I have a table that defines which languages my app supports:
local languages =
{
["en"] = {"English", "English"},
["fr"] = {"French", "Français"},
["de"] = {"German", "Deutsch"},
["zh-Hans"] = {"Chinese", "????"},
["ja"] = {"Japanese", "???"},
["ko"] = {"Korean", "???"},
["es"] = {"Spanish", "Español"}
}
local languages\_mt =
{
\_\_index = function(table, key)
return false; -- ignore all other languages
end
}
setmetatable(languages, languages\_mt);
Then to determine the default language I have this function
local getDefaultLanguage = function()
local systemLang = system.getPreference("ui", "language");
if (not languages[systemLang]) then
if (systemLang == "zh-Hant") then
return "zh-Hans";
else
return "en";
end
else
return systemLang;
end
end
The user can of course choose whichever language supported which I store in my application settings.
When I start my app I just make a call to set the user’s preferred language, which loads the appropriate po file into memory.
localize.setLanguage(settings.language);
.po files have the following layout (Simplified chinese sample)
#: later
msgid "Later"
msgstr "????"
#: demo-mode
msgid "Demo Mode"
msgstr "????"
#: is the tag used in the app to get the translation
msgid is the English term used by the translator
msgstr is the translated string [import]uid: 70847 topic_id: 34379 reply_id: 136657[/import]