Module:Test

-- l10n text, need to be translated. local l10n = { ['en'] = { ['icon_link_d'] = 'Desktop Version history', ['icon_text_d'] = 'Desktop Version', ['icon_link_c'] = 'Console version', ['icon_text_c'] = 'Console Version', ['icon_link_o'] = 'Old-gen console version', ['icon_text_o'] = 'Old-gen console version', ['icon_link_m'] = 'Mobile Version', ['icon_text_m'] = 'Mobile Version', ['icon_link_3'] = '3DS version', ['icon_text_3'] = '3DS Version', ['icon_link_j'] = 'Old-gen console version#Japanese Release', ['icon_text_j'] = 'Japanese Console version', },	['zh'] = { ['icon_link_d'] = '电脑版版本历史', ['icon_text_d'] = '电脑版', ['icon_link_c'] = '主机版', ['icon_text_c'] = '主机版', ['icon_link_o'] = '前代主机版', ['icon_text_o'] = '前代主机版', ['icon_link_m'] = '移动版', ['icon_text_m'] = '移动版', ['icon_link_3'] = '3DS版', ['icon_text_3'] = '3DS版', ['icon_link_j'] = '前代主机版#日本发布版', ['icon_text_j'] = '日本主机版', }, }

-- l10n function to get plural form, it may be different for each language. If function for target language does not exist, the EN version is used. -- about the function itself: -- return plural form, nil if no pluralism processing occurs. -- _arg1 is the original text(without auto translation), _arg2 is custom text(without auto translation) or plural postfix, may be s/es/ies/ves (for english). -- the retruned result will be auto translated. local plural = { -- for English (en) ['en'] = function(_arg1, _arg2) -- intuitive pluralism -- The comparisons are performed in order, so putting the more common ones in front gives the best performance. if _arg2 == 's' then local suffix1 = _arg1:sub(-1) -- cache for better performance. local suffix2 = _arg1:sub(-2) -- cache for better performance. if suffix1 == 'y' then if suffix2 == 'ay' or suffix2 == 'ey' or suffix2 == 'iy' or suffix2 == 'oy' or suffix2 == 'uy' then return _arg1 .. 's'                -- eg. ray->rays else return _arg1:sub(1, -2) .. 'ies'   -- eg. firefly->fireflies end end if suffix2 == 'fe' and string.lower(_arg1) ~= 'safe' then return _arg1:sub(1, -3) .. 'ves'       -- eg. knife->knives end if suffix1 == 'f' and suffix2 ~= 'ff' then return _arg1:sub(1, -2) .. 'ves'       -- eg. wolf->wolves, leaf->leaves, but buff->buffs else return _arg1 .. 's'			end end

if _arg2 == 'es' then return _arg1 .. 'es' end

if _arg2 == 'ies' then return _arg1:sub(1, -2) .. 'ies' end

if _arg2 == 'ves' then if _arg1:sub(-2) == 'fe' then return _arg1:sub(1, -3) .. 'ves'   --replace last 2 letters, e.g. knife->knives end if _arg1:sub(-1) == 'f' or _arg1:sub(-2) ~= 'ff' then return _arg1:sub(1, -2) .. 'ves'   --replace last letter, e.g. wolf-wolves, leaf->leaves, but buff->buffs end end end,

-- for Chinese (zh) ['zh'] = function(_arg1, _arg2) -- there is no plural form in chinese. if _arg2 == 's' or _arg2 == 'es' or _arg2 == 'ies' or _arg2 == 'ves' then return _arg1 end return nil end,

-- for German (de) !! may need to be improved !! ['de'] = function(_arg1, _arg2) if _arg2 == 's' or _arg2 == 'es' or _arg2 == 'ies' or _arg2 == 'ves' then return _arg1 end return nil end,

-- for Italian (it) !! may need to be improved !! ['it'] = function(_arg1, _arg2) if _arg2 == 's' or _arg2 == 'es' or _arg2 == 'ies' or _arg2 == 'ves' then return _arg1 end return nil end,

}

--- The following is not related to l10n. --

local bit = require( 'bit32' ) local bitTest = bit.btest local trim = mw.text.trim local mwlang = mw.getContentLanguage

-- pagename => bit set. d = 1, c = 2, o = 4, m = 8, 3 = 16, j = 32 local exclusive_info = mw.loadData( 'Module:test/db' )

local currentFrame -- cache -- helper function local getArg = function(key) local value = trim(currentFrame.args[key] or '') return (value ~= '') and value or nil end

local function yesno(parameter, base) if parameter then if parameter == 'y' or parameter == 'yes' then return 'y'		end if parameter == 'n' or parameter == 'no' then return nil end end return base end

