Module:Lang: Difference between revisions

Jump to navigation Jump to search
Moved `get_lang_code()` to the module table (`p`).
Fixed processing of HTML tags for get_string
Line 29: Line 29:
end
end


-- Helper function to handle newline conversion
local function process_newlines(text)
local function process_newlines(text)
     if not text then return text end
     if not text or type(text) ~= "string" then return text end
     -- Replace "n with newline character
     -- Replace "n with newline character
     text = text:gsub('"n', '\n')
    return text:gsub('"n', '\n')
end
 
local function process_html_tags(text)
    if not text or type(text) ~= "string" then return text end
   
    -- Replace <span class="highlight">...</span> with '''...'''
    text = text:gsub('<span class="highlight">(.-)</span>', "'''%1'''")
   
    -- Replace <span class="diminish">...</span> with gray text
     text = text:gsub('<span class="diminish">(.-)</span>', '<span style="color:#777777">%1</span>')
   
    -- Remove all other span tags (keeping only content)
    text = text:gsub('<span class=".-">(.-)</span>', '%1')
   
    -- Process <br> tags (replace with newline)
    text = text:gsub('<br>', '\n')
    text = text:gsub('<br/>', '\n')
    text = text:gsub('<br />', '\n')
   
     return text
     return text
end
end


-- Get a localized string by the raw key
function p.get_string(key, lang_code_override, fallback_str, remove_var_index)
p.get_string = function(key, lang_code_override, fallback_str, remove_var_index)
     -- Get frame object (either passed directly or via first argument)
     -- Get frame object (either passed directly or via first argument)
     local frame
     local frame
Line 66: Line 83:
     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 (without HTML processing for fallback)
         local fallback_tooltip = frame:expandTemplate{title = "MissingValveTranslationTooltip"}
         local fallback_tooltip = frame:expandTemplate{title = "MissingValveTranslationTooltip"}
         local fallback
         local fallback
Line 86: Line 103:
     end
     end
      
      
     -- Process newlines and return
     -- Process HTML and return as "raw" HTML
     label = process_newlines(label)
     label = process_newlines(label)
     return label
    label = process_html_tags(label)
    -- Create HTML object for safe output
    local html = mw.html.create()
    html:wikitext(label)
     return tostring(html)
end
end