Module:Lang: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Saag (talk | contribs)
Added retrieval of lang code from subpage name
Saag (talk | contribs)
Added _get_string for use internally
 
(7 intermediate revisions by 2 users not shown)
Line 23: Line 23:
local data = get_lang_file(lang_code)
local data = get_lang_file(lang_code)
if (data == nil) then
if (data == nil) then
return string.format("Language file for code '%s' not found", lang_code)
return string.format("Lang code '%s' does not have a json file", lang_code)
end
end
Line 29: Line 29:
if (label == nil) then  
if (label == nil) then  
return string.format("Key '%s' not found", key)  
return string.format("Key '%s' not found", key)  
end
return label
end
-- get_string, but for internal use by other modules
p._get_string = function(key)
lang_code = get_lang_code()
local data = get_lang_file(lang_code)
if (data == nil) then
return nil
end
local label = data[key]
if (label == nil) then
return nil
end
end
Line 36: Line 53:
-- Search for a localized string using its English label
-- Search for a localized string using its English label
p.search_string = function(frame)
p.search_string = function(frame)
local key = frame.args[1]
local label = frame.args[1]
local lang_code = frame.args[2]
local lang_code_override = frame.args[2]
local data = get_lang_file(lang_code)
end


function get_lang_code()
local lang_code = lang_code_override
    local title = mw.title.getCurrentTitle()
if (lang_code_override == '') then
    local lang_code = title.fullText:match(".*/(.*)$")
    lang_code = get_lang_code()
   
    -- ensure language code is valid
    local valid_codes = {'en', 'fr'}
if string_in_list(lang_code, valid_codes) then
return lang_code
end
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


-- default to english if none is found
-- Search for the key in the English data
    return 'en'
local key = nil
end
for k, v in pairs(data_en) do
if v == label then
key = k  -- Find the key corresponding to the label
break
end
end


function get_effects(ability, used_keys)
if (key == nil) then
effects = {}
return string.format("English label '%s' not found", label)
for key, value in pairs(ability) do
end
if not string_in_list(key, used_keys) then
local effect_value = string.format("'''%s''': %s", key, value)
if (data_lang[key] == nil) then
table.insert(effects, effect_value)
return string.format("Key '%s' not found in for lang code '%s'", key, lang_code)
end
end
end
return effects
 
return data_lang[key]
end
end


function string_in_list(str, list)
function get_lang_code()
for _, value in ipairs(list) do
    local title = mw.title.getCurrentTitle()
if value == str then
    local lang_code = title.fullText:match(".*/(.*)$")
return true
end
-- if the last part the url is not two letters, then its not a lang code,
-- so default to english
if (lang_code == nil or string.len(lang_code) ~= 2) then
return 'en'
end
end
return false
    return lang_code
end
end


return p
return p

Latest revision as of 21:29, 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("Lang code '%s' does not have a json file", lang_code)	
	end
	
	local label = data[key]
	if (label == nil) then 
		return string.format("Key '%s' not found", key) 
	end
	
	return label
end

-- get_string, but for internal use by other modules
p._get_string = function(key)
	lang_code = get_lang_code()

	local data = get_lang_file(lang_code)
	if (data == nil) then
		return nil
	end
	
	local label = data[key]
	if (label == nil) then 
		return nil
	end
	
	return label
end

-- Search for a localized string using its English label
p.search_string = function(frame)
	local label = 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
	
	-- Load the language files
	local data_en = get_lang_file('en')  -- English data
	local data_lang = get_lang_file(lang_code)  -- Target language data

	-- 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

function get_lang_code()
    local title = mw.title.getCurrentTitle()
    local lang_code = title.fullText:match(".*/(.*)$")
	
	-- if the last part the url is not two letters, then its not a lang code,
	-- so default to english
	if (lang_code == nil or string.len(lang_code) ~= 2) then
		return 'en'	
	end
		
    return lang_code
end

return p