Module:Lang: Difference between revisions

Jump to navigation Jump to search
Sur (talk | contribs)
m lang_codes_set outsourced to Data:LangCodes.json so its also usable by Module:Dictionary
Sur (talk | contribs)
m get_string now handles both internal calls and external invokes, and non-1st args all named
Line 29: Line 29:


-- 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, remove_var_index, upper_lower)
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`.
local lang_code_override
local remove_var_index
local upper_lower
    -- 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"]
        remove_var_index = frame.args["remove_var_index"]
        upper_lower = frame.args["upper_lower"]
    end


-- Determine lang_code if not overridden
local lang_code = lang_code_override
local lang_code = lang_code_override
if (lang_code == '' or lang_code == nil) then
if (lang_code == '' or lang_code == nil) then
Line 38: Line 51:
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
Line 43: Line 57:
end
end
-- Localize
local label = data[KEY_OVERRIDES[key] or key]
local label = data[KEY_OVERRIDES[key] or key]
if (label == nil) then  
if (label == nil) then  
return ''
return ''
end
-- Apply remove_var
if (remove_var_index ~= nil) then
label = util_module.remove_var(remove_var_index)
end
-- Apply upper or lowercase
if (upper_lower ~= nil) then
if (mw.ustring.lower(upper_lower) == 'upper') then
label = mw.ustring.upper(label)
elseif mw.ustring.lower(upper_lower) == 'lower' then
label = mw.ustring.lower(label)
end
end
end