അജ്ഞാത ഉപയോക്താവ്
per edit request
(ചെ.) (ഒരു പതിപ്പ് ഇറക്കുമതി ചെയ്തു) |
(per edit request) |
||
വരി 10: | വരി 10: | ||
* User:Jarekt | * User:Jarekt | ||
]] | ]] | ||
-- add optional module | |||
-- used for debugging purposes as it detects cases of unintended global variables | |||
require('strict') | |||
local p = {} | local p = {} | ||
local function add_labels(args) | |||
-- add invisible but machine readable labels to the files, which can be used to add titles to wikidata items | |||
local createTag = require('Module:TagQS').createTag -- lazy loading of the module: only for the files that need it | |||
local qsTable = {''} | |||
-- add text of invisible tag brodcasted by the template which allows creation of QuickStatements command used to add this info to Wikidata | |||
for lang, text in pairs( args ) do | |||
if type(lang)=='string' and mw.language.isSupportedLanguage(lang) then -- lang has to be a valid language | |||
table.insert( qsTable, createTag('label', 'L'..lang, '"' .. text .. '"') ) | |||
end | |||
end | |||
return table.concat( qsTable, '\n') | |||
end | |||
--[[ | --[[ | ||
വരി 32: | വരി 50: | ||
-- Return error if there is not default and no english version | -- 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>' | |||
if args.nocat == '1' then | if args.nocat == '1' then | ||
return | return err | ||
else | else | ||
return ' | return err .. '[[Category:LangSwitch template without default version]]' | ||
end | end | ||
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] | |||
if val == '~' then | |||
return '' | |||
elseif val and val ~= '' then | |||
return val | |||
elseif args.quick then | |||
return nil | |||
end | end | ||
വരി 44: | വരി 74: | ||
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') | ||
for _, language | for _, language in ipairs(langList) do | ||
val = args[language] | |||
if val == '~' then | |||
return '' | return '' | ||
elseif | elseif val and val ~= '' then | ||
return | return val | ||
end | end | ||
end | end | ||
വരി 69: | വരി 100: | ||
]] | ]] | ||
function p.langSwitch(frame) -- version to be used from wikitext | function p.langSwitch(frame) -- version to be used from wikitext | ||
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 | ||
വരി 75: | വരി 106: | ||
end | end | ||
local lang = args.lang | local lang = args.lang | ||
if not lang or not mw.language. | 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 | |||
-- add text of invisible tag brodcasted by the template which allows creation of QuickStatements command used to add this info to Wikidata | |||
local labels = '' | |||
if args.add_labels then | |||
labels = add_labels(args) | |||
end | |||
-- Try quick switch which checks the most likely option when fallback is not needed | |||
args.quick = true; | |||
local val = p._langSwitch(args, lang) | |||
if val then | |||
return val .. labels | |||
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 | ||
വരി 89: | വരി 133: | ||
end | end | ||
end | end | ||
return p._langSwitch(args1, lang) | return p._langSwitch(args1, lang) .. labels | ||
end | end | ||
return p | return p |