Editing Module:DictionaryGive feedback
Jump to navigation
Jump to search
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
| Latest revision | Your text | ||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local dictionary_data = mw.loadJsonData("Data:Dictionary") | local dictionary_data = mw.loadJsonData("Data:Dictionary") | ||
local lang_codes_set = mw.loadJsonData("Data:LangCodes.json") | 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) | 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 | local lang_code = lang_code_override | ||
if | if (lang_code == '' or lang_code == nil) then | ||
lang_code = get_lang_code() | |||
end | end | ||
local translations = dictionary_data[key] | local translations = dictionary_data[key] | ||
if | if (translations == nil) then return string.format("Key '%s' is not in Data:Dictionary", key) end | ||
local translation = translations[lang_code] | local translation = translations[lang_code] | ||
if | if (translation == nil) then | ||
-- Determine the appended tooltip | -- Determine the appended tooltip | ||
local needs_translation_expand = '' | local needs_translation_expand = '' | ||
if key ~= 'MissingValveTranslationTooltip' and key ~= 'NeedsTranslationTooltip' and key ~= 'NotesNeedsTranslation' then | if (key ~= 'MissingValveTranslationTooltip' and key ~= 'NeedsTranslationTooltip' and key ~= 'NotesNeedsTranslation') then | ||
local template_args = {} | local template_args = {} | ||
if key == 'Notes' then | if key == 'Notes' then | ||
template_args[1] = 'NotesNeedsTranslation' | template_args[1] = 'NotesNeedsTranslation' | ||
end | end | ||
needs_translation_expand = mw.getCurrentFrame():expandTemplate{title = 'NeedsTranslationTooltip', args=template_args} | |||
end | end | ||
-- Determine the fallback string to use | -- Determine the fallback string to use | ||
if fallback_str == 'en' or | -- Default to english fallback if none specified or if en specified | ||
if (fallback_str == 'en' or fallback_str == nil) then | |||
translation = translations["en"] | translation = translations["en"] | ||
else | else | ||
translation = fallback_str | translation = fallback_str | ||
-- | -- If english translation is missing, default to the key instead of blank string | ||
if | if (translation == nil) then | ||
translation = key | translation = key | ||
end | end | ||
| Line 90: | Line 60: | ||
-- Add the tooltip | -- Add the tooltip | ||
if translation and translation ~= | if (translation ~= '' and translation ~= nil) then | ||
translation = translation .. needs_translation_expand | translation = translation .. needs_translation_expand | ||
end | end | ||
end | end | ||
if vars ~= nil then | |||
if | for i, v in ipairs(vars) do | ||
if translation ~= nil then | |||
translation = translation:gsub("{{{" .. i .. "}}}", v or "") | |||
translation = | end | ||
end | |||
end | end | ||
end | end | ||
return translation | 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 | end | ||
return p | return p | ||