Enhanced features are added.

Better translations registration feature.
Patching mod feature available.
master
Jonathan jegouzo 2012-10-04 13:13:49 +02:00
parent 43616ce3b7
commit 83796f1dde
3 changed files with 126 additions and 41 deletions

View File

@ -19,11 +19,10 @@ language with less work needed.
Usage:
------
The minetest setting 'language' is set to EN when this mod is first loaded.
You can modify it in your minetest.conf.
You can modify this setting in your minetest.conf.
If no translation is found for a message, his english counterpart is used instead.
If no english counterpart are found, then an error message is returned.
If no english counterpart are found, then no error are thrown but an error message is returned instead of the translation.
Exemple(Code lua) :
-------------------
@ -40,11 +39,38 @@ Exemple(Code lua) :
message_with_some_parameters = "$1,$2,$3"
}
}
translate(translations,"message1") -> "english version" -- si language = EN
translate(translations,"message1") -> "version française" -- si language = FR
translate(translations,"message1") -> "english version" -- si language = other than FR
translate(translations,"message_existing_in_english_only") -> "lorem ipsum"
translate(translations,"message_with_parameter","value") -> "value will be replaced by the first additionnal parameter" -- si language = EN
translate(translations,"message_with_parameter","value") -> "value va être remplacé par la 1ère valeur additionnelle" -- si language = FR
translate(translations,"message_with_some_parameters","value",2,3.5) -> "value,2,3.5"
translate(translations,"message_inexistant","value",2,3.5) -> "Translate(EN,message_inexistant) = No translations available"
-- Registering the translations
localisation.register_translations("your_mod_name",your_var_containing_translations)
localisation.translate("your_mod_name:message1") -> "english version" -- si language = EN
localisation.translate("your_mod_name:message1") -> "version française" -- si language = FR
localisation.translate("your_mod_name:message1") -> "english version" -- si language = other than FR
localisation.translate("your_mod_name:message_existing_in_english_only") -> "lorem ipsum"
localisation.translate("your_mod_name:message_with_parameter","value") -> "value will be replaced by the first additionnal parameter" -- si language = EN
localisation.translate("your_mod_name:message_with_parameter","value") -> "value va être remplacé par la 1ère valeur additionnelle" -- si language = FR
localisation.translate("your_mod_name:message_with_some_parameters","value",2,3.5) -> "value,2,3.5"
localisation.translate("your_mod_name:message_inexistant","value",2,3.5) -> "Translate(EN,message_inexistant) = No translations available" -- si language = EN
localisation.translate("bad_message_without_mod_name","value",2,3.5) -> "No mod_name specified or other error message"
Language Patching
-----------------
If you want to translate a mod using this mod, it's possible.
Create a mod and/or specify the mod_name in the depends.txt and and register your translation with the mod_name.
The messages that wont be patched will be the messages that are used before the initialisation of the patch mod.
Exemple
-------
If you want to add another language support(DE by exemple) to this mod(localisation), you can do it like that
local translations = {
EN = {
no_translation_available = "new english message, just because",
},
DE = {
no_translation_available = "...",
no_translation_registered_for_mod = "...",
}
}
localisation.translate("localisation:no_translation_available") = <previous message>
localisation.register_translations("localisation",translations)
localisation.translate("localisation:no_translation_available") = "new english message, just because"

104
init.lua
View File

@ -5,34 +5,78 @@ if minetest.setting_get("language") == nil then
minetest.setting_set("language", "EN")
end
function translate(label_list,msg_label,...)
local function translation(label_list,language,msg_label,...)
local msg = label_list[language][msg_label]
local args = {...}
for key,arg in ipairs(args) do
msg = string.gsub(msg, "%$"..key.."", arg)
end
return msg
end
local function err(language,msg_label)
return "Translate("..language..","..msg_label..") = No translations available"
end
local language = minetest.setting_get("language") -- has previously been defined as EN if not set
local fallback = "EN"
if label_list[language] == nil then
-- Fallback on english
language = fallback
if label_list[language] == nil then
return
end err(language,msg_label)
else
if label_list[language][msg_label] == nil then
if language == fallback then
return err(language,msg_label)
else
return translation(label_list,fallback,msg_label,...)
end
end
end
return translation(label_list,language,msg_label,...)
localisation = {}
localisation.translations_per_mod = {} -- will received and store the translations
function localisation.register_translations(mod_name,translations)
local function merge_translation(translations_old,translations_new)
for field_name,field_value in pairs(translations_new) do
translations_old[field_name] = field_value
end
return translations_old
end
if localisation.translations_per_mod[mod_name] ~= nil then
local result_trans = localisation.translations_per_mod[mod_name]
for lang,trans in pairs(translations) do
if result_trans[lang] == nil then
result_trans[lang] = {}
end
result_trans[lang] = merge_translation(result_trans[lang],trans)
end
localisation.translations_per_mod[mod_name] = result_trans
else
localisation.translations_per_mod[mod_name] = translations
end
end
-- localisation , need the function previously defined
dofile(minetest.get_modpath("localisation").."/localisation.lua")
-- msg_label should be prefixed by mod_name like this "mod_name:msg_label_value"
function localisation.translate(msg_label,...)
local fallback = "EN"
local function translation(label_list,language,msg_label,fallback,...)
local result = ""
if label_list[language] == nil then
-- Fallback on english
language = fallback
result = translation(label_list,language,msg_label,fallback,...)
else
if label_list[language][msg_label] == nil then
if language ~= fallback then
language = fallback
result = translation(label_list,language,msg_label,fallback,...)
else
return nil
end
else
local msg = label_list[language][msg_label]
local args = {...}
for key,arg in ipairs(args) do
msg = string.gsub(msg, "%$"..key.."", arg)
end
result = msg
end
end
return result
end
if msg_label == nil then
return localisation.translate("localisation:nil_value_for_msg_label")
end
local mod_name = string.match(msg_label,"([%w_]+):")
msg_label = string.match(msg_label,":([%w_]+)")
if mod_name == nil then
return localisation.translate("localisation:no_mod_specified_for_label",msg_label)
end
local label_list = localisation.translations_per_mod[mod_name]
if label_list == nil then
return localisation.translate("localisation:no_translation_registered_for_mod",mod_name)
end
local language = minetest.setting_get("language") -- has previously been defined as EN if not set
local result = translation(label_list,language,msg_label,fallback,...)
if type(result) == "string" then
return result
else
return localisation.translate("localisation:no_translation_available",language,msg_label,mod_name,fallback)
end
end

15
localisation.lua Normal file
View File

@ -0,0 +1,15 @@
localisation.register_translations("localisation",{
EN = {
no_translation_available = "localisation.translate($2) = No translations available in $1 nor in $4 for mod $3",
no_translation_registered_for_mod = "localisation.translate(msg_label) = No translations registered for this mod : $1",
nil_value_for_msg_label = "localisation.translate(msg_label) = msg_label got nil as value",
no_mod_specified_for_label = "localisation.translate($1) = The label $1 was not prefixed by the mod name",
},
FR = {
no_translation_available = "localisation.translate($2) = Pas de traductions disponibles en $1 ni en $4 pour le mod $3",
no_translation_registered_for_mod = "localisation.translate(msg_label) = Aucune traduction enregistrée pour ce mod : $1",
nil_value_for_msg_label = "localisation.translate(msg_label) = msg_label avait nil comme valeur",
no_mod_specified_for_label = "localisation.translate($1) = The label $1 n'était pas préfixé par le nom du mod",
},
}
)