---- 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.--
--------------------------------------------------------------------------------- Configuration---- Values that vary between languages are stored here, to enable easy-- porting between wikis.-------------------------------------------------------------------------------
local cfg =
-- Error message on receiving invalid input. %s is the input passed to the module, and should be included in the message exactly once.cfg.errorMessage = 'Error: invalid input "%s"; please use an ISO 639-1 code'cfg.errorClass = 'error' -- The CSS class used in error messages on the local wiki.cfg.errorCategory = 'Pages with invalid ISO 639-1 language codes' -- The name of the error category to display.
--------------------------------------------------------------------------------- End configuration-------------------------------------------------------------------------------
-- Define constants.local categoryNsText = mw.site.namespaces[14].namelocal currentLanguageCode = mw.language.getContentLanguage:getCode
-- Load dependent modules.local getArgs = require('Module:Arguments').getArgslocal yesno = require('Module:Yesno')
local p =
local codes =
function p.main(frame) local args = getArgs(frame) return p._main(args)end
function p._main(args) local input = args[1] if type(input) ~= 'string' then return -- input is either not a string or is a blank string that was removed by getArgs. end local inputLower = mw.ustring.lower(input) local ret if codes[inputLower] then ret = inputLower else local code = p.getCodeFromLanguageName(inputLower) if code and codes[code] then -- MediaWiki language codes might not be valid ISO 639-1 codes, so we need to check the codes table too. ret = code else ret = mw.ustring.format('' .. cfg.errorMessage .. '', cfg.errorClass, input) if not yesno(args.nocat) then ret = mw.ustring.format('%s%s', ret, categoryNsText, cfg.errorCategory, input) end end end return retend
function p.getCodeFromLanguageName(s) -- Gets an ISO 639-1 code from a language name, or returns nil if none can be found. local languages = mw.language.fetchLanguageNames(currentLanguageCode) for code, name in pairs(languages) do if s
return p