Module:Fallback: Difference between revisions
Content added Content deleted
m (3 revisions imported: testing) |
m (safety check for the "message=ID" (which may not exist or could be disabled) ; add more precise comments to describe some parts.) |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
-- List the full fallback chain from a language to default (usually English) |
|||
function p.fblist(lang) |
|||
local fbtable = mw.language.getFallbacksFor(lang) |
local fbtable = mw.language.getFallbacksFor(lang) |
||
table.insert(fbtable, 1, lang) |
table.insert(fbtable, 1, lang) |
||
--[[ |
|||
Take a translation from "Mediawiki:<Message-ID>/<language-code>" namespace |
|||
or from a loaded i18 resource bundle in MediaWiki for its UI messages (also |
|||
used by the "{{Int:<Message-ID>}}" parser function), before using the |
|||
provided default value. Requires args.message = 'Message-ID', instead of |
|||
args.message = 'actual translated message'. |
|||
--]] |
|||
table.insert(fbtable, 'message') |
table.insert(fbtable, 'message') |
||
table.insert(fbtable, 'default') |
table.insert(fbtable, 'default') |
||
Line 9: | Line 17: | ||
end |
end |
||
--[==[ |
|||
⚫ | |||
Return an error if there is not default and no English version, otherwise |
|||
return the message in the most appropriate, plus the lang code as a second value. |
|||
--]==] |
|||
⚫ | |||
-- args: table of translations |
|||
if not args.en and not args.default and not args.message and args.nocat ~= '1' then |
if not args.en and not args.default and not args.message and args.nocat ~= '1' then |
||
return error("langSwitch error: no default") |
return error("langSwitch error: no default") |
||
end |
end |
||
-- |
-- Get language (either stated one or user's default language). |
||
if not lang then |
if not lang then |
||
return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error |
return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error |
||
end |
end |
||
-- |
-- Get the list of acceptable language (lang + those in lang's fallback chain) and check their content. |
||
for |
for _, code in ipairs(p.fblist(lang)) do |
||
local msg = args[code] |
|||
if msg then |
|||
-- Trim the assigned message value before testing it. |
|||
if j == 'message' then return tostring(mw.message.new(args[j]):inLanguage(lang)) end -- if this is an interface message |
|||
msg = msg:gsub('^%s*(.-)%s*$') |
|||
if args[j] ~= '' then return args[j], j end |
|||
if msg ~= '' then |
|||
if code == 'message' then |
|||
-- If this is an UI message. See [[mw:Manual:Messages API]]. |
|||
msg = mw.message.new(args.message):inLanguage(lang) |
|||
--[==[ If this message name does not exist, converting it to |
|||
a string would not return an actual message, but this name |
|||
within curved angle brackets U+29FC/U+29FD '⧼/⧽', |
|||
part of mathematical symbols). The UI message may also be |
|||
disabled administratively if it causes problems. |
|||
--]==] |
|||
if msg:exists() and not msg:isDisabled() then |
|||
--[==[FIXME: In which language is this message? |
|||
This may be in some fallback language and not lang. |
|||
Note that some UI messages may have placeholders like '%s' |
|||
but there's no way to replace them here by actual values. |
|||
--]==] |
|||
return tostring(msg), lang |
|||
end |
|||
elseif msg == '~' then |
|||
return nil, code |
|||
elseif msg ~= '' then |
|||
return msg, code |
|||
end |
|||
end |
|||
end |
end |
||
end |
end |
||
Line 30: | Line 65: | ||
end |
end |
||
--[==[ |
|||
⚫ | |||
Version to be used from wikitext. |
|||
--]==] |
|||
⚫ | |||
local args = frame.args |
local args = frame.args |
||
-- |
-- If no expected args provided than check parent template/module args. |
||
if not args.en and not args.default and not args.nocat then |
if not args.en and not args.default and not args.nocat then |
||
args = frame:getParent().args |
args = frame:getParent().args |
||
Line 40: | Line 78: | ||
lang = args.lang |
lang = args.lang |
||
args.lang = nil |
args.lang = nil |
||
else -- |
else -- Get user's chosen language. |
||
lang = frame:preprocess( |
lang = frame:preprocess("{{Int:Lang}}") |
||
end |
end |
||
--[==[ |
|||
if args.zh ~= '' and args['zh-hans'] == '' and args['zh-hant'] == '' then |
if args.zh ~= '' and args['zh-hans'] == '' and args['zh-hant'] == '' then |
||
else |
else |
||
end |
end |
||
--]==] |
|||
local str, language = p._langSwitch(args, lang) |
local str, language = p._langSwitch(args, lang) |
||
return str -- |
return str -- Get the first value of the langSwitch, (the text) not the second (the language). |
||
end |
end |
||
Line 59: | Line 95: | ||
if mw.title.new(base .. '/' .. lng).exists then |
if mw.title.new(base .. '/' .. lng).exists then |
||
if formatting == 'table' then |
if formatting == 'table' then |
||
return {base .. '/' .. lng, lng} -- |
return {base .. '/' .. lng, lng} -- Returns name of the page + name of the language. |
||
else |
else |
||
return base .. '/' .. lng -- |
return base .. '/' .. lng -- Returns only the page. |
||
end |
end |
||
end |
end |
||
Line 68: | Line 104: | ||
end |
end |
||
--[==[ |
|||
⚫ | |||
Logic for [[Template:Autotranslate]]. |
|||
]==] |
|||
⚫ | |||
local args = frame.args |
local args = frame.args |
||
if not args.lang or args.lang == '' then |
if not args.lang or args.lang == '' then |
||
args.lang = frame:preprocess( |
args.lang = frame:preprocess("{{Int:Lang}}") -- Get user's chosen language. |
||
end |
end |
||
⚫ | |||
⚫ | |||
local base = args.base |
local base = args.base |
||
if not base or base == '' then |
if not base or base == '' then |
||
Line 80: | Line 118: | ||
end |
end |
||
if string.sub(base, 2, 9) ~= 'emplate:' then |
if string.sub(base, 2, 9) ~= 'emplate:' then |
||
base = 'Template:' .. base |
base = 'Template:' .. base -- Base provided without 'Template:' part. |
||
end |
end |
||
⚫ | |||
⚫ | |||
local page = p.fallbackpage(base, args.lang) -- |
local page = p.fallbackpage(base, args.lang) -- |
||
if (not page and base ~= args.base) then |
if (not page and base ~= args.base) then |
||
-- |
-- Try the original args.base string. This case is only needed if base is not in template namespace. |
||
page = p.fallbackpage(args.base, args.lang) |
page = p.fallbackpage(args.base, args.lang) |
||
end |
end |
||
Line 92: | Line 129: | ||
return string.format('<strong class="error">no fallback page found for autotranslate (base=[[%s]], lang=%s)</strong>', args.base, args.lang) |
return string.format('<strong class="error">no fallback page found for autotranslate (base=[[%s]], lang=%s)</strong>', args.base, args.lang) |
||
end |
end |
||
⚫ | |||
⚫ | |||
local newargs = {} |
local newargs = {} |
||
for field, value in pairs(args) do |
for field, value in pairs(args) do |
||
Line 100: | Line 136: | ||
end |
end |
||
end |
end |
||
-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template. |
-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template. |
||
return frame:expandTemplate{ title = page, args = newargs } |
return frame:expandTemplate{ title = page, args = newargs } |
||
end |
end |
||
--[==[ |
|||
⚫ | |||
Translate data stored in a module. |
|||
⚫ | |||
]==] |
|||
⚫ | |||
⚫ | |||
page = require('Module:' .. page) |
page = require('Module:' .. page) |
||
end |
end |
||
local val |
local val |
||
if page[key] then |
if page[key] then |
||
val = page[key] |
val = page[key] |
||
elseif page.keys and page.keys[key] then-- |
elseif page.keys and page.keys[key] then -- Key 'keys" is an index of all keys, including redirects, see [[Module:i18n/datatype]]. |
||
val = page.keys[key] |
val = page.keys[key] |
||
end |
end |
||
Line 124: | Line 161: | ||
function p.translatelua(frame) |
function p.translatelua(frame) |
||
local lang = frame.args.lang |
local lang = frame.args.lang |
||
local page = require('Module:' .. mw.text.trim(frame.args[1])) -- |
local page = require('Module:' .. mw.text.trim(frame.args[1])) -- Page should only contain a simple of translations. |
||
if not lang or mw.text.trim(lang) == '' then |
if not lang or mw.text.trim(lang) == '' then |
||
lang = frame:preprocess( |
lang = frame:preprocess("{{Int:Lang}}") |
||
end |
end |
||
if frame.args[2] then |
if frame.args[2] then |
||
Line 134: | Line 171: | ||
end |
end |
||
-- This test does not work ('Module:Fallback/tests/fallbacks' is missing) |
|||
function p.runTests() |
function p.runTests() |
||
local toFallbackTest = require('Module:Fallback/tests/fallbacks') |
local toFallbackTest = require('Module:Fallback/tests/fallbacks') |
||
local result = true |
local result = true |
||
mw.log('Testing fallback chains') |
mw.log('Testing fallback chains') |
||
for i, t in ipairs(toFallbackTest) do |
for i, t in ipairs(toFallbackTest) do |
||
Line 146: | Line 183: | ||
result = result and ret |
result = result and ret |
||
end |
end |
||
return result |
return result |
||
end |
end |
||
--[==[ |
|||
⚫ | |||
]==] |
|||
function p.showTemplateArguments(frame) |
function p.showTemplateArguments(frame) |
||
⚫ | |||
local str = '' |
local str = '' |
||
for name, value in pairs( frame:getParent().args ) do |
for name, value in pairs( frame:getParent().args ) do |