Jump to content

Module:Dictionary: Difference between revisions

From Deadlock Wiki
Sur (talk | contribs)
m bugfix for previous
Blanked the page
Tag: Blanking
Line 1: Line 1:
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

Revision as of 11:21, 26 January 2025

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 module returned a nil value. It is supposed to return an export table.">[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

Script error: The module returned a nil value. It is supposed to return an export table.


Example where translation does not yet exist
{{#invoke:Dictionary|translate|Update history|lang_code_override=es}}

Outputs

Script error: The module returned a nil value. It is supposed to return an export table.


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

Script error: The module returned a nil value. It is supposed to return an export table.

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

Script error: The module returned a nil value. It is supposed to return an export table.


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

Script error: The module returned a nil value. It is supposed to return an export table.

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

Script error: The module returned a nil value. It is supposed to return an export table.

With embedment:

{{#invoke:Dictionary|translate_embed
|1=NeedsTranslationTooltip
|2=Template:Translate
}}

Outputs

Script error: The module returned a nil value. It is supposed to return an export table.

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.