Module:Engvar: Difference between revisions

Content deleted Content added
Refine list of en-variant codes: use 'en-GB' not 'en-UK'; use 'en-SCO', more ISO-alpha3 options (per Template talk:Engvar). Improve 'explain' option.
Sync with sandbox, answer edit request
 
(5 intermediate revisions by 5 users not shown)
Line 6:
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local gsub = string.gsub
local lower = string.lower
Line 11 ⟶ 12:
 
function p.variants(frame)
local args = getArgs(frame)
return p._variants(args)
end
 
local function p._variantsfallback(args, useLang)
local cwealthLangs = { ['en-OED'] = 1, ['en-IUPAC'] = 1,
local returnWord = nil
['en-AU'] = 1, ['en-CA'] = 1, ['en-EI'] = 1, ['en-HK'] = 1,
['en-IN'] = 1, ['en-JM'] = 1, ['en-NZ'] = 1, ['en-PK'] = 1,
['en-SCO'] = 1, ['en-SG'] = 1, ['en-ZA'] = 1 }
if (useLang == 'en-IUPAC' or useLang == 'en-OED') and (args['en-OED'] or args['en-CA']) then
return args['en-OED'] or args['en-CA']
elseif useLang == 'en-CA' and (args['en-OED'] or args['en-US']) then
return args['en-OED'] or args['en-US']
elseif cwealthLangs[useLang] and args['en-GB'] then
return args['en-GB']
end
return args.defaultWord or ''
end
 
function p._variants(args)
local returnWord = nil
local auto = false
local defaultWord = args.defaultWord or ''
iflocal args.engvar == nil thenargs.engvar
 
if (lower(engvar or '') == 'auto') then
engvar = require('Module:Engvar/detect').detect()
auto = true
end
 
if engvar == nil then
-- Nothing to look for; use defaultWord right away
returnWord = defaultWord
elseif args.defaultLang == gsub(lower(args.engvar), '^en%-(%w%w)$', formatISO) then
-- By the defaultLang, the defaultWord is asked
returnWord = defaultWord
else
returnWord = args[gsub(lower(args.engvar), '^en%-(%w%w)$', formatISO)]
end
 
