Module:LangSwitch

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Create] Documentation
  1 --[[
  2   __  __           _       _        _                      ____          _ _       _     
  3  |  \/  | ___   __| |_   _| | ___ _| |    __ _ _ __   __ _/ ___|_      _(_) |_ ___| |__  
  4  | |\/| |/ _ \ / _` | | | | |/ _ (_) |   / _` | '_ \ / _` \___ \ \ /\ / / | __/ __| '_ \ 
  5  | |  | | (_) | (_| | |_| | |  __/_| |__| (_| | | | | (_| |___) \ V  V /| | || (__| | | |
  6  |_|  |_|\___/ \__,_|\__,_|_|\___(_)_____\__,_|_| |_|\__, |____/ \_/\_/ |_|\__\___|_| |_|
  7                                                      |___/                               
  8  Authors and maintainers:
  9 * User:Zolo   - original version in Module:Fallback
 10 * User:Jarekt 
 11 ]]
 12 
 13 -- add optional module 
 14 -- used for debugging purposes as it detects cases of unintended global variables
 15 require('Module:No globals') 
 16 
 17 local p = {}
 18 
 19 --[[
 20 _langSwitch
 21  
 22 This function is the core part of the LangSwitch template. 
 23  
 24 Example usage from Lua:
 25 text = _langSwitch({en='text in english', pl='tekst po polsku'}, lang)
 26  
 27 Parameters:
 28   args - table with translations by language
 29   lang - desired language (often user's native language)
 30 
 31  Error Handling:
 32 
 33 ]]
 34 
 35 function p._langSwitch(args, lang) -- args: table of translations
 36 	-- Return error if there is not default and no english version
 37 	if not args.en and not args.default then
 38 		local err = '<b class="error">LangSwitch Error: no default</b>'
 39 		if args.nocat == '1' then
 40 			return err
 41 		else
 42 			return err .. '[[Category:LangSwitch template without default version]]'
 43 		end
 44 	end
 45 	
 46 	-- To improve performance try quick switch, and load fallback chain only if needed. 
 47 	-- In the vast majority of cases fast switch is sufficient
 48 	local val = args[lang]
 49 	if val == '~' then 
 50 		return ''
 51 	elseif val and val ~= '' then 
 52 		return val 
 53 	elseif args.quick then
 54 		return nil
 55 	end
 56 	
 57 	-- get the list of accepetable language (lang + those in lang's fallback chain) and check their content
 58 	assert(lang, 'LangSwitch Error: no lang')
 59 	local langList = mw.language.getFallbacksFor(lang)
 60 	table.insert(langList,1,lang)
 61 	table.insert(langList,math.max(#langList,2),'default')
 62 	for _, language in ipairs(langList) do 
 63 		val = args[language]
 64 		if val == '~' then 
 65 			return ''
 66 		elseif val and val ~= '' then 
 67 			return val 
 68 		end
 69 	end
 70 end
 71 
 72 --[[
 73 langSwitch
 74  
 75 This function is the core part of the LangSwitch template. 
 76  
 77 Example Usage from a template:
 78 {{#invoke:fallback|langSwitch|en=text in english|pl=tekst po polsku|lang={{int:lang}} }}
 79  
 80 Parameters:
 81   frame.args - table with translations by language
 82   frame.args.lang - desired language (often user's native language)
 83 
 84  Error Handling:
 85 
 86 ]]
 87 function p.langSwitch(frame) -- version to be used from wikitext
 88 	local args = frame.args
 89 	-- if no expected args provided than check parent template/module args
 90 	if args.en==nil and args.default==nil and args.nocat==nil then
 91 		args = mw.getCurrentFrame():getParent().args 
 92 	end
 93 	local lang = args.lang
 94 	if not lang or not mw.language.isKnownLanguageTag(lang) then
 95 		lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language 
 96 	end
 97 	
 98 	-- Try quick switch which checks the most likely option when fallback is not needed
 99 	args.quick = true;
100 	local val = p._langSwitch(args, lang)
101 	if val then
102 		return val
103 	end
104 	
105 	-- Allow input in format: {{LangSwitch|de=Grün|es/it/pt=Verde|fr=Vert|en=Green |lang=en}}
106 	-- with multiple languages mapping to a single value
107 	local args1 = {}
108 	for name, value in pairs( args ) do 
109 		if value ~= '' and type(name)=='string' then 
110     		for str in string.gmatch( name, "([^/]+)" ) do
111 				args1[str] = value
112 			end
113 		end
114 	end
115 	return p._langSwitch(args1, lang)
116 end
117 
118 return p