Module:Wt/haw/languages/templates
This module provides access to Module:Wt/haw/languages from templates, so that they can make use of the information stored there.
Usage
editIf you know a language's code (for example, "en") and you want to find out its canonical name, you can use this:
{{#invoke:Wt/haw/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:Wt/haw/languages/templates|getByCanonicalName|English|getCode}}
(returns "en")
Both of these functions are subst:able (type {{subst:#invoke:
...).
Exported functions
editexists
edit{{#invoke:Wt/haw/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
edit{{#invoke:Wt/haw/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:Wt/haw/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:Wt/haw/languages/templates|getByCode|en|getCanonicalName}}
- Result:
Lua error at line 88: The function "getCanonicalName" did not return a string value..
To request its second name, if any:
{{#invoke:Wt/haw/languages/templates|getByCode|en|getOtherNames|1}}
- Result:
To request its family:
{{#invoke:Wt/haw/languages/templates|getByCode|en|getFamily}}
- Result:
Lua error at line 39: attempt to index a nil value.
getByCanonicalName
edit{{#invoke:Wt/haw/languages/templates|getByCanonicalName|language name}}
Gets the language code corresponding to a canonical name.
{{#invoke:Wt/haw/languages/templates|getByCanonicalName|English}}
- ↓
- Lua error in package.lua at line 80: module 'Module:Wt/haw/languages/canonical names' not found.
getByName
edit{{#invoke:Wt/haw/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:Wt/haw/languages/templates|getByName|Modern English}}
- ↓
- Lua error in Module:Wt/haw/languages/by_name at line 5: table index is nil.
getCanonicalName
edit{{#invoke:Wt/haw/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:Wt/haw/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:Wt/haw/languages/templates|getCanonicalName|en}}
- ↓
Lua error in package.lua at line 80: module 'Module:Wt/haw/yesno' not found.
See also
edit- Module:Wt/haw/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:Wt/haw/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 args = frame.args
local langcode = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
local itemname = args[2] or error("Type of information to look up has not been specified. Please pass parameter 2 to the module invocation.")
local lang = require("Module:Wt/haw/languages").getByCode(langcode)
if not lang then
error("The language code '" .. langcode .. "' is not valid.")
end
-- The item that the caller wanted to look up
if itemname == "getOtherNames" then
local index = args[3]; if index == "" then index = nil end
index = tonumber(index) or error("Please specify the numeric index of the desired name.")
return lang:getOtherNames()[index] or ""
elseif itemname == "getFamily" then
return lang:getFamily():getCode()
elseif itemname == "getWikimediaLanguages" then
local index = args[3]; if index == "" then index = nil end
index = tonumber(index) or error("Please specify the numeric index of the desired language.")
local langs = lang:getWikimediaLanguages()
if langs[index] then
return langs[index]:getCode()
else
return ""
end
elseif itemname == "getScripts" then
local index = args[3]; if index == "" then index = nil end
index = tonumber(index) or error("Please specify the numeric index of the desired script.")
local scripts = lang:getScriptCodes()
if scripts[index] then
return scripts[index]
else
return ""
end
elseif itemname == "getAncestors" then
local index = args[3]; if index == "" then index = nil end
index = tonumber(index) or error("Please specify the numeric index of the desired ancestor.")
local ancestors = lang:getAncestors()
if ancestors[index] then
return ancestors[index]:getCode()
else
return ""
end
elseif itemname == "transliterate" then
local text = args[3]; if text == "" then text = nil end
local sc = args[4]; if sc == "" then sc = nil end
local module_override = args[5]; if module_override == "" then module_override = nil end
sc = (sc and (require("Module:Wt/haw/scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
return lang:transliterate(text, sc, module_override) or ""
elseif itemname == "makeEntryName" then
local text = args[3]; if text == "" then text = nil end
return lang:makeEntryName(text) or ""
elseif itemname == "makeSortKey" then
local text = args[3]; if text == "" then text = nil end
return lang:makeSortKey(text) or ""
elseif lang[itemname] then
local ret = lang[itemname](lang)
if type(ret) == "string" then
return ret
else
error("The function \"" .. itemname .. "\" did not return a string value.")
end
else
error("Requested invalid item name \"" .. itemname .. "\".")
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:Wt/haw/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:Wt/haw/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:Wt/haw/languages").getByCode(langname)
if lang then
return lang:makeEntryName(args[2])
else
return ""
end
end
function export.getCanonicalName(frame)
local langCode
if require("Module:Wt/haw/yesno")(frame.args.parent) then
langCode = frame:getParent().args[1]
else
langCode = frame.args[1]
end
if not langCode or langCode == "" then
error("Supply a language code in parameter 1.")
end
return mw.loadData("Module:Wt/haw/languages/code to canonical_name")[langCode] or ""
end
return export