if returnWord == nil then
-- No hit so far. Search by words in the engvar entered, checking the list
local useLang = engvarLang(args.engvar)
if useLang == nil then
returnWord = defaultWord .. addMaintCat(args)
elseif args[useLang] then
returnWord = args[useLang]
elseif yesno(args.fallback or auto, auto) then --default to fallback if auto, otherwise default to no fallback
returnWord = fallback(args, useLang)
else
returnWord = args[useLang] or defaultWord
end
end
Line 44 ⟶ 73:
-- Returns arguments and intermediate result. Plus the template result, in front.
function p.explain(frame)
local args = getArgs(frame)
local ret = {}
local engvar = args.engvar or ''
table.insert(ret, '\n\n: Settings:')
table.insert(ret, 'defaultWord=' .. (args.defaultWord or '') .. '; ')
Line 51 ⟶ 81:
table.insert(ret, 'engvarCat=' .. (args.engvarCat or '[default:yes]') .. '; ')
table.insert(ret, 'engvarCatSort=' .. (args.engvarCatSort or ''))
table.insert(ret, '\n\n: engvarfallback=' .. (args.engvarfallback or '') .. ' [input] ')
localtable.insert(ret, useLang'\n\n: engvar=' engvarLang(args.. engvar or.. ' [input] ')
if (lower(engvar) == 'auto') then
engvar = require('Module:Engvar/detect').detect() or ''
table.insert(ret, 'engvar=' .. engvar .. ' [detected] ')
end
local useLang = engvarLang(engvar)
table.insert(ret, ' => Engvar code [used]: >' .. (useLang or '') .. '<.')
Line 73 ⟶ 108:
 
end
return (args.engvar or '') .. ' => ' .. p._variants(args) .. table.concat(ret, ' ')
end
 
Line 83 ⟶ 118:
function engvarLang(searchEngvar)
-- Search verbose language identifiers to ISO-format 'en-XX'
-- Assumed: not a blank string '' to seacrchsearch
local match = string.match
local langs = {
 
us = 'US', usa = 'US',
au = 'AU', aus = 'AU',
gb = 'GB', gbr = 'GB', uk = 'GB',
ca = 'CA', can = 'CA',
ei = 'EI', irl = 'EI',
hk = 'HK', hkg = 'HK',
['in'] = 'IN', ind = 'IN', --'in' is a reserved keyword and needs to be in quotes
jm = 'JM', jam = 'JM',
mw = 'MW', mwi = 'MW',
nz = 'NZ', nzl = 'NZ',
ng = 'NG', nga = 'NG',
pk = 'PK', pak = 'PK',
ph = 'PH', phl = 'PH',
sco = 'SCO',
sg = 'SG', sgp = 'SG',
za = 'ZA', zaf = 'ZA',
tt = 'TT', tto = 'TT',
oed = 'OED'};
searchEngvar = gsub(searchEngvar, '^%s*en%-(.*)', '%1') -- rm any opening 'en-'.
searchEngvar = gsub(lower(searchEngvar), '[%s%(%)%-]', '') -- To lc, remove all: (, ) , ws, hyphen.
--normalize language codes so we don't trigger the maintenence category on valid codes
local useLang
local useLang = langs[searchEngvar]
if useLang then
return 'en-'..useLang
--Special codes
ifelseif match(searchEngvar, 'oxford') then
useLang = 'OED' -- 'oxford' to catch before anyting 'british'
or searchEngvar == oed then
useLang = 'en-OED' -- 'oxford' to catch before anyting 'british'
elseif match(searchEngvar, 'iupac') then
useLang = 'en-IUPAC' -- chemistry
-- Very often used
elseif match(searchEngvar, 'british') then
or useLang searchEngvar == 'ukGB'
or elseif match(searchEngvar ==, 'gbrunitedstates' then)
or match(searchEngvar, 'american') then
useLang = 'en-GB'
useLang = 'US'
elseif searchEngvar == 'us'
or elseif match(searchEngvar, 'unitedstatesaustralia') then
useLang = 'AU'
or searchEngvar == 'american'
or searchEngvar == 'usa' then
useLang = 'en-US'
elseif match(searchEngvar, 'australia')
or searchEngvar == 'aus' then
useLang = 'en-AU'
-- Often used
elseif match(searchEngvar, 'india') then
or searchEngvar useLang == 'indIN' then
elseif match(searchEngvar, 'newzealand') then
useLang = 'en-IN'
useLang = 'NZ'
elseif searchEngvar == 'newzealand'
or elseif match(searchEngvar ==, 'nzlsouthafrica') then -- not: SA
useLang = 'en-NZZA'
elseif match(searchEngvar, 'southafricacanad') then --'canada' not:or en-SA'canadian'
or searchEngvar useLang == 'zafCA' then
useLang = 'en-ZA'
elseif searchEngvar == 'canada'
or searchEngvar == 'can' then
useLang = 'en-CA'
elseif match(searchEngvar, 'hiberno')
or match(searchEngvar, 'ireland')
or match(searchEngvar, 'irish') then
or searchEngvar useLang == 'irlEI' then
elseif match(searchEngvar, 'hongkong') then
useLang = 'en-EI'
useLang = 'HK'
elseif match(searchEngvar, 'hongkong')
or searchEngvar == 'hkg' then
useLang = 'en-HK'
-- Less often used
elseif match(searchEngvar, 'jamaica') then
or searchEngvar useLang == 'jamJM' then
elseif match(searchEngvar, 'malawi') then
useLang = 'en-JM'
useLang = 'MW'
elseif match(searchEngvar, 'malawi')
or elseif match(searchEngvar ==, 'mwinigeria') then
useLang = 'en-MWNG'
elseif match(searchEngvar, 'nigeriapakistan') then
or searchEngvar useLang == 'ngaPK' then
elseif match(searchEngvar, 'philippine') then
useLang = 'en-NG'
useLang = 'PH'
elseif match(searchEngvar, 'pakistan')
or searchEngvar == 'pak' then
useLang = 'en-PK'
elseif match(searchEngvar, 'philippine')
or searchEngvar == 'phl' then
useLang = 'en-PH'
elseif match(searchEngvar, 'scotland')
or match(searchEngvar, 'scottish') then
useLang = 'SCO' -- Has no alpha-2 code; not 'scotch'
or searchEngvar == 'sco' then
elseif match(searchEngvar, 'singapore') then
useLang = 'en-SCO' -- Has no alpha-2 code; not 'scotch'
useLang = 'SG'
elseif match(searchEngvar, 'singapore')
or searchEngvar == 'sgp' then
useLang = 'en-SG'
elseif match(searchEngvar, 'trinidad')
or match(searchEngvar, 'tobago') then
or searchEngvar useLang == 'ttoTT' then
useLang = 'en-TT'
else
useLang = nil
end
return useLang and 'en-'..useLang
end
 
function addMaintCat(args)
local catMaintenance
if args.engvarCat =~= 'no' then
else
local title = mw.title.getCurrentTitle()
if title:inNamespaces(0) then -- 0=main, 10=templ, 828=module