Module:LangSwitch
Jump to navigation
Jump to search
This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Starfleet resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
- Code for Template:LangSwitch.
--[[
__ __ _ _ _ ____ _ _ _
| \/ | ___ __| |_ _| | ___ _| | __ _ _ __ __ _/ ___|_ _(_) |_ ___| |__
| |\/| |/ _ \ / _` | | | | |/ _ (_) | / _` | '_ \ / _` \___ \ \ /\ / / | __/ __| '_ \
| | | | (_) | (_| | |_| | | __/_| |__| (_| | | | | (_| |___) \ V V /| | || (__| | | |
|_| |_|\___/ \__,_|\__,_|_|\___(_)_____\__,_|_| |_|\__, |____/ \_/\_/ |_|\__\___|_| |_|
|___/
Authors and maintainers:
* User:Zolo - original version in Module:Fallback
* User:Jarekt
]]
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
if args.nocat == '1' then
return '<strong class="error">LangSwitch Error: no default</strong>'
else
return '<strong class="error">LangSwitch Error: no default</strong>[[Category:LangSwitch template without default version]]'
end
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
if args[language ] == '~' then
return ''
elseif args[language] and args[language] ~= '' then
return args[language]
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
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.isSupportedLanguage(lang) then
lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
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
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