Module:DictionaryGive feedback
Overview
See {{Translate}} for how/when to use and more documentation.
p.translate
Translates a given key from Data:Dictionary. Manual invocation should only be used when additional parameters are required, such as {{Update layout}}, where the DISPLAYTITLE is generated using a translation key with expected parameters.
For normal usage, use the basic template.
Function parameters
- key – translation key (required).
- Language-specific:
- lang_code_override (optional, named parameter) – language code overriding the automatic detection. See Data:LangCodes.json.
- fallback_str (optional, named parameter) – value returned only if translation is missing. If set to empty string or nil, fallback will be empty (useful for detecting missing translations).
Examples
{{#invoke:Dictionary|translate|Update history}} Output: Update history
{{#invoke:Dictionary|translate|Updates|lang_code_override=pt-br}} Output: Atualizações
{{#invoke:Dictionary|translate|Update history|fallback_str=Custom fallback text.}} Output: Custom fallback text.[nt]
local p = {}
local dictionary_data = mw.loadJsonData("Data:Dictionary")
local lang_codes_set = mw.loadJsonData("Data:LangCodes.json")
-- Used by [[Template:Translate]]
-- Original author User:sur v2 User:mgpt
-- Get a localized string by the raw key
p.translate = function(key, lang_code_override, fallback_str, vars)
-- 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
local args = frame.args
key = frame.args[1]
lang_code_override = frame.args["lang_code_override"]
fallback_str = frame.args["fallback_str"]
vars = {}
local i = 2
while args[i] do
table.insert(vars, args[i])
i = i + 1
end
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
local translation = translations[lang_code]
if (translation == nil) 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
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
if vars ~= nil then
for i, v in ipairs(vars) do
if translation ~= nil then
translation = translation:gsub("{{{" .. i .. "}}}", v or "")
end
end
end
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
return p