-- get display text. return value need to be translated. local function getText(_arg1, _arg2) if not _arg2 then return _arg1 end local plural_function = plural[lang] or plural['en'] return plural_function(_arg1, _arg2) or _arg2 end

-- output eicons: local eicons = function (vinfo, _small, _link, lang) -- vinfo: bit set of versions. local link = '' local space = false local size local result = '' local l10n = l10n[lang] or l10n['en'] if bitTest(vinfo, 1) then result = ''		space = true end if bitTest(vinfo, 2) then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if bitTest(vinfo, 4) then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if bitTest(vinfo, 8) then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if bitTest(vinfo, 16) then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if bitTest(vinfo, 32) then result = result .. (space and '&thinsp;' or '') .. ''	end return mw.text.tag('span',{class='eico'}, result) end

-- return int vinfo, nil if cache missed local getInfo = function(page, invert) page = mwlang:ucfirst(string.gsub(string.gsub(page, '#.*', ''), '_', ' ')) -- standardize pagename. local vinfo = exclusive_info[page] if not vinfo then return nil end if invert then -- invert d/c/o/m/3, and set j=false -- if only j and invert = y, vinfo will be 0. vinfo = bit.bxor(bit.band(vinfo, 31), 31) end return vinfo end

-- return int, 0 for no exclusive info. local getExclusiveInfo = function(page, invert) local vinfo = page and getInfo(page, invert) or 0 -- 0 when not page or not getInfo, else getInfo -- override return ( yesno(getArg('dsk'), bitTest(vinfo, 1)) and  1 or 0 ) + ( yesno(getArg('cns'), bitTest(vinfo, 2)) and  2 or 0 ) + ( yesno(getArg('old'), bitTest(vinfo, 4)) and  4 or 0 ) + ( yesno(getArg('mbl'), bitTest(vinfo, 8)) and  8 or 0 ) + ( yesno(getArg('3ds'), bitTest(vinfo, 16)) and 16 or 0 ) + ( yesno(getArg('jas'), bitTest(vinfo, 32)) and 32 or 0 ) end

-

return { -- for module:item and other modules. return value need to be translated. getText = getText,

-- for other module such as Module:item, simple eicons: simpleEicons = function(page, lang, small, link) local vinfo = exclusive_info[mwlang:ucfirst(string.gsub(string.gsub(page, '#.*', ''), '_', ' '))] if not vinfo then return '' end return eicons(vinfo, small, link, lang) end,

-- for template, get all exclusive info, setting in dplvars. -- parameters: $1 = pagename, $lang = language, $invert = invert ? getInfo = function(frame) currentFrame = frame -- cache local page = getArg(1) if not page then frame:callParserFunction{ name = '#dplvar:set', args = { 'ex_d', , 'ex_c', , 'ex_o', , 'ex_m', , 'ex_3', , 'ex_j', , 'ex_cached', '' } }			return end local vinfo = getInfo(page, getArg('invert')) if not vinfo then frame:callParserFunction{ name = '#dplvar:set', args = { 'ex_d', , 'ex_c', , 'ex_o', , 'ex_m', , 'ex_3', , 'ex_j', , 'ex_cached', '' } }			return end frame:callParserFunction{ name = '#dplvar:set', args = { 'ex_d', bitTest(vinfo, 1) and 'y' or '', 'ex_c', bitTest(vinfo, 2) and 'y' or '', 'ex_o', bitTest(vinfo, 4) and 'y' or '', 'ex_m', bitTest(vinfo, 8) and 'y' or '', 'ex_3', bitTest(vinfo, 16) and 'y' or '', 'ex_j', bitTest(vinfo, 32) and 'y' or '', 'ex_cached', 'y'		} } end,

-- for eicons = function(frame) currentFrame = frame -- cache local vinfo = getExclusiveInfo(getArg(1), getArg('invert')) if vinfo == 0 then return '' end return eicons(vinfo, getArg('small'), getArg('link'), getArg('lang')) end,

-- for eil = function(frame) currentFrame = frame -- cache local lang = getArg('lang') local link = getArg('link') local text = frame:expandTemplate{ title = 'tr', args = { getText(getArg(1), getArg(2)), lang=lang} } -- auto translate if not getArg('nolink') then if link == text then text = ..text.. else text = ..text.. end end local vinfo = getExclusiveInfo(getArg('epage') or link, getArg('invert')) return mw.text.tag('span',{class='eil'}, text..(vinfo and eicons(vinfo, yesno(getArg('small'), true), getArg('iconlink'), lang) or '') ) end,

}