「モジュール:Fallback」の版間の差分
ナビゲーションに移動
検索に移動
Splitting Module:Fallback into Module:Autotranslate, Module:LangSwitch and Module:Fallback
細 (1版) |
(Splitting Module:Fallback into Module:Autotranslate, Module:LangSwitch and Module:Fallback) |
||
1行目: | 1行目: | ||
--[[ | --[[ | ||
__ __ _ _ _____ _ _ _ _ | |||
| \/ | ___ __| |_ _| | ___ _| ___|_ _| | | |__ __ _ ___| | __ | |||
| |\/| |/ _ \ / _` | | | | |/ _ (_) |_ / _` | | | '_ \ / _` |/ __| |/ / | |||
| | | | (_) | (_| | |_| | | __/_| _| (_| | | | |_) | (_| | (__| < | |||
|_| |_|\___/ \__,_|\__,_|_|\___(_)_| \__,_|_|_|_.__/ \__,_|\___|_|\_\ | |||
Authors and maintainers: | |||
* User:Zolo - original version | |||
* User:Jarekt | |||
]] | ]] | ||
local p = {} | |||
{ | |||
--[[ | --[[ | ||
141行目: | 23行目: | ||
Parameters: | Parameters: | ||
frame.args.1 - name of translation module | frame.args.1 - name of translation module | ||
frame.args.2 - field name of the structure in Module:[frame.args.1] to use | |||
frame.args.lang - desired language (often user's native language) | frame.args.lang - desired language (often user's native language) | ||
Error Handling: | Error Handling: | ||
]] | ]] | ||
function p.translatelua(frame) | function p.translatelua(frame) | ||
local lang = frame.args.lang | local lang = mw.text.trim(frame.args.lang) | ||
local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations | local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations | ||
if not lang or | if not lang or lang == '' then | ||
lang = frame:callParserFunction( "int", "lang" ) | lang = frame:callParserFunction("int", "lang") | ||
end | end | ||
if frame.args[2] then | if frame.args[2] then | ||
page = page[mw.text.trim(frame.args[2])] | page = page[mw.text.trim(frame.args[2])] | ||
end | end | ||
local langList = mw.language.getFallbacksFor(lang) | |||
table.insert(langList,1,lang) | |||
for _,language in ipairs(langList) do | |||
if page[language] then | |||
return page[language] | |||
end | |||
end | |||
end | end | ||
166行目: | 55行目: | ||
Parameters: | Parameters: | ||
lang - desired language (often user's native language) | lang - desired language (often user's native language) | ||
Error Handling: | Error Handling: | ||
]] | ]] | ||
function p.fblist(lang) -- list the full fallback chain from a language to en | function p.fblist(lang) -- list the full fallback chain from a language to en | ||
local fbtable = p.fallbackloop{ lang:lower() } | local fbtable = p.fallbackloop{lang:lower()} | ||
table.insert(fbtable, 'default') | table.insert(fbtable, 'default') | ||
table.insert(fbtable, 'en') | table.insert(fbtable, 'en') | ||
177行目: | 66行目: | ||
end | end | ||
function _inArray(x, t) | local function _inArray(x, t) | ||
for i, v in ipairs(t) do | for i, v in ipairs(t) do | ||
if v == x then return i end | if v == x then return i end | ||
184行目: | 73行目: | ||
end | end | ||
function p.fallbackloop(fbtable) | function p.fallbackloop(fbtable) --list of fallback languages in string format (more convenient than tables) | ||
local langlist = require('Module:Fallbacklist') | |||
local changes = false | local changes = false | ||
for i, j in ipairs(fbtable) do | for i, j in ipairs(fbtable) do |