Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Lang: Difference between revisions

From Deadlock Wiki
Added retrieval of lang code from subpage name
Sur (talk | contribs)
m bugfix with 'dictionary' fallback option
 
(46 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local util_module = require("Module:Utilities")
local lang_codes_set = mw.loadJsonData("Data:LangCodes.json")
local dictionary_module = require("Module:Dictionary")
-- Overrides applied to searches by key. Designed to handle edge cases where
-- the expected key does not have a localization entry
local KEY_OVERRIDES = {
    MoveSlowPercent_label = 'MovementSlow_label',
    BonusHealthRegen_label = 'HealthRegen_label',
    BarbedWireRadius_label = 'Radius_label',
    BarbedWireDamagePerMeter_label = 'DamagePerMeter_label',
    BuildUpDuration_label = 'BuildupDuration_label',
    TechArmorDamageReduction_label = 'TechArmorDamageReduction_Label',
    DamageAbsorb_label = 'DamageAbsorb_Label',
    InvisRegen_label = 'InvisRegen_Label',
    EvasionChance_label = 'EvasionChance_Label',
    DelayBetweenShots_label = 'DelayBetweenShots_Label',
}


function get_lang_file(lang_code)
function get_lang_file(lang_code)
Line 12: Line 30:


-- Get a localized string by the raw key
-- Get a localized string by the raw key
p.get_string = function(frame)
p.get_string = function(key, lang_code_override, fallback_str, remove_var_index)
local key = frame.args[1]
-- If called internally (direct Lua call), args will be passed directly.
local lang_code_override = frame.args[2]
    -- If called from wikitext, `key` will be the `frame` object, and we get args from `frame.args`.
 
    -- Handle the case where it's called via #invoke (i.e., from wikitext)
    if type(key) == "table" and key.args then
        local frame = key
        key = frame.args[1]
        lang_code_override = frame.args["lang_code_override"]
        fallback_str = frame.args["fallback_str"]
        remove_var_index = frame.args["remove_var_index"]
    end


-- Determine lang_code if not overridden
local lang_code = lang_code_override
local lang_code = lang_code_override
if (lang_code_override == '') then
if (lang_code == '' or lang_code == nil) then
     lang_code = get_lang_code()
     lang_code = get_lang_code()
end
end


-- Retrieve lang data
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 file for code '%s' not found", lang_code)
return string.format("Lang code '%s' does not have a json file", lang_code)
end
-- Localize
local label = data[KEY_OVERRIDES[key] or key]
if (label == nil) then
-- Apply fallback
local fallback_tooltip = mw.getCurrentFrame():expandTemplate{title = "MissingValveTranslationTooltip"}
local fallback
if (fallback_str == 'en') then
fallback = p.get_string(key, 'en', key .. fallback_tooltip, remove_var_index, upper_lower)
elseif fallback_str == 'dictionary' then
return dictionary_module.translate(key, lang_code_override)
elseif fallback_str ~= nil then
fallback = fallback_str
else
return ''
end
return fallback .. fallback_tooltip
end
end
local label = data[key]
-- Apply remove_var
if (label == nil) then  
if (remove_var_index ~= nil) then  
return string.format("Key '%s' not found", key)  
label = util_module.remove_var(label, remove_var_index)
end
end
Line 36: Line 83:
-- Search for a localized string using its English label
-- Search for a localized string using its English label
p.search_string = function(frame)
p.search_string = function(frame)
local key = frame.args[1]
local label = frame.args[1]
local lang_code = frame.args[2]
local lang_code_override = frame.args[2]
local data = get_lang_file(lang_code)
 
return p._search_string(label, lang_code_override)
end
end


function get_lang_code()
-- search_string, but for internal use by other modules
    local title = mw.title.getCurrentTitle()
p._search_string = function(label, lang_code_override)
    local lang_code = title.fullText:match(".*/(.*)$")
lang_code = lang_code_override
   
if (lang_code == '' or lang_code == nil) then
    -- ensure language code is valid
lang_code = get_lang_code()
    local valid_codes = {'en', 'fr'}
if string_in_list(lang_code, valid_codes) then
return lang_code
end
end


-- default to english if none is found
-- Load the language files
    return 'en'
local data_en = get_lang_file('en')  -- English data
end
local data_lang = get_lang_file(lang_code)  -- Target language data


function get_effects(ability, used_keys)
if (data_lang == nil) then
effects = {}
return string.format("Lang code '%s' does not have a json file", lang_code)
for key, value in pairs(ability) do
end
if not string_in_list(key, used_keys) then
local effect_value = string.format("'''%s''': %s", key, value)
-- Search for the key in the English data
table.insert(effects, effect_value)
local key = nil
for k, v in pairs(data_en) do
if v == label then
key = k  -- Find the key corresponding to the label
break
end
end
end
end
return effects
 
if (key == nil) then
return string.format("English label '%s' not found", label)
end
if (data_lang[key] == nil) then
return label
end
 
return data_lang[key]
end
end


function string_in_list(str, list)
function get_lang_code()
for _, value in ipairs(list) do
    local title = mw.title.getCurrentTitle()
if value == str then
    local lang_code = title.fullText:match(".*/(.*)$")
return true
   
end
if lang_code == nil or lang_codes_set[lang_code] == nil then
return 'en'
end
end
return false
    return lang_code
end
end


return p
return p