Module:I18nUtil

local DEFAULTLANG = 'en' local util_map = require('Module:MapUtil') local util_table = require('Module:TableUtil') local util_vars = require('Module:VarsUtil')

local p = {} local h = {}

function p.init(...) -- generally the i18n will be located at a subpage of the parent module local args = util_map.inPlace({...}, function (name)		return require(('Module:%s/i18n'):format(name))	end) if I18N_INTERNAL_TABLE then table.insert(args, 1, I18N_INTERNAL_TABLE) end h.initGlobalFromTables(unpack(args)) end

function p.initAndClear(...) -- generally the i18n will be located at a subpage of the parent module local args = util_map.inPlace({...}, function (name)		return require(('Module:%s/i18n'):format(name))	end) h.initGlobalFromTables(unpack(args)) end

function h.initGlobalFromTables(...) -- initialize global table 'i18n' from file I18N_INTERNAL_TABLE = h.initFromTables(...) end

function h.initFromTables(...) 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_map.safe(args, function(arg)			return arg[lang]		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 ():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 key 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 if not I18N_INTERNAL_TABLE[DEFAULTLANG][key] then return nil end return I18N_INTERNAL_TABLE[DEFAULTLANG][key]:format(...) 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