Module:Lang: Difference between revisions

m Default search string to input label if none is found
Parse "n as newline
Line 20: Line 20:


function get_lang_file(lang_code)
function get_lang_file(lang_code)
local file_name = string.format("Data:Lang_%s.json", lang_code)
    local file_name = string.format("Data:Lang_%s.json", lang_code)
     local success, data = pcall(mw.loadJsonData, file_name)
     local success, data = pcall(mw.loadJsonData, file_name)
     if success then
     if success then
Line 27: Line 27:
         return nil
         return nil
     end
     end
end
-- Helper function to handle newline conversion
local function process_newlines(text)
    if not text then return text end
    -- Replace "n with newline character
    text = text:gsub('"n', '\n')
    return text
end
end


-- Get a localized string by the raw key
-- Get a localized string by the raw key
p.get_string = function(key, lang_code_override, fallback_str, remove_var_index)
p.get_string = function(key, lang_code_override, fallback_str, remove_var_index)
-- If called internally (direct Lua call), args will be passed directly.
    -- Get frame object (either passed directly or via first argument)
     -- If called from wikitext, `key` will be the `frame` object, and we get args from `frame.args`.
     local frame
 
    -- Handle the case where it's called via #invoke (i.e., from wikitext)
     if type(key) == "table" and key.args then
     if type(key) == "table" and key.args then
         local frame = key
         frame = key
         key = frame.args[1]
         key = frame.args[1]
         lang_code_override = frame.args["lang_code_override"]
         lang_code_override = frame.args["lang_code_override"]
         fallback_str = frame.args["fallback_str"]
         fallback_str = frame.args["fallback_str"]
         remove_var_index = frame.args["remove_var_index"]
         remove_var_index = frame.args["remove_var_index"]
    else
        frame = mw.getCurrentFrame()
     end
     end


-- Determine lang_code if not overridden
    -- Determine lang_code if not overridden
local lang_code = lang_code_override
    local lang_code = lang_code_override
if (lang_code == '' or lang_code == nil) then
    if (lang_code == '' or lang_code == nil) then
    lang_code = get_lang_code()
        lang_code = get_lang_code()
end
    end


-- Retrieve lang data
    -- Retrieve lang data
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("Lang code '%s' does not have a json file", lang_code)
        return string.format("Lang code '%s' does not have a json file", lang_code)  
end
    end
   
-- Localize
    -- Localize
local label = data[KEY_OVERRIDES[key] or key]
    local label = data[KEY_OVERRIDES[key] or key]
if (label == nil) then
    if (label == nil) then
-- Apply fallback
        -- Apply fallback
local fallback_tooltip = mw.getCurrentFrame():expandTemplate{title = "MissingValveTranslationTooltip"}
        local fallback_tooltip = frame:expandTemplate{title = "MissingValveTranslationTooltip"}
local fallback
        local fallback
if (fallback_str == 'en') then
        if (fallback_str == 'en') then
fallback = p.get_string(key, 'en', key .. fallback_tooltip, remove_var_index, upper_lower)
            fallback = p.get_string(key, 'en', key .. fallback_tooltip, remove_var_index)
elseif fallback_str == 'dictionary' then
        elseif fallback_str == 'dictionary' then
return dictionary_module.translate(key, lang_code_override)
            return dictionary_module.translate(key, lang_code_override)
elseif fallback_str ~= nil then
        elseif fallback_str ~= nil then
fallback = fallback_str
            fallback = fallback_str
else
        else
return ''
            return ''
end
        end
return fallback .. fallback_tooltip
        return fallback .. fallback_tooltip
end
    end
   
-- Apply remove_var
    -- Apply remove_var
if (remove_var_index ~= nil) then  
    if (remove_var_index ~= nil) then  
label = util_module.remove_var(label, remove_var_index)
        label = util_module.remove_var(label, remove_var_index)
end
    end
   
return label
    -- Process newlines and return
    label = process_newlines(label)
    return label
end
end


-- 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 label = frame.args[1]
    local label = frame.args[1]
local lang_code_override = frame.args[2]
    local lang_code_override = frame.args[2]


return p._search_string(label, lang_code_override)
    local result = p._search_string(label, lang_code_override)
    result = process_newlines(result)
    return result
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, lang_code_override)
p._search_string = function(label, lang_code_override)
lang_code = lang_code_override
    lang_code = lang_code_override
if (lang_code == '' or lang_code == nil) then
    if (lang_code == '' or lang_code == nil) then
lang_code = get_lang_code()
        lang_code = get_lang_code()
end
    end


-- Load the language files
    -- Load the language files
local data_en = get_lang_file('en')  -- English data
    local data_en = get_lang_file('en')  -- English data
local data_lang = get_lang_file(lang_code)  -- Target language data
    local data_lang = get_lang_file(lang_code)  -- Target language data


if (data_lang == nil) then
    if (data_lang == nil) then
error("Lang code '%s' does not have a json file", lang_code)
        error("Lang code '%s' does not have a json file", lang_code)  
end
    end
   
-- Search for the key in the English data
    -- Search for the key in the English data
local key = nil
    local key = nil
for k, v in pairs(data_en) do
    for k, v in pairs(data_en) do
if v == label then
        if v == label then
key = k  -- Find the key corresponding to the label
            key = k  -- Find the key corresponding to the label
break
            break
end
        end
end
    end


-- Default to input label if localized string is not found
    -- Default to input label if localized string is not found
if (key == nil) then
    if (key == nil) then
return label
        return label
end
    end
if (data_lang[key] == nil) then
    if (data_lang[key] == nil) then
return label
        return label
end
    end


return data_lang[key]
    return data_lang[key]
end
end


Line 128: Line 140:
     local lang_code = title.fullText:match(".*/(.*)$")
     local lang_code = title.fullText:match(".*/(.*)$")
      
      
if lang_code == nil or lang_codes_set[lang_code] == nil then
    if lang_code == nil or lang_codes_set[lang_code] == nil then
return 'en'
        return 'en'  
end
    end
       
     return lang_code
     return lang_code
end
end


return p
return p