Модуль:Fallback
Выгляд
Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Fallback/Дакумэнтацыя
local p = {}
-- List the full fallback chain from a language to default
function p.fblist(lang)
local fbtable = mw.language.getFallbacksFor(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, 'default')
return fbtable
end
--[==[
Return an error if there is not default and no Biełarusan version, otherwise
return the message in the most appropriate, plus the lang code as a second value.
--]==]
function p._langSwitch(args, lang)
-- args: table of translations
if not args['be-tarask'] and not args.default and not args.message and args.nocat ~= '1' then
return error("Памылка пераключальніка моваў: не зададзены дапомны варыянт")
end
-- get language (either stated one or user's default language)
if not lang then
return '<strong class="error">Памылка пераключальніка моваў: няма мовы</strong>' -- must become proper error
end
-- get the list of acceptable languages (lang + those in lang's fallback chain) and check their content
for _, code in ipairs(p.fblist(lang)) do
local msg = args[code]
if msg then
-- Trim the assigned message value before testing it.
msg = mw.text.trim(msg)
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
else
return msg, code
end
end
end
end
return nil
end
--[==[
Version to be used from wikitext.
--]==]
function p.langSwitch(frame)
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
args = frame:getParent().args
end
local lang
if args.lang and args.lang ~= '' then
lang = args.lang
args.lang = nil
else -- get user's chosen language.
lang = frame:preprocess( "{{int:lang}}" )
end
local str, language = p._langSwitch(args, lang)
return str -- get the first value of the langSwitch, (the text) not the second (the language)
end
function p.fallbackpage(base, lang, formatting)
local languages = p.fblist(lang)
for i, lng in ipairs(languages) do
if mw.title.new(base .. '/' .. lng).exists then
if formatting == 'table' then
return {base .. '/' .. lng, lng} -- returns name of the page + name of the language
else
return base .. '/' .. lng -- returns only the page
end
end
end
return base
end
--[==[
Лёгіка для [[Шаблён:Аўтапераклад]].
]==]
function p.autotranslate(frame)
local args = frame.args
if not args.lang or args.lang == '' then
args.lang = frame:preprocess( "{{int:lang}}" ) -- get user's chosen language
end
-- find base page
local base = args.base
if not base or base == '' then
return '<strong class="error">Для аўтаперакладу не прадастаўленая базавая старонка</strong>'
end
if string.sub(base, 2, 7) ~= 'аблён:' then
base = 'Шаблён:' .. base -- base provided without 'Шаблён:' part
end
-- find base template language subpage
local page = p.fallbackpage(base, args.lang)
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)
end
if not page then
return string.format('<strong class="error">ня знойдзеная запасная старонка для аўтаперакладу (база=[[%s]], мова=%s)</strong>', args.base, args.lang)
end
-- repack args in a standard table
local newargs = {}
for field, value in pairs(args) do
if field ~= 'base' then
newargs[field] = value
end
end
-- transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template
return frame:expandTemplate{ title = page, args = newargs }
end
--[==[
Translate data stored in a module.
]==]
function p.translate(page, key, lang)
if type(page) == 'string' then -- if the requested translation table is not yet loaded
page = require('Модуль:' .. page)
end
local val
if page[key] then
val = page[key]
elseif page.keys and page.keys[key] then -- key 'keys" is an index of all keys, including redirects, see [[Модуль:i18n/тып зьвестак]].
val = page.keys[key]
end
if not val then
return '<' .. key .. '>'
end
return p._langSwitch(val, lang)
end
function p.translatelua(frame)
local lang = frame.args.lang
local page = require('Модуль:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations
if not lang or mw.text.trim(lang) == '' then
lang = frame:preprocess( "{{int:lang}}" )
end
if frame.args[2] then
page = page[mw.text.trim(frame.args[2])]
end
return p._langSwitch(page, lang)
end
-- This test does not work ('Модуль:Fallback/тэсты/запасныя варыянты' is missing)
function p.runTests()
local toFallbackTest = require('Модуль:Fallback/тэсты/запасныя варыянты')
local result = true
mw.log('Тэст ланцужкоў запасных варыянтаў')
for i, t in ipairs(toFallbackTest) do
local fbtbl = table.concat(p.fblist(t.initial), ', ')
local expected = table.concat(t.expected, ', ')
local ret = (fbtbl == expected)
mw.log(i, ret and 'пройдзены' or 'ПРАВАЛЕНЫ', t.initial, (not ret) and ('FAILED\nis >>' .. fbtbl .. '<<\nbut should be >>' .. expected .. '<<\n') or '')
result = result and ret
end
return result
end
--[==[
List all input arguments of the template that calls "{{#invoke:Fallback|showTemplateArguments}}"
]==]
function p.showTemplateArguments(frame)
local str = ''
for name, value in pairs( frame:getParent().args ) do
if str == '' then
str = string.format('%s=%s', name, value) -- argument #1
else
str = string.format('%s, %s=%s', str, name, value) -- the rest
end
end
return str
end
return p