Module:Dictionary: Difference between revisions
initial |
m comment referring to Template:Translate |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local dictionary_data = mw.loadJsonData("Data:Dictionary") | local dictionary_data = mw.loadJsonData("Data:Dictionary") | ||
--Used by [[Template:Translate]], see documentation there | |||
-- Get a localized string by the raw key | -- Get a localized string by the raw key |
Revision as of 00:54, 9 October 2024
Overview
See Template:Translate for how/when to use and more documentation. See Data:Dictionary/sources for list of where each translation is used. This may be particularly helpful for reading on the context of the phrase that needs translation.
Functions
translate
Translates a given key using Data:Dictionary.
Parameters
- key - key to translate
- lang_code_override (OPTIONAL, Named parameter) - language code that overrides current language. See supported list at Data:LangCodes.json.
- fallback_str (OPTIONAL, Named parameter) - string to return if key cannot be translated. Defaults to the english translation. Use a blank string or nil for easier catching of missed translations, where the needs translation tooltip will also not be added.
- show_needs_translation (OPTIONAL, Named parameter) - boolean that determines if the <span class="tooltip" title="Script error: The function "translate_embed" does not exist.">[nt] is appended when its unable to be translated. Defaults to 'true'
Example
Example where translation does exist
{{#invoke:Dictionary|translate|Update history|lang_code_override=en}}
Outputs
Update history
Example where translation does not yet exist
{{#invoke:Dictionary|translate|Update history|lang_code_override=es}}
Outputs
Update history
Example where translation does not yet exist but displaying the tooltip is off
{{#invoke:Dictionary|translate|Update history|lang_code_override=es|show_needs_translation=false}}
Outputs
Update history
Example where translation does not yet exist and the fallback is an empty string
{{#invoke:Dictionary|translate|Update history|lang_code_override=es|fallback_str=}}
Outputs
Update history
Example where translation does not yet exist and the fallback is another text
{{#invoke:Dictionary|translate|Update history|lang_code_override=es|fallback_str=My custom fallback text}}
Outputs
Update history
translate_embed
Translates a given key then replaces %s in the string with extra parameters
Parameters
- key - key to translate
- var1 - first variable to embed in the string
- var2 - second
- varN - Nth
Ensure the number of variables passed matches the number of instances of %s
in the translated string.
Examples
Without embedment:
{{#invoke:Dictionary|translate|NeedsTranslationTooltip}}
Outputs
This needs translation to English. To help translate it, visit %s.
With embedment:
{{#invoke:Dictionary|translate_embed |1=NeedsTranslationTooltip |2=Template:Translate }}
Outputs
Script error: The function "translate_embed" does not exist.
Notes
- The parameters do not need to be prefixed with "1=", "2=", etc., they are used in the above example because the 4th parameter contains an equal sign (
=
) in it, where it would be read as a named parameter rather than a ordinal parameter. - Ensure the number of passed variables (other than the key) matches the number of %s used in the translated string.
local p = {}
local dictionary_data = mw.loadJsonData("Data:Dictionary")
--Used by [[Template:Translate]], see documentation there
-- Get a localized string by the raw key
p.translate = function(frame)
local key = frame.args[1]
local lang_code_override = frame.args[2]
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
translation = translations["en"]
if (translation == nil) then
return string.format("lang_code '%s' nor 'en' were found in Data:Dictionary for key '%s'", lang_code, key)
end
end
return 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 string.len(lang_code) ~= 2) then
return 'en'
end
return lang_code
end
return p