Language translations
paa93 opened this issue ยท 4 comments
All output text should be stored in a way so translations easily can be implemented for any language thus allowing GTW-RPG to be translated into any language in the future. See GTWantispam for a live example of how this should be implemented, more information will be published once it's confirmed to work.
So we start by defining default language in s_lang.lua/c_lang.lua (two files that should be in each resource) fron GTWcore as first option and locally as backup option this way:
-- Definition of default language for this resource
r_lang = exports.GTWcore:getGTWLanguage() or "en_US"
Then we store the translations in txt = {
["lang_ID_1"]={ ["msg1"]="message1", ["msg2"]="message2", ... }
["lang_ID_2"]={ ["msg1"]="message1", ["msg2"]="message2", ... }
...}
TODO: create a function that takes a language ID as argument and returns corresponding result from the table and if there is no such text, return the english translations (default) and as a last options return empty or an error message.
Now, wouldn't it be better to use a shared language file instead of s_lang.lua/c_lang.lua and simply name it lang.lua?
Something like this maybe:
--[[ Language translations for resource ]]--
function get_translation(text_id, input_lang)
-- Validation of language code
if not input_lang then input_lang = "en_US" end
-- Validation of request
if not txt[input_lang] then return "" end
if not txt[input_lang][text_id] then return "" end
-- Return translation
return txt[input_lang][text_id]
end
I'd also suggest a modification to include a element data post for the client so that players can choose their own language (stored as element data) via command or a GUI, like this:
function get_translation(text_id, input_lang, client)
Where "client" is an optional argument indicating if we should check for client preferences for local translations, (some features might not use this).
Hi everyone, your project is very cool especially as it is open-source.
Was just reading this issue I faced myself with one of my personal resources and it looks like you didn't think about "formatable" translations as I would call them.
I mean for example a string with standard Lua placeholders for variables:
["msg1"]="You need %s dollars to buy this house !"
So the function definition would look like this:
function get_translation(text_id, input_lang, ...)
Keep up the good work and I'll probably check the whole sources when I'll have some free time.
Cheers,
Citizen
(French Section Moderator just FYI)
Brilliant, just remembered all the places where variables are used within the strings and how it wouldn't work to pass those other than as arguments. I can't believe I forgot those and I'm glad I haven't started this translation feature yet.