Module:I18n: Difference between revisions
No edit summary |
add useLanguageConversionType from https://dev.fandom.com/wiki/Module:I18n line 378-386 |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
--- I18n library for message storage in Lua | --- I18n library for message storage in Lua datastores. | ||
-- The module is designed to enable message separation from modules & | -- The module is designed to enable message separation from modules & | ||
-- templates. It has support for handling language fallbacks. This | -- templates. It has support for handling language fallbacks. This | ||
-- module is a Lua port of | -- module is a Lua port of I18n-js and i18n modules that can be loaded | ||
-- by it are editable through | -- by it are editable through I18nEdit. | ||
-- | -- | ||
-- @module i18n | -- @module i18n | ||
-- @version 1. | -- @version 1.4.0 | ||
-- @require Module:Entrypoint | -- @require Module:Entrypoint | ||
-- @require Module:Fallbacklist | -- @require Module:Fallbacklist | ||
| Line 13: | Line 13: | ||
-- @attribution [[User:Cqm|Cqm]] | -- @attribution [[User:Cqm|Cqm]] | ||
-- @release stable | -- @release stable | ||
-- <nowiki> | -- <nowiki> | ||
local i18n, _i18n = {}, {} | local i18n, _i18n = {}, {} | ||
| Line 21: | Line 18: | ||
-- Module variables & dependencies. | -- Module variables & dependencies. | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
local fallbacks = require(' | local fallbacks = require('Module:Fallbacklist') | ||
local entrypoint = require(' | local entrypoint = require('Module:Entrypoint') | ||
local uselang | local uselang | ||
| Line 73: | Line 70: | ||
Data.__index = Data | Data.__index = Data | ||
--- | --- Datastore message getter utility. | ||
-- This method returns localized messages from the datastore corresponding | -- This method returns localized messages from the datastore corresponding | ||
-- to a `key`. These messages may have `$n` parameters, which can be | -- to a `key`. These messages may have `$n` parameters, which can be | ||
| Line 82: | Line 79: | ||
-- verbosity; it can be used to select message language & source(s). | -- verbosity; it can be used to select message language & source(s). | ||
-- @function Data:msg | -- @function Data:msg | ||
-- @usage | -- @usage | ||
-- | -- | ||
| Line 134: | Line 130: | ||
local lang = self.tempLang or self.defaultLang | local lang = self.tempLang or self.defaultLang | ||
self.tempLang = nil | self.tempLang = nil | ||
-- Message fetching. | -- Message fetching. | ||
local msg | local msg | ||
for i, messages in ipairs(self._messages) do | for i, messages in ipairs(self._messages) do | ||
-- Message data. | -- Message data. | ||
local | local msg = (messages[lang] or {})[key] | ||
-- Fallback support (experimental). | -- Fallback support (experimental). | ||
for _, l in ipairs((fallbacks[lang] or {})) do | for _, l in ipairs((fallbacks[lang] or {})) do | ||
| Line 242: | Line 149: | ||
if msg and source_i[i] and lang ~= 'qqx' then | if msg and source_i[i] and lang ~= 'qqx' then | ||
return frame and _i18n.isWikitext(msg) | return frame and _i18n.isWikitext(msg) | ||
and frame:preprocess(msg) | and frame:preprocess(mw.text.trim(msg)) | ||
or mw.text.trim(msg | or mw.text.trim(msg) | ||
end | end | ||
end | end | ||
| Line 249: | Line 156: | ||
end | end | ||
--- | --- Datastore template parameter getter utility. | ||
-- This method, given a table of arguments, tries to find a parameter's | -- This method, given a table of arguments, tries to find a parameter's | ||
-- localized name in the datastore and returns its value, or nil if | -- localized name in the datastore and returns its value, or nil if | ||
| Line 289: | Line 196: | ||
end | end | ||
--- | --- Datastore temporary source setter to a specificed subset of datastores. | ||
-- By default, messages are fetched from the datastore in the same | -- By default, messages are fetched from the datastore in the same | ||
-- order of priority as `i18n.loadMessages`. | -- order of priority as `i18n.loadMessages`. | ||
| Line 309: | Line 216: | ||
end | end | ||
--- | --- Datastore default language getter. | ||
-- @function Data:getLang | -- @function Data:getLang | ||
-- @return {string} Default language to serve datastore messages in. | -- @return {string} Default language to serve datastore messages in. | ||
| Line 316: | Line 223: | ||
end | end | ||
--- | --- Datastore language setter to `wgUserLanguage`. | ||
-- @function Data:useUserLang | -- @function Data:useUserLang | ||
-- @return {Data} Datastore instance. | -- @return {Data} Datastore instance. | ||
| Line 326: | Line 233: | ||
end | end | ||
--- | --- Datastore language setter to `wgContentLanguage`. | ||
-- @function Data:useContentLang | -- @function Data:useContentLang | ||
-- @return {Data} Datastore instance. | -- @return {Data} Datastore instance. | ||
function Data:useContentLang() | function Data:useContentLang() | ||
self.defaultLang = | self.defaultLang = mw.language.getContentLanguage():getCode() | ||
return self | return self | ||
end | end | ||
--- | --- Datastore language setter to specificed language. | ||
-- @function Data:useLang | -- @function Data:useLang | ||
-- @param {string} code Language code to use. | -- @param {string} code Language code to use. | ||
| Line 345: | Line 252: | ||
end | end | ||
--- | --- Temporary datastore language setter to `wgUserLanguage`. | ||
-- The | -- The datastore language reverts to the default language in the next | ||
-- @{Data:msg} call. | -- @{Data:msg} call. | ||
-- @function Data:inUserLang | -- @function Data:inUserLang | ||
| Line 355: | Line 262: | ||
end | end | ||
--- | --- Temporary datastore language setter to `wgContentLanguage`. | ||
-- Only affects the next @{Data:msg} call. | -- Only affects the next @{Data:msg} call. | ||
-- @function Data:inContentLang | -- @function Data:inContentLang | ||
-- @return {Data} Datastore instance. | -- @return {Data} Datastore instance. | ||
function Data:inContentLang() | function Data:inContentLang() | ||
self.tempLang = | self.tempLang = mw.language.getContentLanguage():getCode() | ||
return self | return self | ||
end | end | ||
--- | --- Temporary datastore language setter to a specificed language. | ||
-- Only affects the next @{Data:msg} call. | -- Only affects the next @{Data:msg} call. | ||
-- @function Data:inLang | -- @function Data:inLang | ||
| Line 388: | Line 295: | ||
-- Package functions. | -- Package functions. | ||
--- | --- Localized message getter by key. | ||
-- Can be used to fetch messages in a specific language code through `uselang` | -- Can be used to fetch messages in a specific language code through `uselang` | ||
-- parameter. Extra numbered parameters can be supplied for substitution into | -- parameter. Extra numbered parameters can be supplied for substitution into | ||
-- the | -- the datastore message. | ||
-- @function i18n.getMsg | -- @function i18n.getMsg | ||
-- @param {table} frame Frame table from invocation. | -- @param {table} frame Frame table from invocation. | ||
| Line 427: | Line 334: | ||
end | end | ||
--- I18n | --- I18n message datastore loader. | ||
-- @function i18n.loadMessages | -- @function i18n.loadMessages | ||
-- @param {string} ... ROOTPAGENAME/path for target i18n | -- @param {string} ... ROOTPAGENAME/path for target i18n | ||
| Line 454: | Line 361: | ||
source = mw.ustring.find(source, ':') | source = mw.ustring.find(source, ':') | ||
and source | and source | ||
or ' | or 'Module:' .. source .. '/i18n' -- Dev: -> Module: for on-wiki use | ||
ds._messages[i] = mw.loadData(source) | ds._messages[i] = mw.loadData(source) | ||
end | end | ||
| Line 468: | Line 375: | ||
end | end | ||
--- | --- Language code getter. | ||
-- Can validate a template's language code through `uselang` parameter. | -- Can validate a template's language code through `uselang` parameter. | ||
-- @function i18n.getLang | -- @function i18n.getLang | ||
| Line 493: | Line 384: | ||
local parentFrame = frame.getParent and frame:getParent() or {} | local parentFrame = frame.getParent and frame:getParent() or {} | ||
local code = | local code = mw.language.getContentLanguage():getCode() | ||
local subPage = title.subpageText | |||
-- Language argument test. | -- Language argument test. | ||
| Line 501: | Line 393: | ||
if _i18n.isValidCode(langOverride) then | if _i18n.isValidCode(langOverride) then | ||
code = langOverride | code = langOverride | ||
-- Subpage language test. | |||
elseif title.isSubpage and _i18n.isValidCode(subPage) then | |||
code = _i18n.isValidCode(subPage) and subPage or code | |||
-- User language test. | -- User language test. | ||