Module:Engvar: Difference between revisions

Content deleted Content added
No edit summary
Sync with sandbox, answer edit request
 
(17 intermediate revisions by 6 users not shown)
Line 1:
-- This module implements templateTemplate:Engvar.
-- The defaultWordTemplate:Engvar is returned,to unlessbe thebuild engvarinto inputthe hitstemplate on(like an existinginfobox), with default & variant (en-XX)spellings worddefined.
-- That template should also allow parameter '|engvar=' for the editor (article page).
-- The module/template Engvar then returns the spelling variant as is set in that article (for example '|engvar=en-GB').
-- The defaultWord is returned, unless the engvar input hits on a defined (en-XX) variant word.
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local gsub = string.gsub
local lower = string.lower
Line 8 ⟶ 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
 
--Nothing to look for; use defaultWord right away
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)
local catMaintenance
ifelseif args.engvarCat == 'no'[useLang] then
returnWord = args[useLang]
else
elseif yesno(args.fallback or auto, auto) then --default to fallback if auto, otherwise default to no fallback
local title = mw.title.getCurrentTitle()
returnWord = fallback(args, useLang)
if title:inNamespaces(0, 10, 828) then --main, templ, module
catMaintenance = '_[[:Category:Articles using an unknown Template:Engvar option]]'
end
end
returnWord = defaultWord .. (catMaintenance or '')
else
returnWord = args[useLang] or defaultWord
end
end
Line 47 ⟶ 71:
end
 
-- Returns arguments and intermediate result. Plus the template result, in front.
function p.explain(frame)
local args = getArgs(frame)
local ret = {'\n\n* Module:Engvar: '}
table.insert(ret,local 2,engvar 'defaultWord=' .. (args.defaultWordengvar or '') .. '; ')
table.insert(ret, 3, 'defaultLang=' .. (args.defaultLang or '') .. ';\n\n: Settings:')
table.insert(ret, 4, 'engvardefaultWord=' .. (args.engvardefaultWord or '') .. '; ')
table.insert(ret, 'defaultLang=' .. (args.defaultLang or '') .. '; ')
table.insert(ret, 'engvarCat=' .. (args.engvarCat or '[default:yes]') .. '; ')
table.insert(ret, 'engvarCatSort=' .. (args.engvarCatSort or ''))
table.insert(ret, 'fallback=' .. (args.fallback or ''))
table.insert(ret, '\n\n: engvar=' .. engvar .. ' [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 '') .. '<.')
for k, v in pairs(args) do
if k == 'en-UK' then
table.insert(ret, k .. ' ? better: use "en-GB"; ')
elseif k == 'en-SA' then
table.insert(ret, k .. ' ? misleading; use "en-ZA"; ')
end
if k == 'defaultWord' then
elseif k == 'defaultLang' then
elseif k == 'engvar' then
elseif k == 'engvarcat' then
elseif k == gsub(lower(k), '^en%-(%w%w)$', formatISO) then
table.insert(ret, k .. '=' .. v .. '; ')
else
table.insert(ret, k .. ' [not standard:]=' .. v .. '; ')
 
table.insert(ret, k .. ' (check, lowercase?)=' .. v .. ' ; ')
 
end
 
end
localreturn useLangengvar .. ' => engvarLang' .. p._variants(args) ..engvar ortable.concat(ret, ' ')
table.insert(ret, 5, 'Engvar code: >' .. (useLang or '') .. '<.\n\n:')
return p._variants(args) .. table.concat(ret)
end
 
-- Turn a match into pattern 'en-XX'
function formatISO(country)
return ('en-' .. string.upper(country) or '')
end
 
function engvarLang(searchEngvar)
-- Search verbose language identifiers to ISO-format 'en-XX'
-- Assumed: not a blank string '' to search
local match = string.match
local match = string.match
 
local langs = {
searchEngvar = gsub(string.lower(searchEngvar), '[%s%(%)%-]', '') -- to lc, remove all: (, ) , ws, hyphen.
us = 'US', usa = 'US',
local useLang
au = 'AU', aus = 'AU',
--specials
gb = 'GB', gbr = 'GB', uk = 'GB',
if match(searchEngvar, 'oxford')
ca = 'CA', can = 'CA',
or searchEngvar == 'enoed'
ei = 'EI', irl = 'EI',
or searchEngvar == oed then
hk = 'HK', hkg = 'HK',
useLang = 'en-OED' -- 'oxford' to catch before anyting 'british'
['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 = langs[searchEngvar]
if useLang then
return 'en-'..useLang
--Special codes
elseif match(searchEngvar, 'oxford') then
useLang = 'OED' -- 'oxford' to catch before anyting 'british'
elseif match(searchEngvar, 'iupac') then
useLang = 'en-IUPAC' -- chemistry
-- veryVery often used
elseif match(searchEngvar, 'british') then
or useLang searchEngvar == 'ukGB'
or elseif match(searchEngvar==, 'engbunitedstates' then)
or match(searchEngvar, 'american') then
useLang = 'en-UK'
useLang = 'US'
elseif searchEngvar == 'us'
or match(searchEngvar, 'unitedstates')
or searchEngvar=='american' then
useLang = 'en-US'
elseif match(searchEngvar, 'australia') then
useLang = 'en-AU'
-- oftenOften used
elseif match(searchEngvar, 'india') then
useLang = 'en-IN'
elseif match(searchEngvar ==, 'newzealand') then
useLang = 'en-NZ'
elseif match(searchEngvar, 'southafrica') then -- not: SA
or searchEngvar useLang == 'ensaZA' then
elseif match(searchEngvar, 'canad') then --'canada' or 'canadian'
useLang = 'en-ZA'
useLang = 'CA'
elseif searchEngvar == 'canada' then
useLang = 'en-CA'
elseif match(searchEngvar, 'hiberno')
or match(searchEngvar, 'ireland')
or match(searchEngvar, 'irish') then
useLang = 'en-EI'
elseif match(searchEngvar ==, 'hongkong') then
useLang = 'en-HK'
--less Less often used
elseif match(searchEngvar, 'jamaica') then
useLang = 'en-JM'
elseif match(searchEngvar, 'malawi') then
useLang = 'en-MW'
elseif match(searchEngvar, 'nigeria') then
useLang = 'en-NG'
elseif match(searchEngvar, 'pakistan') then
useLang = 'en-PK'
elseif match(searchEngvar, 'philippine') then
useLang = 'en-PH'
elseif match(searchEngvar, 'scotland')
or match(searchEngvar, 'scots')
or match(searchEngvar, 'scottish') then
useLang = 'en-SCOTLANDSCO' -- hasHas no alpha-2 code; not 'scotch'
elseif match(searchEngvar, 'singapore') then
useLang = 'en-SG'
elseif match(searchEngvar, 'trinidad')
or match(searchEngvar, 'tobago') then
useLang = 'en-TT'
else
useLang = nil
end
return useLang and 'en-'..useLang
end
 
function addMaintCat(args)
local catMaintenance
if args.engvarCat ~= 'no' then
local title = mw.title.getCurrentTitle()
if title:inNamespaces(0) then -- 0=main, 10=templ, 828=module
if args.engvarCatSort then
catMaintenance = '|' .. args.engvarCatSort .. ', ' .. title.text
end
catMaintenance = '[[Category:Articles using an unknown Template:Engvar option' .. (catMaintenance or '') .. ']]'
end
end
return catMaintenance or ''
end