Module:I18n: Difference between revisions
m Protected "Module:I18n" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) |
No edit summary |
||
| Line 1: | Line 1: | ||
--- I18n library for message storage in Lua | --- I18n library for message storage in Lua datastore collections. | ||
-- 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 | ||
| Line 6: | Line 6: | ||
-- | -- | ||
-- @module i18n | -- @module i18n | ||
-- @version 1. | -- @version 1.5.0 | ||
-- @require Module:Entrypoint | -- @require Module:Entrypoint | ||
-- @require Module:Fallbacklist | -- @require Module:Fallbacklist | ||
| Line 21: | Line 21: | ||
-- Module variables & dependencies. | -- Module variables & dependencies. | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
local fallbacks = require(' | local fallbacks = require('Dev:Fallbacklist') | ||
local entrypoint = require(' | local entrypoint = require('Dev:Entrypoint') | ||
local uselang | local uselang | ||
| Line 73: | Line 73: | ||
Data.__index = Data | Data.__index = Data | ||
--- | --- Gets data message 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 82: | ||
-- 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 133: | Line 134: | ||
local lang = self.tempLang or self.defaultLang | local lang = self.tempLang or self.defaultLang | ||
self.tempLang = nil | self.tempLang = nil | ||
-- Language converter handling | |||
-- Types: 'languageConverterMarkup', 'pageSourceCodeLanguage', | |||
-- 'siteLanguage', 'userLanguage' | |||
-- Note: Currently cannot handle page view language without using | |||
-- 'languageConverterMarkup', should use 'userLanguage' in most cases if | |||
-- not able to use 'languageConverterMarkup'. | |||
local languageConversionType = self.tempLanguageConversionType or | |||
'languageConverterMarkup' | |||
self.tempLangConversionType = 'languageConverterMarkup' | |||
-- 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 msg = (messages[lang] or {})[key] | local msg = nil | ||
-- Language converter support | |||
if lang == 'zh' and | |||
languageConversionType ~= 'pageSourceCodeLanguage' and | |||
languageConversionType ~= 'siteLanguage' and | |||
-- Lua error: too many expensive function calls. | |||
-- ( | |||
-- mw.title.getCurrentTitle().pageLang:toBcp47Code() == 'zh' or | |||
-- ( | |||
-- -- Module namespace | |||
-- mw.title.getCurrentTitle():inNamespace( 828 ) and | |||
mw.language.getContentLanguage():toBcp47Code() == 'zh' | |||
-- ) | |||
-- ) | |||
then | |||
local userLang = ( i18n.getLang() or self.defaultLang ) | |||
local zh_hani = (messages['zh'] or {})[key] | |||
local zh_hans = (messages['zh-hans'] or {})[key] | |||
local zh_hant = (messages['zh-hant'] or {})[key] | |||
local zh_hant_hk = (messages['zh-hk'] or {})[key] | |||
if | |||
languageConversionType == 'userLanguage' | |||
then | |||
if userLang == 'zh' then | |||
msg = zh_hani or zh_hans or zh_hant or zh_hant_hk | |||
elseif | |||
userLang == 'zh-hans' or | |||
userLang == 'zh-cn' or | |||
userLang == 'zh-my' or | |||
userLang == 'zh-sg' | |||
then | |||
msg = zh_hans or zh_hani or zh_hant or zh_hant_hk | |||
elseif | |||
userLang == 'zh-hant' or | |||
userLang == 'zh-tw' | |||
then | |||
msg = zh_hant or zh_hant_hk or zh_hani or zh_hans | |||
elseif | |||
userLang == 'zh-hk' or | |||
userLang == 'zh-mo' | |||
then | |||
msg = zh_hant_hk or zh_hant or zh_hani or zh_hans | |||
end | |||
elseif zh_hans ~= nil then | |||
if zh_hant ~= nil then | |||
msg = '-{' .. | |||
'zh-hans:' .. zh_hans .. ';' .. | |||
' zh-hant:' .. zh_hant .. ';' | |||
if zh_hant_hk ~= nil then | |||
msg = msg .. | |||
' zh-hk:' .. zh_hant_hk .. ';' | |||
end | |||
msg = msg .. '}-' | |||
elseif zh_hant_hk ~= nil then | |||
msg = '-{' .. | |||
'zh-hans:' .. zh_hans .. ';' .. | |||
' zh-hk:' .. zh_hant_hk .. ';' .. | |||
'}-' | |||
else | |||
msg = zh_hans | |||
end | |||
elseif zh_hant ~= nil then | |||
if zh_hant_hk ~= nil then | |||
msg = '-{' .. | |||
'zh-hant:' .. zh_hant .. ';' .. | |||
' zh-hk:' .. zh_hant_hk .. ';' .. | |||
'}-' | |||
else | |||
msg = zh_hant | |||
end | |||
elseif zh_hant_hk ~= nil then | |||
msg = zh_hant_hk | |||
end | |||
end | |||
if msg == nil then | |||
msg = (messages[lang] or {})[key] | |||
end | |||
-- Fallback support (experimental). | -- Fallback support (experimental). | ||
for _, l in ipairs((fallbacks[lang] or {})) do | for _, l in ipairs((fallbacks[lang] or {})) do | ||
| Line 152: | Line 242: | ||
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 | and frame:preprocess(msg) | ||
or mw.text.trim(msg) | or mw.text.trim(msg, '\r\n\f\v') | ||
end | end | ||
end | end | ||
| Line 159: | Line 249: | ||
end | end | ||
--- | --- Gets localised template parameter from a datastore. | ||
-- 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 199: | Line 289: | ||
end | end | ||
--- | --- Sets temporary source 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 219: | Line 309: | ||
end | end | ||
--- | --- Gets data default language. | ||
-- @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 226: | Line 316: | ||
end | end | ||
--- | --- Sets data language to `wgUserLanguage`. | ||
-- @function Data:useUserLang | -- @function Data:useUserLang | ||
-- @return {Data} Datastore instance. | -- @return {Data} Datastore instance. | ||
| Line 236: | Line 326: | ||
end | end | ||
--- | --- Sets data language 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 = i18n.getContentLang() | ||
return self | return self | ||
end | end | ||
--- | --- Sets data language to specificed language. | ||
-- @function Data:useLang | -- @function Data:useLang | ||
-- @param {string} code Language code to use. | -- @param {string} code Language code to use. | ||
| Line 255: | Line 345: | ||
end | end | ||
--- | --- Sets temporary data language to `wgUserLanguage`. | ||
-- The | -- The data language reverts to the default language in the next | ||
-- @{Data:msg} call. | -- @{Data:msg} call. | ||
-- @function Data:inUserLang | -- @function Data:inUserLang | ||
| Line 265: | Line 355: | ||
end | end | ||
--- | --- Sets temporary data language 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 = i18n.getContentLang() | ||
return self | return self | ||
end | end | ||
--- | --- Sets temporary data language 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 283: | Line 373: | ||
and code | and code | ||
or self.tempLang | or self.tempLang | ||
return self | |||
end | |||
--- Sets temporary data language to a specificed language conversion resolution. | |||
-- Only affects the next @{Data:msg} call. | |||
-- @function Data:useLanguageConversionType | |||
-- @param {string} convType Language conversion type to use. | |||
-- @return {Data} Datastore instance. | |||
function Data:useLanguageConversionType(convType) | |||
self.tempLanguageConversionType = convType | |||
return self | return self | ||
end | end | ||
| Line 288: | Line 388: | ||
-- Package functions. | -- Package functions. | ||
--- | --- Gets a localized message by data module 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 data collection's message. | ||
-- @function i18n.getMsg | -- @function i18n.getMsg | ||
-- @param {table} frame Frame table from invocation. | -- @param {table} frame Frame table from invocation. | ||
| Line 327: | Line 427: | ||
end | end | ||
--- I18n message | --- I18n data loader for message collections. | ||
-- @function i18n.loadMessages | -- @function i18n.loadMessages | ||
-- @param {string} ... ROOTPAGENAME/path for target i18n | -- @param {string} ... ROOTPAGENAME/path for target i18n | ||
| Line 354: | Line 454: | ||
source = mw.ustring.find(source, ':') | source = mw.ustring.find(source, ':') | ||
and source | and source | ||
or ' | or 'Dev:' .. source .. '/i18n' | ||
ds._messages[i] = mw.loadData(source) | ds._messages[i] = mw.loadData(source) | ||
end | end | ||
| Line 368: | Line 468: | ||
end | end | ||
--- | --- Gets the language code of the wiki or page contents. | ||
-- @function i18n.getContentLang | |||
-- @usage {{i18n|getContentLang}} | |||
-- @return {string} Content language code. | |||
function i18n.getContentLang() | |||
local code = mw.language.getContentLanguage():getCode() | |||
local subPage = title.subpageText | |||
-- Subpage language test. | |||
if title.isSubpage and _i18n.isValidCode(subPage) then | |||
code = _i18n.isValidCode(subPage) and subPage or code | |||
end | |||
return code | |||
end | |||
--- Gets the language code of the content. | |||
-- 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 377: | Line 493: | ||
local parentFrame = frame.getParent and frame:getParent() or {} | local parentFrame = frame.getParent and frame:getParent() or {} | ||
local code = | local code = i18n.getContentLang() | ||
-- Language argument test. | -- Language argument test. | ||
| Line 386: | Line 501: | ||
if _i18n.isValidCode(langOverride) then | if _i18n.isValidCode(langOverride) then | ||
code = langOverride | code = langOverride | ||
-- User language test. | -- User language test. | ||