Module:Lang: Difference between revisions
swapped argument order |
Added retrieval of lang code from subpage name |
||
Line 14: | Line 14: | ||
p.get_string = function(frame) | p.get_string = function(frame) | ||
local key = frame.args[1] | local key = frame.args[1] | ||
local | local lang_code_override = frame.args[2] | ||
local lang_code = lang_code_override | |||
if (lang_code_override == '') then | |||
lang_code = get_lang_code() | |||
end | |||
local data = get_lang_file(lang_code) | local data = get_lang_file(lang_code) | ||
if (data == nil) then | if (data == nil) then | ||
return string.format("Language code '%s' not found", lang_code) | return string.format("Language file for code '%s' not found", lang_code) | ||
end | end | ||
Line 34: | Line 39: | ||
local lang_code = frame.args[2] | local lang_code = frame.args[2] | ||
local data = get_lang_file(lang_code) | local data = get_lang_file(lang_code) | ||
end | |||
function get_lang_code() | |||
local title = mw.title.getCurrentTitle() | |||
local lang_code = title.fullText:match(".*/(.*)$") | |||
-- ensure language code is valid | |||
local valid_codes = {'en', 'fr'} | |||
if string_in_list(lang_code, valid_codes) then | |||
return lang_code | |||
end | |||
-- default to english if none is found | |||
return 'en' | |||
end | |||
function get_effects(ability, used_keys) | |||
effects = {} | |||
for key, value in pairs(ability) do | |||
if not string_in_list(key, used_keys) then | |||
local effect_value = string.format("'''%s''': %s", key, value) | |||
table.insert(effects, effect_value) | |||
end | |||
end | |||
return effects | |||
end | |||
function string_in_list(str, list) | |||
for _, value in ipairs(list) do | |||
if value == str then | |||
return true | |||
end | |||
end | |||
return false | |||
end | end | ||
return p | return p |
Revision as of 13:44, 15 September 2024
Overview
overview
Functions
get_string
Localizes a given string to the current language, i.e. Data:Lang_en.json for english.
Parameters
- key - Key string to localize
- lang_code_override (OPTIONAL) - Overrides the current language to a specific language code
- fallback_str (OPTIONAL) - Passing
en
causes it to return the english localization if it can't be localized to the current language. Passing any other string causes it to return that string if it can't be localized. Both have Template:MissingValveTranslationTooltip appended. Use this very often as some keys are not yet localized in every language by the game. Passingdictionary
causes it to return the translation via Data:Dictionary if it can't be localized. If parsing the fallback_str from lua and is computationally expensive (i.e. add_space_before_cap), consider using fallback outside this function so it only computes when needed. - remove_var_index (OPTIONAL) - Removes %variables% from the resulting string. -1 also removes the character prefixing %variables%, while 1 removes the postfixed character, and 0 removes only the %variables%.
NOTE: Optional parameters are ideally named when not all parameters are provided, though named parameters can only be passed by invoke, and not internal lua calls.
Examples
Invokes from wikitext:
{{#invoke:Lang|get_string|CitadelHeroStats_Weapon_Falloff}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
{{#invoke:Lang|get_string|CitadelHeroStats_Weapon_Falloff|lang_code_override=es}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
Examples for fallback_str
{{#invoke:Lang|get_string|StatDesc_CritDamageBonusScale|lang_code_override=es}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
{{#invoke:Lang|get_string|StatDesc_CritDamageBonusScale|lang_code_override=es|fallback_str=en}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
{{#invoke:Lang|get_string|StatDesc_CritDamageBonusScale|lang_code_override=es|fallback_str=Crit Damage Bonus Scale}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
{{#invoke:Lang|get_string|Tech Items|fallback_str=dictionary}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
Examples for remove_var_index
{{#invoke:Lang|get_string|Citadel_HeroBuilds_DefaultHeroBuild}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
TODO: Debug why is =0 still removing that extra space? Doesn't matter yet I suppose, no use cases for 0 yet
{{#invoke:Lang|get_string|Citadel_HeroBuilds_DefaultHeroBuild|remove_var_index=0}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
{{#invoke:Lang|get_string|Citadel_HeroBuilds_DefaultHeroBuild|remove_var_index=-1}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
When calling by internal modules, the parameters cannot be named, and therefore have to be in order. Unused parameters before the last used parameter should be nil
. Such as, .get_string('hero_atlas', nil, 'en')
search_string
Searches for the unlocalized key corresponding to a given english string, then localizes it to the current language. NOTE: Use sparingly, always use get_string instead where plausible, as it has time complexity O(1) compared to search_string's O(10,000).
Parameters
- string - English string to search for
Examples
From wikitext:
{{#invoke:Lang|search_string|Abrams}}
Lua error at line 4: bad argument #2 to 'format' (string expected, got nil).
local p = {}
function get_lang_file(lang_code)
local file_name = string.format("Data:Lang_%s.json", lang_code)
local success, data = pcall(mw.loadJsonData, file_name)
if success then
return data
else
return nil
end
end
-- Get a localized string by the raw key
p.get_string = function(frame)
local key = frame.args[1]
local lang_code_override = frame.args[2]
local lang_code = lang_code_override
if (lang_code_override == '') then
lang_code = get_lang_code()
end
local data = get_lang_file(lang_code)
if (data == nil) then
return string.format("Language file for code '%s' not found", lang_code)
end
local label = data[key]
if (label == nil) then
return string.format("Key '%s' not found", key)
end
return label
end
-- Search for a localized string using its English label
p.search_string = function(frame)
local key = frame.args[1]
local lang_code = frame.args[2]
local data = get_lang_file(lang_code)
end
function get_lang_code()
local title = mw.title.getCurrentTitle()
local lang_code = title.fullText:match(".*/(.*)$")
-- ensure language code is valid
local valid_codes = {'en', 'fr'}
if string_in_list(lang_code, valid_codes) then
return lang_code
end
-- default to english if none is found
return 'en'
end
function get_effects(ability, used_keys)
effects = {}
for key, value in pairs(ability) do
if not string_in_list(key, used_keys) then
local effect_value = string.format("'''%s''': %s", key, value)
table.insert(effects, effect_value)
end
end
return effects
end
function string_in_list(str, list)
for _, value in ipairs(list) do
if value == str then
return true
end
end
return false
end
return p