ഘടകം:language utilities
This module provides access to Module:languages and Module:families from templates, so that they can make use of the information stored there.
Exported functions
തിരുത്തുകlookup_language
തിരുത്തുക{{#invoke:language utilities|lookup_language|(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. If it is missing or does not exist, the result will be a script error.
- The item is the name of one of the pieces of data that is stored for a language, such as
names
orscripts
. If no item has been provided, the result will be a script error. However, if the name of a nonexistent item is given, the result will be an empty string. This allows you to check for the presence or absence of an item without triggering errors. - The index is optional, and is used for items that are lists, such as
names
orscripts
. It selects which item in the list to return. On items that are single strings, likefamily
, it has no effect. If no index is given, the default will be 1 (the first subitem). 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 default (canonical) name of the language whose code is en
:
{{#invoke:language utilities|lookup_language|en|names}}
- Result:
English
To request its second name, if any:
{{#invoke:language utilities|lookup_language|en|names|2}}
- Result:
Modern English
To request its family:
{{#invoke:language utilities|lookup_language|en|family}}
- Result:
gmw
lookup_family
തിരുത്തുക{{#invoke:language utilities|lookup_family|(language family code)|(item to look up)|(index)}}
Analogous to lookup_language
, but for language families.
language_exists
തിരുത്തുക{{#invoke:language utilities|language_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.
family_exists
തിരുത്തുക{{#invoke:language utilities|family_exists|(language family code)}}
Analogous to language_exists
, but for language families.
See also
തിരുത്തുക- Module:JSON data — for exporting all the data at once
local export = {}
-- Look up an item from the table of language data, and return it.
-- This function allows templates to access this data.
-- Returns an empty string if the item does not exist.
function export.lookup_language(frame)
local languages = mw.loadData("Module:languages/alldata")
local args = frame.args
local lang = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
local langinfo = languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
-- The item that the caller wanted to look up
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 item = langinfo[itemname] or ""
if type(item) == "table" then
return item[tonumber(args[3] or 1)] or ""
else
return item or ""
end
end
function export.lookup_family(frame)
local families = mw.loadData("Module:families/tempdata")
local args = frame.args
local fam = args[1] or error("Family code has not been specified. Please pass parameter 1 to the module invocation.")
local faminfo = families[fam] or error("The family code \"" .. fam .. "\" is not valid.")
-- The item that the caller wanted to look up
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 item = faminfo[itemname] or ""
if type(item) == "table" then
return item[tonumber(args[3] or 1)] or ""
else
return item or ""
end
end
function export.language_exists(frame)
local languages = mw.loadData("Module:languages/alldata")
local args = frame.args
local lang = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
if languages[lang] then
return "1"
else
return ""
end
end
function export.family_exists(frame)
local families = mw.loadData("Module:families/tempdata")
local args = frame.args
local fam = args[1] or error("Family code has not been specified. Please pass parameter 1 to the module invocation.")
if families[fam] then
return "1"
else
return ""
end
end
return export