Jump to content

Module:Sandbox/Mr. Stradivarius/Check ISO 639-1

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 14:54, 13 December 2013 (use the list of codes from ISO 639-1 language matrix, as I misunderstood what mw.language.isKnownLanguageTag was checking). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
-- This module detects whether the first positional parameter is a valid ISO 639-1 language code.
-- If it is, it returns the code. If not, it returns an error and a tracking category. For blank
-- or non-string input, the returns a blank string. The tracking category is sorted by the language
-- code, rather than by the page name.
--]]

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

local p = {}

local codes = {
	ab = true, af = true, an = true, ar = true, as = true, az = true, be = true, bg = true, bn = true, bo = true,
	br = true, bs = true, ca = true, ce = true, co = true, cs = true, cu = true, cy = true, da = true, de = true,
	el = true, en = true, eo = true, es = true, et = true, eu = true, fa = true, fi = true, fj = true, fo = true,
	fr = true, fy = true, ga = true, gd = true, gl = true, gv = true, he = true, hi = true, hr = true, ht = true,
	hu = true, hy = true, id = true, is = true, it = true, ja = true, jv = true, ka = true, kg = true, ko = true,
	ku = true, kw = true, ky = true, la = true, lb = true, li = true, ln = true, lt = true, lv = true, mg = true,
	mk = true, mn = true, mo = true, ms = true, mt = true, my = true, nb = true, ne = true, nl = true, nn = true,
	no = true, oc = true, pl = true, pt = true, rm = true, ro = true, ru = true, sc = true, se = true, sk = true,
	sl = true, so = true, sq = true, sr = true, sv = true, sw = true, tk = true, tr = true, ty = true, uk = true,
	ur = true, uz = true, vi = true, vo = true, yi = true, zh = true
}

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local code = args[1]
	if type(code) ~= 'string' then
		return '' -- code is either not a string or is a blank string that was removed by getArgs.
	end
	local ret = mw.ustring.lower(code)
	if not codes[ret] then
		ret = '<strong class="error">Error: "' .. code .. '" is not a valid [[List of ISO 639-1 codes|ISO 639-1 code]]</strong>'
		if not yesno(args.nocat) then
			ret = ret .. '[[Category:Pages with invalid ISO 639-1 language codes|' .. code .. ']]'
		end
	end
	return ret
end

return p