local engvar_disable_getcontent = false
local p = {}
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 find_engvar(content)
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
end
function p.detect(pagename)
local title_object = mw.title.getCurrentTitle()
local is_current_title = true
if not pagename or pagename == title_object.fullText then
if _G.engvar_cache then
return _G.engvar_cache
end
else
title_object = mw.title.new(pagename)
is_current_title = false
end
if engvar_disable_getcontent then
return
end
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
local var = find_engvar(content)
if is_current_title and var then
_G.engvar_cache = var
_G.engvar_cache_certain = true
end
return var
end
function p.test(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
return p.detect(args.pagename)
end
function p.use_engvar(frame)
local title = frame:getParent():getTitle():gsub('/sandbox$', '')
local var = find_engvar('{{ ' .. title .. ' }}')
if not var then
return require('Module:Error').error{ message = 'unknown engvar' .. title }
end
if (not _G.currentTitle_engvar_cache
or not _G.currentTitle_engvar_cache_certain
or _G.currentTitle_engvar_cache == var) then
_G.currentTitle_engvar_cache = var
_G.currentTitle_engvar_cache_certain = true
return
end
return require('Module:Error').error{ message = 'engvar conflict' }
end
function p.use_mdy(frame)
if _G.currentTitle_engvar_cache then
return
end
_G.currentTitle_engvar_cache = 'en-US'
end
return p