Module:I18n: Difference between revisions

No edit summary
Kuhlau (talk | contribs)
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 datastore collections.
--- 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 [[I18n-js]] and i18n modules that can be loaded
--  module is a Lua port of I18n-js and i18n modules that can be loaded
--  by it are editable through [[I18nEdit]].
--  by it are editable through I18nEdit.
--   
--   
--  @module        i18n
--  @module        i18n
--  @version        1.5.0
--  @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
--  @see            [[I18n|I18n guide]]
--  @see            [[I18n-js]]
--  @see            [[I18nEdit]]
--  <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('Dev:Fallbacklist')
local fallbacks = require('Module:Fallbacklist')
local entrypoint = require('Dev:Entrypoint')
local entrypoint = require('Module:Entrypoint')
local uselang
local uselang


Line 73: Line 70:
Data.__index = Data
Data.__index = Data


--- Gets data message utility.
--- 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
    -- 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 = nil
         local msg = (messages[lang] or {})[key]
        -- 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 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, '\r\n\f\v')
                 or  mw.text.trim(msg)
         end
         end
     end
     end
Line 249: Line 156:
end
end


--- Gets localised template parameter from a datastore.
--- 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


--- Sets temporary source to a specificed subset of datastores.
--- 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


--- Gets data default language.
--- 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


--- Sets data language to `wgUserLanguage`.
--- 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


--- Sets data language to `wgContentLanguage`.
--- 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 = i18n.getContentLang()
     self.defaultLang = mw.language.getContentLanguage():getCode()
     return self
     return self
end
end


--- Sets data language to specificed language.
--- 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


--- Sets temporary data language to `wgUserLanguage`.
--- Temporary datastore language setter to `wgUserLanguage`.
--  The data language reverts to the default language in the next
--  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


--- Sets temporary data language to `wgContentLanguage`.
--- 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 = i18n.getContentLang()
     self.tempLang = mw.language.getContentLanguage():getCode()
     return self
     return self
end
end


--- Sets temporary data language to a specificed language.
--- 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.


--- Gets a localized message by data module key.
--- 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 data collection's message.
--  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 data loader for message collections.
--- 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  'Dev:' .. source .. '/i18n'
                 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


--- Gets the language code of the wiki or page contents.
--- Language code getter.
--  @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 493: Line 384:
     local parentFrame = frame.getParent and frame:getParent() or {}
     local parentFrame = frame.getParent and frame:getParent() or {}


     local code = i18n.getContentLang()
     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.