Leaguepedia | League of Legends Esports Wiki
[checked revision][checked revision]
(+ p.exists() (via Mediawiker ST3))
(init part of export table (via Mediawiker ST3))
Line 18: Line 18:
 
function p.initGlobal(tbl)
 
function p.initGlobal(tbl)
 
-- initialize global table 'i18n' from file
 
-- initialize global table 'i18n' from file
I18N_INTERNAL_TABLE = h.init(tbl)
+
I18N_INTERNAL_TABLE = p.init(tbl)
 
end
 
end
   
function h.init(tbl)
+
function p.init(tbl)
 
tbl.this = {}
 
tbl.this = {}
 
for k, v in pairs(tbl[DEFAULTLANG]) do
 
for k, v in pairs(tbl[DEFAULTLANG]) do

Revision as of 07:56, 1 May 2019

Edit the documentation or categories for this module.


local DEFAULTLANG = 'en'

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)
end

function p.initGlobalFromFile(name)
	-- generally the i18n will be located at a subpage of the parent module
	local tbl = require(('Module:%s/i18n'):format(name))
	p.initGlobal(tbl)
end

function p.initGlobal(tbl)
	-- initialize global table 'i18n' from file
	I18N_INTERNAL_TABLE = p.init(tbl)
end

function p.init(tbl)
	tbl.this = {}
	for k, v in pairs(tbl[DEFAULTLANG]) do
		tbl.this[k] = h.concatTranslations(tbl, k, v)
	end
	return tbl
end

function h.concatTranslations(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')
	return ('{{#switch:{{USERLANGUAGECODE}}%s}}'):format(table.concat(output, ''))
end

function p.print(key, tbl, lang)
	-- return the localized value
	-- optionally can specify an i18n table (this should be done for any util modules that print things directly)
	-- also optionally can specify language but you probably shouldn't do that ever because p.defaultLang exists
	if not key then return nil end
	if not tbl then tbl = I18N_INTERNAL_TABLE end
	local frame = mw.getCurrentFrame()
	if tbl.this[key] then
		return frame:preprocess(tbl.this[key]:format(tbl[DEFAULTLANG][key]))
	else
		return nil
	end
end

function p.printForInclusion(key, tbl, lang)
	-- 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 key then return nil end
	if not tbl then tbl = I18N_INTERNAL_TABLE end
	local frame = mw.getCurrentFrame()
	if tbl.this[key] then
		return tbl.this[key]:format(tbl[DEFAULTLANG][key])
	else
		return nil
	end
end

function p.printWithFallback(key, tbl, lang)
	local frame = mw.getCurrentFrame()
	if not key then return nil end
	if not tbl then tbl = I18N_INTERNAL_TABLE end
	if tbl.this[key] then
		return frame:preprocess(tbl.this[key]:format(tbl[DEFAULTLANG][key] or key))
	else
		return key
	end
end

function p.default(key, tbl)
	-- in the event we're storing a string in cargo it should still go through i18n but we'd just force default language
	return p.print(key, tbl, DEFAULTLANG)
end

function p.defaultWithFallback(key, tbl)
	return p.printWithFallback(key, tbl, DEFAULTLANG)
end

function p.exists()
	if I18N_INTERNAL_TABLE then return true end
end

return p