「モジュール:LangSwitch」の版間の差分

ナビゲーションに移動 検索に移動
fix error with global variables
(1版 をインポートしました)
bsd>Jarekt
(fix error with global variables)
10行目: 10行目:
* User:Jarekt  
* User:Jarekt  
]]
]]
-- add optional module
-- used for debugging purposes as it detects cases of unintended global variables
require('Module:No globals')


local p = {}
local p = {}
24行目: 28行目:
   args - table with translations by language
   args - table with translations by language
   lang - desired language (often user's native language)
   lang - desired language (often user's native language)
 
  Error Handling:
  Error Handling:
 
]]
]]
local function defaultCheck(args)
 
function p._langSwitch(args, lang) -- args: table of translations
-- Return error if there is not default and no english version
if not args.en and not args.default then
if not args.en and not args.default then
local err = '<b class="error">LangSwitch Error: no default</b>'
local err = '<b class="error">LangSwitch Error: no default</b>'
37行目: 43行目:
end
end
end
end
return false
end
-- To improve performance try quick switch, and load fallback chain only if needed.
 
-- In the vast majority of cases fast switch is sufficient
 
local val = args[lang]
local function quickSwitch(args, arg)
if val == '~' then  
local err = defaultCheck(args)
return ''
if err then
elseif val and val ~= '' then
return err
return val
end
elseif args.quick then
if arg == '~' then
return nil
arg = ''
end
return arg
end
 
 
function p._langSwitch(args, lang) -- args: table of translations
-- Return error if there is not default and no english version
local err = defaultCheck(args)
if err then
return err
end
end
-- get the list of accepetable language (lang + those in lang's fallback chain) and check their content
-- get the list of accepetable language (lang + those in lang's fallback chain) and check their content
assert(lang, 'LangSwitch Error: no lang')
assert(lang, 'LangSwitch Error: no lang')
--local langList = {lang}
--if not args[lang] then
local langList = mw.language.getFallbacksFor(lang)
local langList = mw.language.getFallbacksFor(lang)
table.insert(langList, 1, lang)
table.insert(langList,1,lang)
table.insert(langList, math.max(#langList, 2), 'default')
table.insert(langList,math.max(#langList,2),'default')
--end
for _, language in ipairs(langList) do  
 
val = args[language]
for _, language in ipairs(langList) do
if val == '~' then  
lang = args[language]
if lang == '~' then
return ''
return ''
elseif lang and lang ~= '' then
elseif val and val ~= '' then  
return lang
return val
end
end
end
end
90行目: 81行目:
   frame.args - table with translations by language
   frame.args - table with translations by language
   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.langSwitch(frame) -- version to be used from wikitext
function p.langSwitch(frame) -- 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 no expected args provided than check parent template/module args
if args.en == nil and args.default == nil and args.nocat == nil then
if args.en==nil and args.default==nil and args.nocat==nil then
args = mw.getCurrentFrame():getParent().args
args = mw.getCurrentFrame():getParent().args  
end
end
local lang = args.lang
local lang = args.lang
if not lang or not mw.language.isSupportedLanguage(lang) then
if not lang or not mw.language.isKnownLanguageTag(lang) then
lang = frame:callParserFunction("int", "lang") -- get user's chosen language
lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language  
end
end
 
-- Try quick switch
-- Try quick switch which checks the most likely option when fallback is not needed
local args1 = args[lang]
args.quick = true;
if args1 and args1 ~= '' then
local val = p._langSwitch(args, lang)
return quickSwitch(args, args1)
if val then
return val
end
end
 
-- Allow input in format: {{LangSwitch|de=Grün|es/it/pt=Verde|fr=Vert|en=Green |lang=en}}
-- Allow input in format: {{LangSwitch|de=Grün|es/it/pt=Verde|fr=Vert|en=Green |lang=en}}
-- with multiple languages mapping to a single value
-- with multiple languages mapping to a single value
args1 = {}
local args1 = {}
for name, value in pairs(args) do
for name, value in pairs( args ) do  
if value ~= '' and type(name) == 'string' then
if value ~= '' and type(name)=='string' then  
-- split multi keys
    for str in string.gmatch( name, "([^/]+)" ) do
for str in string.gmatch(name, "([^/]+)") do
args1[str] = value
args1[str] = value
end
end
匿名利用者

案内メニュー