Editing
Module:Dictionary
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
local p = {} local dictionary_data = mw.loadJsonData("Data:Dictionary") local lang_codes_set = mw.loadJsonData("Data:LangCodes.json") --Used by [[Template:Translate]], see documentation there -- Get a localized string by the raw key p.translate = function(key, lang_code_override, fallback_str, show_needs_translation) -- If called internally (direct Lua call), args will be passed directly. -- 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) if type(key) == "table" and key.args then local frame = key key = frame.args[1] lang_code_override = frame.args["lang_code_override"] fallback_str = frame.args["fallback_str"] show_needs_translation = frame.args["show_needs_translation"] end --default to showing needs translation if (show_needs_translation == nil) then show_needs_translation = 'true' end local lang_code = lang_code_override if (lang_code == '' or lang_code == nil) then lang_code = get_lang_code() end local translations = dictionary_data[key] if (translations == nil) then return string.format("Key '%s' is not in Data:Dictionary", key) end translation = translations[lang_code] if (translation == nil) then -- Determine the appended tooltip local needs_translation_expand = '' if (show_needs_translation == 'true' and key ~= 'MissingValveTranslationTooltip' and key ~= 'NeedsTranslationTooltip' and key ~= 'NotesNeedsTranslation') then local template_args = {} if key == 'Notes' then template_args[1] = 'NotesNeedsTranslation' end needs_translation_expand = mw.getCurrentFrame():expandTemplate{title = 'NeedsTranslationTooltip', args=template_args} end -- Determine the fallback string to use --default to english fallback if none specified or if en specified if (fallback_str == 'en' or fallback_str == nil) then translation = translations["en"] else translation = fallback_str -- If english translation is missing, default to the key instead of blank string if (translation == nil) then translation = key end end -- Add the tooltip if (translation ~= '' and translation ~= nil) then translation = translation .. needs_translation_expand end end return translation end function p.translate_embed(key, var1, var2, var3, var4, var5) -- If called internally (direct Lua call), args will be passed directly. -- If called from wikitext, `key` will be the `frame` object, and we get args from `frame.args`. local vars -- Handle the case where it's called via #invoke (i.e., from wikitext) if type(key) == "table" and key.args then local frame = key key = frame.args[1] -- Collect all the variables from the frame (starting from the second argument) vars = {} local i = 2 while frame.args[i] do table.insert(vars, frame.args[i]) i = i + 1 end else vars = {[1] = var1, [2] = var2, [3] = var3, [4] = var4, [5] = var5} end -- Retrieve the translation string using the provided key local translation = p.translate(key) if not translation then return string.format("No translation found for key '%s'", key) end -- Now we use string.format to insert variables into the translation string local formatted_translation = string.format(translation, unpack(vars)) -- Return the final string with embedded variables return formatted_translation end function get_lang_code() local title = mw.title.getCurrentTitle() local lang_code = title.fullText:match(".*/(.*)$") -- if the last part the url is not two letters, then its not a lang code, -- so default to english if (lang_code == nil or lang_codes_set[lang_code] == nil) then return 'en' end return lang_code end return p
Summary:
Please note that all contributions to 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
Templates used on this page:
Data:Dictionary
(
edit
)
Data:LangCodes.json
(
edit
)
Template:NeedsTranslationTooltip
(
edit
)
Template:Tooltip
(
edit
)
Module:Dictionary
(
edit
)
Module:Dictionary/doc
(
edit
)
Navigation menu
Personal tools
English
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Module
Discussion
English
Views
Read
Edit source
View history
More
Search
Navigation
Main page
Discord community
Recent changes
Random page
Tools
What links here
Related changes
Special pages
Page information
Languages