Module:Lang: Difference between revisions

Jump to navigation Jump to search
Sur (talk | contribs)
m _search_string properly passes as frame
Sur (talk | contribs)
m _search_string STRUGGLE IS REAL
Line 90: Line 90:


-- search_string, but for internal use by other modules
-- search_string, but for internal use by other modules
p._search_string = function(key, lang_code_override)
p._search_string = function(label, lang_code_override)
frame = {}
local lang_code = lang_code_override
frame[1] = key
if (lang_code_override == '') then
if (lang_code_override ~= nil) then frame[2] = lang_code_override end
    lang_code = get_lang_code()
return p.search_string(frame)
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