<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://commons.islamical.org/w/index.php?action=history&amp;feed=atom&amp;title=Module%3ALangSwitch%2Fsandbox</id>
	<title>Module:LangSwitch/sandbox - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://commons.islamical.org/w/index.php?action=history&amp;feed=atom&amp;title=Module%3ALangSwitch%2Fsandbox"/>
	<link rel="alternate" type="text/html" href="https://commons.islamical.org/w/index.php?title=Module:LangSwitch/sandbox&amp;action=history"/>
	<updated>2026-09-28T04:10:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://commons.islamical.org/w/index.php?title=Module:LangSwitch/sandbox&amp;diff=6037&amp;oldid=prev</id>
		<title>Shahroudi: Created page with &quot;--[[   __  __           _       _        _                      ____          _ _       _       |  \/  | ___   __| |_   _| | ___ _| |    __ _ _ __   __ _/ ___|_      _(_) |_ ___| |__    | |\/| |/ _ \ / _` | | | | |/ _ (_) |   / _` | &#039;_ \ / _` \___ \ \ /\ / / | __/ __| &#039;_ \   | |  | | (_) | (_| | |_| | |  __/_| |__| (_| | | | | (_| |___) \ V  V /| | || (__| | | |  |_|  |_|\___/ \__,_|\__,_|_|\___(_)_____\__,_|_| |_|\__, |____/ \_/\_/ |_|\__\___|_| |_|...&quot;</title>
		<link rel="alternate" type="text/html" href="https://commons.islamical.org/w/index.php?title=Module:LangSwitch/sandbox&amp;diff=6037&amp;oldid=prev"/>
		<updated>2026-09-27T04:32:15Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;--[[   __  __           _       _        _                      ____          _ _       _       |  \/  | ___   __| |_   _| | ___ _| |    __ _ _ __   __ _/ ___|_      _(_) |_ ___| |__    | |\/| |/ _ \ / _` | | | | |/ _ (_) |   / _` | &amp;#039;_ \ / _` \___ \ \ /\ / / | __/ __| &amp;#039;_ \   | |  | | (_) | (_| | |_| | |  __/_| |__| (_| | | | | (_| |___) \ V  V /| | || (__| | | |  |_|  |_|\___/ \__,_|\__,_|_|\___(_)_____\__,_|_| |_|\__, |____/ \_/\_/ |_|\__\___|_| |_|...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--[[&lt;br /&gt;
  __  __           _       _        _                      ____          _ _       _     &lt;br /&gt;
 |  \/  | ___   __| |_   _| | ___ _| |    __ _ _ __   __ _/ ___|_      _(_) |_ ___| |__  &lt;br /&gt;
 | |\/| |/ _ \ / _` | | | | |/ _ (_) |   / _` | &amp;#039;_ \ / _` \___ \ \ /\ / / | __/ __| &amp;#039;_ \ &lt;br /&gt;
 | |  | | (_) | (_| | |_| | |  __/_| |__| (_| | | | | (_| |___) \ V  V /| | || (__| | | |&lt;br /&gt;
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)_____\__,_|_| |_|\__, |____/ \_/\_/ |_|\__\___|_| |_|&lt;br /&gt;
                                                     |___/                               &lt;br /&gt;
 Authors and maintainers:&lt;br /&gt;
* User:Zolo - original version in Module:Fallback&lt;br /&gt;
* User:Jarekt - perfomance improvement to avoid loading fallbacks when not needed&lt;br /&gt;
* User:Verdy_p - performance improvements, fix for &amp;#039;default=*&amp;#039;&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
-- add optional module &lt;br /&gt;
-- used for debugging purposes as it detects cases of unintended global variables&lt;br /&gt;
require(&amp;#039;strict&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
local insert = table.insert&lt;br /&gt;
local concat = table.concat&lt;br /&gt;
local gmatch = string.gmatch&lt;br /&gt;
local getFallbacksFor = mw.language.getFallbacksFor&lt;br /&gt;
local getCurrentFrame = mw.getCurrentFrame&lt;br /&gt;
local isValidCode = mw.language.isValidCode&lt;br /&gt;
&lt;br /&gt;
local createTag -- lazy loading of the module: only for the files that need it&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
Add invisible but machine readable labels to the files, which can be used to add titles to wikidata items&lt;br /&gt;
]]&lt;br /&gt;
local function add_labels(args)&lt;br /&gt;
	if not createTag then&lt;br /&gt;
		createTag = require(&amp;#039;Module:TagQS&amp;#039;).createTag -- lazy loading of the module: only for the files that need it&lt;br /&gt;
	end&lt;br /&gt;
	-- Add text of invisible tag brodcasted by the template which allows creation of QuickStatements command used to add this info to Wikidata&lt;br /&gt;
	local qsTable = {}&lt;br /&gt;
	for key, text in pairs(args) do &lt;br /&gt;
		if type(key) == &amp;#039;string&amp;#039; and key ~= &amp;#039;add_labels&amp;#039; then&lt;br /&gt;
			for code in gmatch(key:lower(), &amp;#039;([%-0-9a-z]+)&amp;#039;) do&lt;br /&gt;
				if isValidCode(code) then -- code has to be a valid language&lt;br /&gt;
					insert(qsTable, createTag(&amp;#039;label&amp;#039;, &amp;#039;L&amp;#039; .. code, &amp;#039;&amp;quot;&amp;#039; .. text .. &amp;#039;&amp;quot;&amp;#039;))&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return concat(qsTable)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
_langSwitch&lt;br /&gt;
 &lt;br /&gt;
This function is the core part of the LangSwitch template. &lt;br /&gt;
 &lt;br /&gt;
Example usage from Lua:&lt;br /&gt;
  text = _langSwitch({en=&amp;#039;text in english&amp;#039;, pl=&amp;#039;tekst po polsku&amp;#039;}, lang)&lt;br /&gt;
 &lt;br /&gt;
Parameters:&lt;br /&gt;
  args - table with translations by language&lt;br /&gt;
  lang - desired language (often user&amp;#039;s native language)&lt;br /&gt;
&lt;br /&gt;
Error Handling:&lt;br /&gt;
]]&lt;br /&gt;
function p._langSwitch(args, lang, quick, nocat) -- args: table of translations only&lt;br /&gt;
	assert(lang, &amp;#039;LangSwitch Error: no lang&amp;#039;)&lt;br /&gt;
	-- Return error if there is not &amp;#039;default&amp;#039; and no &amp;#039;en&amp;#039; version&lt;br /&gt;
	if not args.default and not args.en then&lt;br /&gt;
		local err = &amp;#039;&amp;lt;b class=&amp;quot;error&amp;quot;&amp;gt;LangSwitch Error: no default&amp;lt;/b&amp;gt;&amp;#039;&lt;br /&gt;
		if nocat then&lt;br /&gt;
			return err&lt;br /&gt;
		else&lt;br /&gt;
			return err .. &amp;#039;[[Category:LangSwitch template without default version]]&amp;#039;&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	-- To improve performance try quick switch, and load fallback chain only if needed. &lt;br /&gt;
	-- In the vast majority of cases quick switch (not using any fallbacks) is sufficient.&lt;br /&gt;
	local value = args[lang]&lt;br /&gt;
	if value or quick then&lt;br /&gt;
		return value == &amp;#039;~&amp;#039; and &amp;#039;&amp;#039; or value&lt;br /&gt;
	end&lt;br /&gt;
	-- Get the list of acceptable languages (lang + those in lang&amp;#039;s fallback chain) and check their content.&lt;br /&gt;
	local langList = getFallbacksFor(lang)&lt;br /&gt;
	-- table.insert(langList, 1, lang) -- not needed (already checked in the quick test above)&lt;br /&gt;
	for _, language in ipairs(langList) do&lt;br /&gt;
		if language ~= &amp;#039;en&amp;#039; or lang:sub(1, 3) == &amp;#039;en-&amp;#039; then&lt;br /&gt;
			value = args[language]&lt;br /&gt;
			if value then&lt;br /&gt;
				return value == &amp;#039;~&amp;#039; and &amp;#039;&amp;#039; or value&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	value = args.default and args.default or args.en&lt;br /&gt;
	return value == &amp;#039;~&amp;#039; and &amp;#039;&amp;#039; or value&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[&lt;br /&gt;
langSwitch&lt;br /&gt;
 &lt;br /&gt;
This function is the core part of the LangSwitch template. &lt;br /&gt;
 &lt;br /&gt;
Example Usage from a template:&lt;br /&gt;
  {{#invoke:fallback|langSwitch|en=text in english|pl=tekst po polsku|lang={{int:lang}} }}&lt;br /&gt;
 &lt;br /&gt;
Parameters:&lt;br /&gt;
  frame.args - table with translations by language&lt;br /&gt;
  frame.args.lang - desired language (often user&amp;#039;s native language)&lt;br /&gt;
  frame.args.quick - if non-empty, disable the lookup for fallbacks&lt;br /&gt;
  frame.args.nocat - if non-empty, disable the tracking categorization if there&amp;#039;s no &amp;#039;default&amp;#039; or &amp;#039;en&amp;#039; version&lt;br /&gt;
&lt;br /&gt;
Error Handling:&lt;br /&gt;
]]&lt;br /&gt;
function p.langSwitch(frame) -- version to be used from wikitext&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	-- If no expected args provided, then check parent template/module args.&lt;br /&gt;
	if not(args.lang or args.en or args.default or args.quick or args.nocat) then&lt;br /&gt;
		args = getCurrentFrame():getParent().args&lt;br /&gt;
	end&lt;br /&gt;
	local lang = args.lang&lt;br /&gt;
	if not lang or lang == &amp;#039;&amp;#039; or not isValidCode(lang) then&lt;br /&gt;
		lang = frame:callParserFunction(&amp;#039;Int&amp;#039;, &amp;#039;Lang&amp;#039;) -- get user&amp;#039;s chosen language &lt;br /&gt;
	end&lt;br /&gt;
	lang = lang:lower()	&lt;br /&gt;
	-- add text of invisible tag brodcasted by the template which allows creation of QuickStatements command used to add this info to Wikidata&lt;br /&gt;
	local labels = &amp;#039;&amp;#039;&lt;br /&gt;
	if args.add_labels then&lt;br /&gt;
		labels = add_labels(args)&lt;br /&gt;
	end&lt;br /&gt;
	-- Fast lookup for most frequent simple cases (direct match, without composite keys or fallbacks)&lt;br /&gt;
	local value = args[lang]&lt;br /&gt;
	if value and value ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		return value ~= &amp;#039;~&amp;#039; and value or &amp;#039;&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	-- Allow input in format: {{LangSwitch|de=Grün|es/it/pt=Verde|fr=Vert|en=Green |lang=en}}&lt;br /&gt;
	-- with multiple languages mapping to a single value.&lt;br /&gt;
	-- We need a new table as we cannot change the args table from the caller&amp;#039;s or parent&amp;#039;s frame.&lt;br /&gt;
	local args1, nocat, quick = {}, args.nocat and args.nocat ~= &amp;#039;&amp;#039;, args.quick and args.quick ~= &amp;#039;&amp;#039;&lt;br /&gt;
	for key, value in pairs(args) do&lt;br /&gt;
		if value ~= &amp;#039;&amp;#039; and type(key) == &amp;#039;string&amp;#039;&lt;br /&gt;
		and not(key == &amp;#039;lang&amp;#039; or key == &amp;#039;quick&amp;#039; or key == &amp;#039;nocat&amp;#039;) then&lt;br /&gt;
			for code in gmatch(key:lower(), &amp;#039;([%-0-9a-z]+)&amp;#039;) do&lt;br /&gt;
				args1[code] = value&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return p._langSwitch(args1, lang, quick, nocat)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Shahroudi</name></author>
	</entry>
</feed>