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 05:07, 13 December 2013 (create module for checking ISO 639-1 language codes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

--[[
-- 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 p = {}

function p.main(frame)
	local args = getArgs(frame)
	local code = args[1]
	return p._main(code)
end

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

return p