Module:Lang: Difference between revisions

No edit summary
Improved error handling
Line 2: Line 2:


function get_lang_file(lang_code)
function get_lang_file(lang_code)
local data = mw.loadJsonData(string.format("Data:%s.json", lang_code))
local file_name = string.format("Data:%s.json", lang_code)
if (data == nil) then  
    local success, data = pcall(mw.loadJsonData, file_name)
return string.format("Language code '%s' not found")
    if success then
end
        return data
return data
    else
        return nil
    end
end
end


Line 13: Line 15:
local lang_code = frame.args[1]
local lang_code = frame.args[1]
local key = frame.args[2]
local key = frame.args[2]
local data = get_lang_file(lang_code)
local data = get_lang_file(lang_code)
if (data == nil) then
return string.format("Language code '%s' not found", lang_code)
end
local label = data[key]
local label = data[key]
if (label == nil) then  
if (label == nil) then