Module:Lang: Difference between revisions
Jump to navigation
Jump to search
Added retrieval of lang code from subpage name |
More general check for subpage lang code |
||
Line 44: | Line 44: | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
local lang_code = title.fullText:match(".*/(.*)$") | local lang_code = title.fullText:match(".*/(.*)$") | ||
-- if the last part the url is two letters, then it is likely a valid lang code | |||
if (string.len(lang_code) == 2) then | |||
if | return lang_code | ||
return lang_code | |||
end | end | ||
-- default to english if none is found | -- default to english if none is found | ||
return 'en' | return 'en' | ||
end | end | ||
return p | return p |
Revision as of 17:37, 15 September 2024
Documentation for this module may be created at Module:Lang/doc
local p = {}
function get_lang_file(lang_code)
local file_name = string.format("Data:Lang_%s.json", lang_code)
local success, data = pcall(mw.loadJsonData, file_name)
if success then
return data
else
return nil
end
end
-- Get a localized string by the raw key
p.get_string = function(frame)
local key = frame.args[1]
local lang_code_override = frame.args[2]
local lang_code = lang_code_override
if (lang_code_override == '') then
lang_code = get_lang_code()
end
local data = get_lang_file(lang_code)
if (data == nil) then
return string.format("Language file for code '%s' not found", lang_code)
end
local label = data[key]
if (label == nil) then
return string.format("Key '%s' not found", key)
end
return label
end
-- Search for a localized string using its English label
p.search_string = function(frame)
local key = frame.args[1]
local lang_code = frame.args[2]
local data = get_lang_file(lang_code)
end
function get_lang_code()
local title = mw.title.getCurrentTitle()
local lang_code = title.fullText:match(".*/(.*)$")
-- if the last part the url is two letters, then it is likely a valid lang code
if (string.len(lang_code) == 2) then
return lang_code
end
-- default to english if none is found
return 'en'
end
return p