Module:Languagename
Appearance
This module implements Template:Languagename. It returns the human-readable name of a language given its language code. It is a Lua port of the original template.
Usage
From wikitext
{{#invoke:Languagename|main|<code>|<lang>}}
Or, more commonly, via the wrapper template:
{{Languagename|<code>|<lang>}}
From another module
local languagename = require( 'Module:Languagename' )
local name = languagename._main( code, lang )
Parameters
| Parameter | Position | Required | Description |
|---|---|---|---|
code |
1 |
Yes | The language code to look up (e.g. fr, de, nl). Case-insensitive; it is lower-cased internally.
|
lang |
2 |
No | The language in which to render the name. If omitted, the language's autonym (its name in its own language) is returned. Case-insensitive. |
Return value
The module resolves the language name in the following order:
- If
codeis empty, an empty string is returned. - If the code is a valid built-in MediaWiki language code, the localised name from
mw.language.fetchLanguageNameis returned (the autonym whenlangis omitted). - Otherwise, a fallback lookup is performed in Module:Languagename/data.json. The autonym is returned when
langis omitted or equals the code; otherwise the English name is returned. - If the code is a valid built-in code but no name was found, the BCP 47 form of the code is returned.
- For an unknown, invalid code, a "Missing language code" error is produced.
Examples
| Wikitext | Expected | Result |
|---|---|---|
{{#invoke:Languagename|main|fr}} |
français | français |
{{#invoke:Languagename|main|fr|en}} |
French | French |
{{#invoke:Languagename|main|de|nl}} |
Duits | Duits |
{{#invoke:Languagename|main|nl}} |
Nederlands | Nederlands |
{{#invoke:Languagename|main|}} |
(empty string) |
Error handling
If an unknown language code that is neither a valid built-in code nor present in Module:Languagename/data.json is supplied, the module raises an error of the form:
Missing language code "xx"
Codes that are not built into MediaWiki but should still be supported can be added to Module:Languagename/data.json with autonym and en fields.
Dependencies
- Module:Languagename/data.json — fallback data for codes not built into MediaWiki.
See also
- Template:Languagename
- Help:Magic words — for the built-in
{{#language:}}parser function.
--
-- Port [[Template:Languagename]] to Lua
--
require( 'strict' )
local p = {}
local function isEmpty( str )
return str == nil or str == ''
end
function p._main( code, lang )
if isEmpty( code ) then
return ''
end
-- mw.language only accepts codes in lowercase
code = code:lower()
if not isEmpty( lang ) then
lang = lang:lower()
end
local mwLang = mw.language.new( code )
local mwCode = mwLang:getCode()
if mw.language.isValidBuiltInCode( mwCode ) then
-- If lang is nil, returns autonym, otherwise tries in language provided
local localisedName = mw.language.fetchLanguageName( mwCode, lang )
if not isEmpty( localisedName ) then
return localisedName
end
end
-- [[Module:Languagename/data.json]]
local data = mw.loadJsonData( 'Module:Languagename/data.json' )
if data[ mwCode ] ~= nil then
if not isEmpty( lang ) and lang ~= mwCode then
return data[ mwCode ][ 'en' ] or data[ mwCode ][ 'autonym' ]
end
return data[ mwCode ][ 'autonym' ]
end
-- Return an error only for invalid codes
if mw.language.isValidBuiltInCode( mwCode ) then
return mwLang:toBcp47Code()
end
return error( 'Missing language code "' .. mwLang:toBcp47Code() .. '"' )
end
function p.main( frame )
local getArgs = require( 'Module:Arguments' ).getArgs
local args = getArgs( frame )
local code = args[ 1 ]
local lang = args[ 2 ]
return p._main( code, lang )
end
return p