Module:languages/templates
This module provides access to Module:languages from templates, so that they can make use of the information stored there.
Usage
[jhyuk]If you know a language's code (for example, "en") and you want to find out its canonical name, you can use this:
{{#invoke:languages/templates|getByCode|en|getCanonicalName}}
(returns "English")
If you know a language's canonical name (for example, "English") and you want to find out its code, use this:
{{#invoke:languages/templates|getByCanonicalName|English|getCode}}
(returns "en")
Both of these functions are subst:able (type {{subst:#invoke:
...).
Exported functions
[jhyuk]exists
[jhyuk]{{#invoke:languages/templates|exists|language code}}
Check whether a language code exists and is valid. It will return "1" if the language code exists, and the empty string "" if it does not.
This is rarely needed, because a script error will result when someone uses a code that is not valid, so you do not need this just to check for errors. However, in case you need to decide different actions based on whether a certain parameter is a language code or something else, this function can be useful.
getByCode
[jhyuk]{{#invoke:languages/templates|getByCode|language code|item to look up|index}}
Queries information about a language code.
- The language code should be one of the codes that is defined in Module:languages data. If it is missing or does not exist, the result will be a script error.
- The item is the name of one of the functions of a language object, such as
getCanonicalName
orgetScripts
. If no item has been provided, the result will be a script error. - The index is optional, and is used for items that are lists, such as
getOtherNames
orgetScripts
. It selects which item in the list to return. On items that are single strings, likegetFamily
, it has no effect. If an index is given that is higher than the number of items in the list, the result will be an empty string.
For example, to request the canonical name of the language whose code is en
:
{{#invoke:languages/templates|getByCode|en|getCanonicalName}}
- Result:
Lua error in package.lua at line 80: module 'Module:language-like' not found.
To request its second name, if any:
{{#invoke:languages/templates|getByCode|en|getOtherNames|1}}
- Result:
Lua error in package.lua at line 80: module 'Module:language-like' not found.
To request its family:
{{#invoke:languages/templates|getByCode|en|getFamily}}
- Result:
Lua error in package.lua at line 80: module 'Module:language-like' not found.
getByCanonicalName
[jhyuk]{{#invoke:languages/templates|getByCanonicalName|language name}}
Gets the language code corresponding to a canonical name.
{{#invoke:languages/templates|getByCanonicalName|English}}
- ↓
- en
getByName
[jhyuk]{{#invoke:languages/templates|getByName|language name}}
Like the above, except it will also accept other names for the language that are listed in the language's otherNames
field. For instance:
{{#invoke:languages/templates|getByName|Modern English}}
- ↓
- Lua error in package.lua at line 80: module 'Module:languages/by name' not found.
makeEntryName
[jhyuk]{{#invoke:languages/templates|makeEntryName|language code|display name}}
Gets the entry name corresponding to the display name of a link. The entry name is the display name with language-specific diacritics removed, and is the actual name of the page that is normally displayed in a link using the display name. For example, the display name of Ta̱mpi̱let:m+ is amo, without the macron over the o, and the display name of Ta̱mpi̱let:m+ is школа, without the acute accent marking the stress.
{{#invoke:languages/templates|makeEntryName|la|amō}}
- ↓
- amo
getCanonicalName
[jhyuk]{{#invoke:languages/templates|getCanonicalName|language code}}
Gets the canonical name for a language code if the language code is valid, or else returns an empty string. It uses a table that converts language code to canonical name, generated by Module:languages/code to canonical name. Requires more Lua memory than getByCode
(about 10 megabytes) for a single instance, but may require less memory on pages that call the function many times.
Unlike {{#invoke:languages/templates|getByCode|<language_code>|getCanonicalName}}
, this function does does not yield a script error for an invalid language code.
{{#invoke:languages/templates|getCanonicalName|en}}
{{#invoke:languages/templates|getCanonicalName|invalid code}}
- ↓
English
See also
[jhyuk]- Module:JSON data — for exporting all the data at once
local export = {}
function export.exists(frame)
local args = frame.args
local lang = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
lang = require("Module:languages").getByCode(lang)
if lang then
return "1"
else
return ""
end
end
-- Used by the following JS:
-- * [[WT:ACCEL]]
-- * [[WT:EDIT]]
-- * [[WT:NEC]]
function export.getByCode(frame)
local iparams = {
[1] = {required = true},
[2] = {required = true},
[3] = {},
[4] = {},
[5] = {},
}
local iargs = require("Module:parameters").process(frame.args, iparams, nil, "languages/templates", "getByCode")
local langcode = iargs[1]
local lang = require("Module:languages").getByCode(langcode, true)
return require("Module:language-like").templateGetByCode(lang, iargs,
function(itemname)
local list
if itemname == "getWikimediaLanguages" then
list = lang:getWikimediaLanguages()
elseif itemname == "getScripts" then
list = lang:getScriptCodes()
elseif itemname == "getAncestors" then
list = lang:getAncestors()
end
if list then
local index = iargs[3]
index = tonumber(index) or error("Please specify the numeric index of the desired item.")
local retval = list[index]
if retval then
if type(retval) == "string" then
return retval
else
return retval:getCode()
end
else
return ""
end
end
if itemname == "transliterate" then
local text = iargs[3]
local module_override = iargs[5]
local sc = require("Module:scripts").getByCode(iargs[4], 4)
return (lang:transliterate(text, sc, module_override)) or ""
elseif itemname == "makeDisplayText" then
local text = iargs[3]
local sc = require("Module:scripts").getByCode(iargs[4], 4)
return (lang:makeDisplayText(text, sc)) or ""
elseif itemname == "makeEntryName" then
local text = iargs[3]
local sc = require("Module:scripts").getByCode(iargs[4], 4)
return (lang:makeEntryName(text, sc)) or ""
elseif itemname == "makeSortKey" then
local text = iargs[3]
local sc = require("Module:scripts").getByCode(iargs[4], 4)
return (lang:makeSortKey(text, sc)) or ""
elseif itemname == "countCharacters" then
local text = iargs[3] or ""
local sc = require("Module:scripts").getByCode(iargs[4], 4, "disallow nil")
return sc:countCharacters(text)
elseif itemname == "findBestScript" then
local text = iargs[3] or ""
return lang:findBestScript(text):getCode()
end
end
)
end
function export.getByCanonicalName(frame)
local args = frame.args
local langname = args[1] or error("Language name has not been specified. Please pass parameter 1 to the module invocation.")
local lang = require("Module:languages").getByCanonicalName(langname)
if lang then
return lang:getCode()
else
return ""
end
end
function export.getByName(frame)
local args = frame.args
local langname = args[1] or error("Language name has not been specified. Please pass parameter 1 to the module invocation.")
local lang = require("Module:languages").getByName(langname)
if lang then
return lang:getCode()
else
return ""
end
end
function export.makeEntryName(frame)
local args = frame.args
local langname = args[1] or error("Language name has not been specified. Please pass parameter 1 to the module invocation.")
local lang = require("Module:languages").getByCode(langname)
if lang then
return lang:makeEntryName(args[2])
else
return ""
end
end
function export.getCanonicalName(frame)
local langCode, args
if require("Module:yesno")(frame.args.parent) then
args = frame:getParent().args
else
args = frame.args
end
langCode = args[1]
if not langCode or langCode == "" then
error("Supply a language code in parameter 1.")
end
return mw.loadData("Module:languages/code to canonical name")[langCode]
or not args.return_if_invalid and "" or langCode
end
function export.getNonEtymological(frame)
local langCode, args
if require("Module:yesno")(frame.args.parent) then
args = frame:getParent().args
else
args = frame.args
end
langCode = args[1]
if not langCode or langCode == "" then
error("Supply a language code in parameter 1.")
end
local lang = require("Module:languages").getByCode(langCode, nil, true)
local nonEtym_lang = lang:getNonEtymological()
return nonEtym_lang:getCode()
or not args.return_if_invalid and "" or langCode
end
return export