Module:Engvar/detect/sandbox

local engvar_template_patterns = {                                              
--British, EngvarB, and American are listed first because they are the most commonly used
	['{{ *[Uu]se [Bb]i?ri?tish [Ee]nglish *[|}]'] = 'en-GB',
	['{{ *[Ee][Nn]gvar ?-? ?[Bb] *[|}]'] = 'en-GB',
	['{{ *[Uu]se [Aa]merican [Ee]nglish *[|}]'] = 'en-US',
-- Remaining countries in alphabetical order
	['{{ *[Uu]se [Aa]ustralian [Ee]nglish *[|}]'] = 'en-AU',
	['{{ *[Uu]se [Cc]anadian [Ee]nglish *[|}]'] = 'en-CA',	
	['{{ *[Uu]se [Hh]iberno ?-? ?[Ee]nglish *[|}]'] = 'en-EI',
	['{{ *[Uu]se [Hh]ong [Kkong] [Ee]nglish *[|}]'] = 'en-HK',
	['{{ *[Uu]se [Ii]ndian [Ee]nglish *[|}]'] = 'en-IN',
	['{{ *[Uu]se IUPAC [Ee]nglish *[|}]'] = 'en-IUPAC',							-- redirects to {{Use Oxford Spelling}}, but en-IUPAC is used here because it's supported by {{Engvar}}
	['{{ *[Uu]se [Jj]amacan [Ee]nglish *[|}]'] = 'en-JM',
	['{{ *[Uu]se [Nn]ew [Zz]ealand [Ee]nglish *[|}]'] = 'en-NZ',
	['{{ *[Uu]se [Nn]igerian [Ee]nglish *[|}]'] = 'en-NG',
	['{{ *[Uu]se [Oo]xford [Ss]pelling *[|}]'] = 'en-OED',
	['{{ *[Uu]se [Pp]akistani? [Ee]nglish *[|}]'] = 'en-PK',
	['{{ *[Uu]se [Pp]hilippine [Ee]nglish *[|}]'] = 'en-PH',
	['{{ *[Uu]se [Ss]cottish [Ee]nglish *[|}]'] = 'en-SCO',						-- redirects to {{Use British English}}, but en-SCO is used here because it's supported by {{Engvar}}
	['{{ *[Uu]se [Ss]ingapore [Ee]nglish *[|}]'] = 'en-SG',
	['{{ *[Uu]se [Ss]outh [Aa]frican? [Ee]nglish *[|}]'] = 'en-ZA',
	['{{ *[Uu]se [Tt]rinidad [Aa]nd [Tt]obago [Ee]nglish *[|}]'] = 'en-TT',
-- Redirects to the above templates	
	['{{ *[Ee][Bb] *[|}]'] = 'en-GB',
	['{{ *[Ee]n-?[Gg][Bb] *[|}]'] = 'en-GB',
	['{{ *[Uu]se [Bb]ritish *[|}]'] = 'en-GB',
	['{{ *[Uu][Bb][Ee] *[|}]'] = 'en-GB',
	['{{ *[Uu]se [Bb][Rr][Ee] *[|}]'] = 'en-GB',
	['{{ *[Uu]se [Ww]elsh [Ee]nglish *[|}]'] = 'en-GB',
	['{{ *[Uu]se [Bb]ritish [Ee]nglish spelling *[|}]'] = 'en-GB',
	['{{ *[Uu][Kk]-?[Ee][Nn] *[|}]'] = 'en-GB',

	['{{ *[Uu]se [Ee]uropean [Ee]nglish *[|}]'] = 'en-GB',						-- redirects to {{EngvarB}}
	['{{ *[Uu]se [Ii]nternational [Ee]nglish *[|}]'] = 'en-GB',					-- redirects to {{EngvarB}}
	['{{ *[Ii]nternational [Ee]nglish *[|}]'] = 'en-GB',						-- redirects to {{EngvarB}}
	
	['{{ *[Uu]se [Uu]%.?[Ss]%.? [Ee]nglish *[|}]'] = 'en-US',
	['{{ *[Uu]se [Uu]nited [Ss]tates [Ee]nglish *[|}]'] = 'en-US',
	['{{ *[Ee]ngvar[Aa] *[|}]'] = 'en-US',
	['{{ *[Ee]n-?[Uu][Ss] *[|}]'] = 'en-US',
	['{{ *[Uu][Ss]-?[Ee]n *[|}]'] = 'en-US',
	
	['{{ *[Ee]ngvar[Aa][Uu] *[|}]'] = 'en-AU',

	['{{ *[Ee]ngvar[Cc] *[|}]'] = 'en-CA',
	['{{ *[Ee]n-?[Cc][Aa]] *[|}]'] = 'en-CA',

	['{{ *[Uu]se [Hh]iberno ?-? ?[Ee]nglish [Ss]pelling *[|}]'] = 'en-EI',
	['{{ *[Uu]se [Ii]rish [Ee]nglish *[|}]'] = 'en-EI',
	
	['{{ *[Ee]n-?[Hh][Kk]] *[|}]'] = 'en-HK',
	['{{ *[Hh][Kk]-?[Ee]n *[|}]'] = 'en-HK',
	
	['{{ *[Uu]se [Bb]ritish %(?IUPAC%)? [Ee]nglish *[|}]'] = 'en-IUPAC',		-- redirects to {{Use Oxford Spelling}}, but en-IUPAC is used here because it's supported by {{Engvar}}
	
	['{{ *[Ee]n-?[Nn][Zz]] *[|}]'] = 'en-NZ',
	['{{ *[Uu]se [Nn][Zz] [Ee]nglish *[|}]'] = 'en-NZ',
	
	['{{ *[Ee]ngvar[Oo]x? *[|}]'] = 'en-OED',
	['{{ *[Uu]se [Bb]ritish %(?[Oo]xford%)? [Ee]nglish *[|}]'] = 'en-OED',
	['{{ *[Uu]se [Oo]xford [Ee]nglish *[|}]'] = 'en-OED',
	['{{ *[Ee]n-[Gg][Bb]-oxendict *[|}]'] = 'en-OED',
	['{{ *[Uu]se [Bb]ritish [Ee]nglish %(?[Oo]xford%)? [Ss]pelling *[|}]'] = 'en-OED',
	
	['{{ *[Uu]se [Ff]ilipino [Ee]nglish *[|}]'] = 'en-PH',
	
	['{{ *[Uu]se [Ss]ingaporean [Ee]nglish *[|}]'] = 'en-SG',
	
	['{{ *[Uu][Zz][Aa][Ee] *[|}]'] = 'en-ZA',
	
	['{{ *[Uu]se [Tt]rinidadian [Ee]nglish *[|}]'] = 'en-TT',
};

local function get_lang_var(pagename)
	local title_object = pagename and mw.title.new(pagename) or mw.title.getCurrentTitle();
	local content;	
	if 10 ~= title_object.namespace and 828 ~= title_object.namespace then		-- all namespaces except Template and Module
		content = title_object:getContent() or '';								-- get the content of the article or ''; new pages edited w/ve do not have 'content' until saved; ve does not preview; phab:T221625
	end

	if (not content or content == '') then															
		return nil;																
	end
	for pattern, var in pairs (engvar_template_patterns) do						-- loop through the patterns looking for {{Use American English}}, {{Use British English}}, etc., or any of their redirects
		if content:find(pattern) then
			return var;
		end
	end
	return nil;
end

local getArgs = require('Module:Arguments').getArgs

local function test(frame)
	local args = getArgs(frame)
	return get_lang_var(args.pagename)
end

return {detect = get_lang_var,test=test}