Edit the documentation or categories for this module.
local DEFAULTLANG = 'en'
local util_table = require('Module:TableUtil')
local util_vars = require('Module:VarsUtil')
local p = {}
local h = {}
function p.initFromFile(name)
-- generally the i18n will be located at a subpage of the parent module
local tbl = require(('Module:%s/i18n'):format(name))
p.init(tbl)
return tbl
end
function p.initGlobalFromFile(...)
-- generally the i18n will be located at a subpage of the parent module
local args = util_table.mapInPlace({...}, function (name)
return require(('Module:%s/i18n'):format(name))
end)
p.initGlobal(unpack(args))
end
function p.initGlobal(...)
-- initialize global table 'i18n' from file
I18N_INTERNAL_TABLE = p.init(...)
end
function p.init(...)
local args = {...}
local tbl = h.combineInputsIntoOneTable(args)
tbl.this = {}
for k, v in pairs(tbl[DEFAULTLANG]) do
tbl.this[k] = h.getTranslationList(tbl, k, v)
end
return tbl
end
function h.combineInputsIntoOneTable(args)
local ret = {}
for lang, _ in pairs(h.getLangListFromArgs(args)) do
ret[lang] = util_table.merge(unpack(util_table.mapSafe(args, function(arg)
return arg[lang] or { TournamentResults = 'kitten lol' }
end)))
end
return ret
end
function h.getLangListFromArgs(args)
local ret = {}
for _, arg in ipairs(args) do
for lang, _ in pairs(arg) do
ret[lang] = true
end
end
return ret
end
function h.getTranslationList(tbl, k, v)
local output = {}
for lang, langTable in pairs(tbl) do
output[#output+1] = ('|%s=%s'):format(lang, langTable[k] or v)
end
output[#output+1] = ('|#default=%s'):format(tbl[DEFAULTLANG][k])
return output
end
function h.concatForPrint(tbl, ...)
local temp = mw.clone(tbl)
for i, v in ipairs(temp) do
temp[i] = v:format(...)
end
return ('{{#switch:{{USERLANGUAGECODE}}%s}}'):format(table.concat(temp, ''))
end
function h.checkEverythingExists(key)
return key and I18N_INTERNAL_TABLE and I18N_INTERNAL_TABLE.this[key]
end
function h.preprocess(output)
return mw.getCurrentFrame():preprocess(output)
end
function p.print(key, ...)
-- return the localized value, forces you to use global table
if not h.checkEverythingExists(key) then return nil end
return h.preprocess(h.concatForPrint(I18N_INTERNAL_TABLE.this[key], ...))
end
function p.printForInclusion(key, ...)
-- same as print but doesn't preprocess the output, so it will be displayed in raw wikitext on the page
-- then when the page is transcluded to raw text via a python script the switch will be included properly
if not h.checkEverythingExists(key) then return nil end
return h.concatForPrint(I18N_INTERNAL_TABLE.this[key], ...)
end
function p.printWithFallback(key, ...)
if not h.checkEverythingExists(key) then return nil end
return h.preprocess(h.concatForPrint(I18N_INTERNAL_TABLE.this[key], ...))
end
function p.default(key)
-- in the event we're storing a string in cargo it should still go through i18n but we'd just force default language
if not key then return nil end
if not I18N_INTERNAL_TABLE then return nil end
return I18N_INTERNAL_TABLE[DEFAULTLANG][key]
end
function p.defaultWithFallback(key)
if not key then return nil end
if not I18N_INTERNAL_TABLE then return nil end
return I18N_INTERNAL_TABLE[DEFAULTLANG][key] or key
end
function p.exists()
if I18N_INTERNAL_TABLE then return true end
end
return p