Home
Random
Log in
Settings
About the Deadlock Wiki
Search
Editing
Module
:
Dictionary
Give feedback
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.
Anti-spam check. Do
not
fill this in!
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 lang_codes_set = mw.loadJsonData("Data:LangCodes.json") -- Persistent cache across multiple #invoke calls on the same page 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) local is_frame = false local args -- Handle the case where it's called via #invoke (i.e., from wikitext) if type(key) == "table" and key.args then is_frame = true current_frame = key args = current_frame.args key = args[1] lang_code_override = args["lang_code_override"] fallback_str = args["fallback_str"] end local lang_code = lang_code_override if not lang_code or lang_code == '' then lang_code = get_lang_code() end local translations = dictionary_data[key] if not translations then return string_format("Key '%s' is not in Data:Dictionary", key or 'nil') end local translation = translations[lang_code] if not translation then -- Determine the appended tooltip local needs_translation_expand = '' if key ~= 'MissingValveTranslationTooltip' and key ~= 'NeedsTranslationTooltip' and key ~= 'NotesNeedsTranslation' then local template_args = {} if key == 'Notes' then template_args[1] = 'NotesNeedsTranslation' end -- 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 -- Determine the fallback string to use if fallback_str == 'en' or not fallback_str then translation = translations["en"] else translation = fallback_str -- Only fallback to key if explicit fallback_str fails if not translation then translation = key end end -- Add the tooltip if translation and translation ~= '' then translation = translation .. needs_translation_expand end end -- Variable Substitution Optimization if translation then if is_frame then -- Single-pass regex replacement directly from frame arguments (Zero allocations) translation = string_gsub(translation, "{{{(%d+)}}}", function(n) 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 return translation end return p
Summary:
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
Pages included on this page:
Template:NeedsTranslationTooltip
(
edit
)
Template:Tl
(
edit
)
Template:Tooltip
(
edit
)
Module:Dictionary
(
edit
)
Module:Dictionary/doc
(
edit
)
Data:Dictionary
(
edit
)
Data:LangCodes.json
(
view source
) (⧼restriction-level-bot+⧽)