Editing
Module:I18nUtil
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
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 ('{{#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 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
Summary:
Please note that all contributions to Porn Base Central may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Porn Base Central:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Preview page with this template
Template used on this page:
Module:I18nUtil/doc
(
view source
)
Navigation menu
Personal tools
English
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Module
Discussion
English
Views
Read
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Upload file
Special pages
Page information