Module:Lang: Difference between revisions

Jump to navigation Jump to search
Sur (talk | contribs)
m all alternate get_string functions removed, now all work within main get_string
Sur (talk | contribs)
m search_string now calls _search_string
Line 89: Line 89:
local lang_code_override = frame.args[2]
local lang_code_override = frame.args[2]


local lang_code = lang_code_override
return p._search_string(label, lang_code_override)
if (lang_code == '' or lang_code == nil) then
    lang_code = get_lang_code()
end
-- Load the language files
local data_en = get_lang_file('en')  -- English data
local data_lang = get_lang_file(lang_code)  -- Target language data
 
if (data_lang == nil) then
return string.format("Lang code '%s' does not have a json file", lang_code)
end
-- Search for the key in the English data
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
 
if (key == nil) then
return string.format("English label '%s' not found", label)
end
if (data_lang[key] == nil) then
return string.format("Key '%s' not found in for lang code '%s'", key, lang_code)
end
 
return data_lang[key]
end
end


-- search_string, but for internal use by other modules
-- search_string, but for internal use by other modules
p._search_string = function(label)
p._search_string = function(label, lang_code_override)
lang_code = get_lang_code()
lang_code = lang_code_override
if (lang_code == '' or lang_code == nil) then
lang_code = get_lang_code()
end


-- Load the language files
-- Load the language files