Module:Translation
From Seul En Groupe
More actions
Documentation for this module may be created at Module:Translation/doc
local Args = require('Module:Args')
local p = {}
local TranslationCache = nil -- caches translations from getPageTable()
-- language codes
local languages = {
af = true, -- Afrikaans
ca = true, -- Catalan
cs = true, -- Czech
de = true, -- German
es = true, -- Spanish
fr = true, -- French
hu = true, -- Hungarian
it = true, -- Italian
ja = true, -- Japanese
ko = true, -- Korean
nl = true, -- Dutch
no = true, -- Norwegian
pl = true, -- Polish
pt = true, -- Portuguese
["pt-br"] = true, -- Brazilian Portuguese
ru = true, -- Russian
th = true, -- Thai
tr = true, -- Turkish
uk = true, -- Ukrainian
vi = true, -- Vietnamese
["zh-hans"] = true, -- Chinese (Simplified)
["zh-hant"] = true -- Chinese (Traditional)
}
-- returns the languages table
function p.getLanguages()
return languages
end
-- checks if a language code exists
function p.langExist(frame)
local args = Args.getArgs(frame)
local lang = Args.get(args[1])
return (lang and languages[string.lower(lang)]) and 'true' or ''
end
-- lists all the languages with a configurable output
function p.listLanguages(frame)
local args = frame and frame.args or {}
local wrap = args.wrap
local mode = args.mode or 'sep'
local sep = args.sep or ', '
local f = (frame and frame.preprocess) and frame or mw.getCurrentFrame()
local list = {}
for code in pairs(languages) do
local item
if wrap and wrap ~= '' then
item = wrap
item = item:gsub("%%s", code)
local name = f:preprocess("{{#language:" .. code .. "}}")
item = item:gsub("%%lang%%", name)
else
item = code
end
table.insert(list, item)
end
table.sort(list)
if mode == 'list' then
return '* ' .. table.concat(list, '\n* ')
else
return table.concat(list, sep)
end
end
-- returns the subpage of the current page if it's a valid language code
-- core logic
function p._getLangSubpage(lang, slash)
local code = string.lower(lang or '')
if not languages[code] then
return ''
elseif slash == false then
return lang
else
return '/' .. lang
end
end
-- template wrapper
function p.getLangSubpage(frame)
local args = Args.getArgs(frame)
local raw1 = args[1]
-- legacy noslash for backwards compatibility
local isLegacyNoSlash = (type(raw1) == 'string' and raw1:lower() == 'noslash')
-- determine language
local lang = isLegacyNoSlash and mw.title.getCurrentTitle().subpageText or Args.get(raw1, mw.title.getCurrentTitle().subpageText)
-- determine slash
local slash
if isLegacyNoSlash then
slash = false
elseif args.slash ~= nil then
slash = args.slash ~= false -- default true unless explicitly "false"
else
slash = true
end
return p._getLangSubpage(lang, slash)
end
-- generates a link, linking to the appropriate language subpage
-- core logic
function p._makeLink(page, label, lang, section)
local code = string.lower(lang or '')
local suffix = languages[code] and ('/' .. lang) or ''
local sectionPart = section and ('#' .. section) or ''
local link = '[[' .. page .. suffix .. sectionPart
if label then link = link .. '|' .. label end
return link .. ']]'
end
-- template wrapper
function p.makeLink(frame)
local args = Args.getArgs(frame)
local page = Args.get(args[1], '')
local label = Args.get(args[2])
local lang = Args.get(args[3])
local section = Args.get(args["#"])
if not lang then
local subpage = mw.title.getCurrentTitle().subpageText
if languages[string.lower(subpage or '')] then
lang = subpage
end
end
return p._makeLink(page, label, lang, section)
end
-- get a translation
-- NOTE: WIP DO NOT USE
function p.getTranslation(frame)
local args = Args.getArgs(frame)
local key = Args.get(args[1])
local pageName = Args.get(args[2])
if not key then
return 'Missing key'
end
local tableData = p.getPageTable(pageName)
if tableData.error then
return tableData.error
end
return tableData[key] or 'Missing translation: ' .. key
end
-- get a page table in json format wrapped in: <syntaxhighlight lang="json"> ... </syntaxhighlight>
-- NOTE: WIP DO NOT USE
function p.getPageTable(pageName)
if TranslationCache then
return TranslationCache
end
pageName = pageName or 'User:Vaileasys/sandbox' --TODO: add a proper fallback page
local title = mw.title.new(pageName)
if not title then
return { error = 'Invalid page name.' }
end
local content = title:getContent()
if not content then
return { error = 'No content found or page does not exist.' }
end
-- matches: <syntaxhighlight lang="json"> ... </syntaxhighlight>
local raw = content:match('<syntaxhighlight%s+lang="json"%s*>(.-)</syntaxhighlight>')
if not raw then
return { error = 'No <syntaxhighlight lang="json"> block found.' }
end
-- remove trailing commas
raw = raw:gsub(',%s*}', '}')
-- parse json format
local parsed = mw.text.jsonDecode(raw)
if not parsed or type(parsed) ~= 'table' then
return { error = 'Failed to decode JSON.' }
end
TranslationCache = parsed
return parsed
end
return p