Module:languages/templates
This module provides access to Module:languages from templates, so that they can make use of the information stored there.
Usage
[កែប្រែ]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
[កែប្រែ]exists
[កែប្រែ]{{#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
[កែប្រែ]{{#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
getCanonicalNameorgetScripts. 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
getOtherNamesorgetScripts. 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:
English
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:languages/data/2/extra' not found.
To request its family:
{{#invoke:languages/templates|getByCode|en|getFamily}}
- Result:
gmw
getByCanonicalName
[កែប្រែ]{{#invoke:languages/templates|getByCanonicalName|language name}}
Gets the language code corresponding to a canonical name.
{{#invoke:languages/templates|getByCanonicalName|English}}- ↓
- en
getByName
[កែប្រែ]{{#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}}- ↓
- Script error: The function "getByName" does not exist.
getCanonicalName
[កែប្រែ]{{#invoke:languages/templates|getCanonicalName|language code}}
Gets the canonical name for a language code. 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.
As with getByCode, an invalid language code will yield a script error.
{{#invoke:languages/templates|getCanonicalName|en}}- ↓
English
See also
[កែប្រែ]- Module:JSON data — for exporting all the data at once
local export = {}
local concat = table.concat
local insert = table.insert
local sort = table.sort
local scripts_module = "Module:scripts"
local function get_script_by_code(...)
get_script_by_code = require(scripts_module).getByCode
return get_script_by_code(...)
end
function export.exists(frame)
return require("Module:languages").getByCode(
require("Module:parameters").process(frame.args, {
[1] = {required = true}
})[1]
) and "1" or ""
end
do
local function getByCode(frame, allow_etym)
local plain = true
local args = require("Module:parameters").process(frame.args, {
[1] = {required = true, type = allow_etym and "language" or "full language"},
[2] = {required = true},
[3] = plain,
[4] = plain,
[5] = plain,
})
local function check_empty(...)
local varargs = {...}
for i = 1, select("#", ...) do
if args[varargs[i]] then
error(("Cannot specify a value for argument %s= when using subfunction '%s' of getByCode()"):format(
varargs[i], args[2]))
end
end
end
return require("Module:language-like").templateGetByCode(args,
function(itemname)
local list
if itemname == "getWikimediaLanguages" then
list = args[1]:getWikimediaLanguages()
elseif itemname == "getScripts" then
list = args[1]:getScriptCodes()
elseif itemname == "getAncestors" then
list = args[1]:getAncestors()
end
if list then
check_empty(4, 5)
local retval = list[tonumber(args[3]) or error("Please specify the numeric index of the desired item in 3=.")]
if retval then
if type(retval) == "string" then
return retval
else
return retval:getCode()
end
else
return ""
end
end
if itemname == "transliterate" then
local sc = get_script_by_code(args[4])
return (args[1]:transliterate(args[3], sc, args[5])) or ""
elseif itemname == "makeDisplayText" then
local sc = get_script_by_code(args[4])
check_empty(5)
return (args[1]:makeDisplayText(args[3], sc)) or ""
elseif itemname == "makeEntryName" then
-- FIXME, find places that use makeEntryName and convert to stripDiacritics
local sc = get_script_by_code(args[4])
check_empty(5)
return args[1]:makeEntryName(args[3], sc) or ""
elseif itemname == "stripDiacritics" then
local sc = get_script_by_code(args[4])
check_empty(5)
return args[1]:stripDiacritics(args[3], sc) or ""
elseif itemname == "makeSortKey" then
local sc = get_script_by_code(args[4])
check_empty(5)
return (args[1]:makeSortKey(args[3], sc)) or ""
elseif itemname == "logicalToPhysical" then
check_empty(4, 5)
return args[1]:logicalToPhysical(args[3]) or ""
elseif itemname == "countCharacters" then
local sc = get_script_by_code(args[4])
check_empty(5)
return sc:countCharacters(args[3] or "")
elseif itemname == "findBestScript" then
check_empty(5)
return args[1]:findBestScript(args[3] or "", args[4]):getCode()
end
end
)
end
-- Used by the following JS:
-- * [[WT:ACCEL]]
-- * [[WT:EDIT]]
-- * [[WT:NEC]]
function export.getByCode(frame)
return getByCode(frame, false)
end
function export.getByCodeAllowEtym(frame)
return getByCode(frame, true)
end
end
function export.getByCanonicalName(frame)
return require("Module:parameters").process(frame.args, {
[1] = {required = true, type = "language", method = "name"}
})[1]:getCode() or ""
end
function export.getCanonicalName(frame)
local args = require("Module:parameters").process(
require("Module:yesno")(frame.args.parent) and frame:getParent().args or frame.args,
{
[1] = {required = true},
["return_if_invalid"] = {type = "boolean"},
}
)
local lang = require("Module:languages").getByCode(args[1], nil, true)
return lang and lang:getCanonicalName() or not args.return_if_invalid and "" or args[1]
end
function export.getFull(frame)
local args = require("Module:parameters").process(
require("Module:yesno")(frame.args.parent) and frame:getParent().args or frame.args,
{
[1] = {required = true, type = "language"},
}
)
return args[1]:getFullCode()
end
function export.getChildren(frame)
local args = require("Module:parameters").process(
require("Module:yesno")(frame.args.parent) and frame:getParent().args or frame.args,
{
[1] = {required = true, type = "language"},
}
)
local children = args[1]:getChildren()
sort(children, function(a, b)
return a:getCanonicalName() < b:getCanonicalName()
end)
local list = {}
for _, child in ipairs(children) do
insert(list, "* " .. child:makeWikipediaLink() .. ": " .. "<code>" .. child:getCode() .. "</code>")
end
return concat(list, "\n")
end
return export