Editing Module:Dictionary

Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
local p = {}
local p = {}
-- Localize core functions to bypass global table lookups (_G)
local type = type
local tonumber = tonumber
local string_format = string.format
local string_gsub = string.gsub
-- Load data data sets once at module initialization
local dictionary_data = mw.loadJsonData("Data:Dictionary")
local dictionary_data = mw.loadJsonData("Data:Dictionary")
local lang_codes_set = mw.loadJsonData("Data:LangCodes.json")
local lang_codes_set = mw.loadJsonData("Data:LangCodes.json")
-- Used by [[Template:Translate]]


-- Persistent cache across multiple #invoke calls on the same page
-- Get a localized string by the raw key
local cached_lang_code = nil
local current_frame = nil
 
-- Forward declare local function for speed and scope
local function get_lang_code()
if cached_lang_code then
return cached_lang_code
end
 
-- mw.title.getCurrentTitle() is expensive; we cache the result
local title = mw.title.getCurrentTitle()
local lang_code = title.fullText:match(".*/(.-)$")
 
if not lang_code or not lang_codes_set[lang_code] then
cached_lang_code = 'en'
else
cached_lang_code = lang_code
end
 
return cached_lang_code
end
 
-- Used by [[Template:Translate]]
p.translate = function(key, lang_code_override, fallback_str, vars)
p.translate = function(key, lang_code_override, fallback_str, vars)
local is_frame = false
-- If called internally (direct Lua call), args will be passed directly.
local args
    -- If called from wikitext, "key" will be the "frame" object, and we get args from "frame.args".


-- Handle the case where it's called via #invoke (i.e., from wikitext)
    -- 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
is_frame = true
        local frame = key
current_frame = key
        local args = frame.args
args = current_frame.args
        key = frame.args[1]
key = args[1]
        lang_code_override = frame.args["lang_code_override"]
lang_code_override = args["lang_code_override"]
        fallback_str = frame.args["fallback_str"]
fallback_str = args["fallback_str"]
        vars = {}
end
        local i = 2
        while args[i] do
            table.insert(vars, args[i])
            i = i + 1
        end
    end


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


local translations = dictionary_data[key]
local translations = dictionary_data[key]
if not translations then  
if (translations == nil) then return string.format("Key '%s' is not in Data:Dictionary", key) end
return string_format("Key '%s' is not in Data:Dictionary", key or 'nil')  
end


local translation = translations[lang_code]
local translation = translations[lang_code]


if not translation then
if (translation == nil) then
-- Determine the appended tooltip
-- Determine the appended tooltip
local needs_translation_expand = ''
local needs_translation_expand = ''
if key ~= 'MissingValveTranslationTooltip' and key ~= 'NeedsTranslationTooltip' and key ~= 'NotesNeedsTranslation' then
if (key ~= 'MissingValveTranslationTooltip' and key ~= 'NeedsTranslationTooltip' and key ~= 'NotesNeedsTranslation') then
local template_args = {}
local template_args = {}
if key == 'Notes' then
if key == 'Notes' then
template_args[1] = 'NotesNeedsTranslation'
template_args[1] = 'NotesNeedsTranslation'
end
end
needs_translation_expand = mw.getCurrentFrame():expandTemplate{title = 'NeedsTranslationTooltip', args=template_args}
-- Reuse the frame context to avoid unnecessary boundary shifts
if not current_frame then
current_frame = mw.getCurrentFrame()
end
needs_translation_expand = current_frame:expandTemplate{title = 'NeedsTranslationTooltip', args = template_args}
end
end


-- Determine the fallback string to use
-- Determine the fallback string to use
if fallback_str == 'en' or not fallback_str then
-- Default to english fallback if none specified or if en specified
if (fallback_str == 'en' or fallback_str == nil) then
translation = translations["en"]
translation = translations["en"]
else
else
translation = fallback_str
translation = fallback_str
 
-- Only fallback to key if explicit fallback_str fails
-- If english translation is missing, default to the key instead of blank string
if not translation then
if (translation == nil) then
translation = key
translation = key
end
end
Line 90: Line 59:


-- Add the tooltip
-- Add the tooltip
if translation and translation ~= '' then
if (translation ~= '' and translation ~= nil) then
translation = translation .. needs_translation_expand
translation = translation .. needs_translation_expand
end
end
end
end


-- Variable Substitution Optimization
if vars ~= nil then
if translation then
for i, v in ipairs(vars) do
if is_frame then
if translation ~= nil then
-- Single-pass regex replacement directly from frame arguments (Zero allocations)
translation = translation:gsub("{{{" .. i .. "}}}", v or "")
translation = string_gsub(translation, "{{{(%d+)}}}", function(n)
end
return args[tonumber(n) + 1] or ""
end)
elseif vars then
-- Single-pass regex replacement for standard Lua table arrays
translation = string_gsub(translation, "{{{(%d+)}}}", function(n)
return vars[tonumber(n)] or ""
end)
end
end
end
end


return translation
return translation
end
function get_lang_code()
    local title = mw.title.getCurrentTitle()
    local lang_code = title.fullText:match(".*/(.*)$")
-- Not a lang code, default to english
if (lang_code == nil or lang_codes_set[lang_code] == nil) then
return 'en'
end
    return lang_code
end
end


return p
return p
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template