Module

Difference between revisions of "LangSwitch"

(per edit request)
(rollback)
Tag: Undo
 
(One intermediate revision by the same user not shown)
Line 8: Line 8:
 
  Authors and maintainers:
 
  Authors and maintainers:
 
* User:Zolo  - original version in Module:Fallback
 
* User:Zolo  - original version in Module:Fallback
* User:Jarekt  
+
* User:Jarekt - in Wikidata
 +
* User:Eihel
 
]]
 
]]
  
 
-- add optional module  
 
-- add optional module  
 
-- used for debugging purposes as it detects cases of unintended global variables
 
-- used for debugging purposes as it detects cases of unintended global variables
require('strict')
+
require('Module:No globals')  
  
 
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
 
  
 
--[[
 
--[[
Line 108: Line 95:
 
if not lang or not mw.language.isKnownLanguageTag(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
 
 
-- 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
 
end
 
 
Line 120: Line 101:
 
local val = p._langSwitch(args, lang)
 
local val = p._langSwitch(args, lang)
 
if val then
 
if val then
return val .. labels
+
return val
 
end
 
end
 
 
Line 133: Line 114:
 
end
 
end
 
end
 
end
return p._langSwitch(args1, lang) .. labels
+
return p._langSwitch(args1, lang)
 
end
 
end
  
 
return p
 
return p

Latest revision as of 15:33, 21 May 2023

Documentation for this module may be created at Module:LangSwitch/doc

--[[
  __  __           _       _        _                      ____          _ _       _     
 |  \/  | ___   __| |_   _| | ___ _| |    __ _ _ __   __ _/ ___|_      _(_) |_ ___| |__  
 | |\/| |/ _ \ / _` | | | | |/ _ (_) |   / _` | '_ \ / _` \___ \ \ /\ / / | __/ __| '_ \ 
 | |  | | (_) | (_| | |_| | |  __/_| |__| (_| | | | | (_| |___) \ V  V /| | || (__| | | |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)_____\__,_|_| |_|\__, |____/ \_/\_/ |_|\__\___|_| |_|
                                                     |___/                               
 Authors and maintainers:
* User:Zolo   - original version in Module:Fallback
* User:Jarekt - in Wikidata
* User:Eihel
]]

-- add optional module 
-- used for debugging purposes as it detects cases of unintended global variables
require('Module:No globals') 

local p = {}

--[[
_langSwitch
 
This function is the core part of the LangSwitch template. 
 
Example usage from Lua:
text = _langSwitch({en='text in english', pl='tekst po polsku'}, lang)
 
Parameters:
  args - table with translations by language
  lang - desired language (often user's native language)

 Error Handling:

]]

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
		local err = '<b class="error">LangSwitch Error: no default</b>'
		if args.nocat == '1' then
			return err
		else
			return err .. '[[Category:LangSwitch template without default version]]'
		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
	
	-- get the list of accepetable language (lang + those in lang's fallback chain) and check their content
	assert(lang, 'LangSwitch Error: no lang')
	local langList = mw.language.getFallbacksFor(lang)
	table.insert(langList,1,lang)
	table.insert(langList,math.max(#langList,2),'default')
	for _, language in ipairs(langList) do 
		val = args[language]
		if val == '~' then 
			return ''
		elseif val and val ~= '' then 
			return val 
		end
	end
end

--[[
langSwitch
 
This function is the core part of the LangSwitch template. 
 
Example Usage from a template:
{{#invoke:fallback|langSwitch|en=text in english|pl=tekst po polsku|lang={{int:lang}} }}
 
Parameters:
  frame.args - table with translations by language
  frame.args.lang - desired language (often user's native language)

 Error Handling:

]]
function p.langSwitch(frame) -- version to be used from wikitext
	local args = frame.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
		args = mw.getCurrentFrame():getParent().args 
	end
	local lang = args.lang
	if not lang or not mw.language.isKnownLanguageTag(lang) then
		lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language 
	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
	end
	
	-- 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
	local args1 = {}
	for name, value in pairs( args ) do 
		if value ~= '' and type(name)=='string' then 
    		for str in string.gmatch( name, "([^/]+)" ) do
				args1[str] = value
			end
		end
	end
	return p._langSwitch(args1, lang)
end

return p