Module:Sandbox: Difference between revisions

No edit summary
No edit summary
Line 27: Line 27:
         return nil
         return nil
     end
     end
end
-- Helper function to preprocess text with proper newline handling
local function preprocess_text(text)
    if not text then return text end
    -- First replace \"n with actual newlines
    text = text:gsub('\\"n', '\n')
    -- Then let MediaWiki's preprocess handle the newlines and other wikitext
    return mw.text.preprocess(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


Line 68: Line 59:
     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)
Line 87: Line 78:
     end
     end
      
      
     -- Preprocess the text (handles newlines and other wikitext)
     -- Replace \"n with <br> tags using frame:preprocess()
     return preprocess_text(label)
     label = label:gsub('\\"n', '<br>')
    label = frame:preprocess(label)
   
    return label
end
end


Line 96: Line 90:
     local lang_code_override = frame.args[2]
     local lang_code_override = frame.args[2]


     return p._search_string(label, lang_code_override)
     -- Replace \"n with <br> tags using frame:preprocess()
    local result = p._search_string(label, lang_code_override)
    result = result:gsub('\\"n', '<br>')
    return frame:preprocess(result)
end
end


Line 125: Line 122:
     -- 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 preprocess_text(label)
         return label
     end
     end
     if (data_lang[key] == nil) then
     if (data_lang[key] == nil) then
         return preprocess_text(label)
         return label
     end
     end


    -- Preprocess the text (handles newlines and other wikitext)
     return data_lang[key]
     return preprocess_text(data_lang[key])
end
end