- The following documentation is located at Module:languages/templates/documentation. [edit]
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
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}}(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:
To request its family:
{{#invoke:languages/templates|getByCode|en|getFamily}}
- Result:
gmw-ang
getByCanonicalName
{{#invoke:languages/templates|getByCanonicalName|language name}}
Gets the language code corresponding to a canonical name.
{{#invoke:languages/templates|getByCanonicalName|English}}- ↓
- en
getCanonicalName
{{#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
- Module:JSON data — for exporting all the data at once
localexport={} localconcat=table.concat localinsert=table.insert localsort=table.sort localscripts_module="Module:scripts" localfunctionget_script_by_code(...) get_script_by_code=require(scripts_module).getByCode returnget_script_by_code(...) end functionexport.exists(frame) returnrequire("Module:languages").getByCode( require("Module:parameters").process(frame.args,{ [1]={required=true} })[1] )and"1"or"" end do localfunctiongetByCode(frame,allow_etym) localplain=true localargs=require("Module:parameters").process(frame.args,{ [1]={required=true,type=allow_etymand"language"or"full language"}, [2]={required=true}, [3]=plain, [4]=plain, [5]=plain, }) localfunctioncheck_empty(...) localvarargs={...} fori=1,select("#",...)do ifargs[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 returnrequire("Module:language-like").templateGetByCode(args, function(itemname) locallist ifitemname=="getWikimediaLanguages"then list=args[1]:getWikimediaLanguages() elseifitemname=="getScripts"then list=args[1]:getScriptCodes() elseifitemname=="getAncestors"then list=args[1]:getAncestors() end iflistthen check_empty(4,5) localretval=list[tonumber(args[3])orerror("Please specify the numeric index of the desired item in 3=.")] ifretvalthen iftype(retval)=="string"then returnretval else returnretval:getCode() end else return"" end end ifitemname=="transliterate"then localsc=get_script_by_code(args[4]) return(args[1]:transliterate(args[3],sc,args[5]))or"" elseifitemname=="makeDisplayText"then localsc=get_script_by_code(args[4]) check_empty(5) return(args[1]:makeDisplayText(args[3],sc))or"" elseifitemname=="makeEntryName"then -- FIXME, find places that use makeEntryName and convert to stripDiacritics localsc=get_script_by_code(args[4]) check_empty(5) returnargs[1]:makeEntryName(args[3],sc)or"" elseifitemname=="stripDiacritics"then localsc=get_script_by_code(args[4]) check_empty(5) returnargs[1]:stripDiacritics(args[3],sc)or"" elseifitemname=="makeSortKey"then localsc=get_script_by_code(args[4]) check_empty(5) return(args[1]:makeSortKey(args[3],sc))or"" elseifitemname=="logicalToPhysical"then check_empty(4,5) returnargs[1]:logicalToPhysical(args[3])or"" elseifitemname=="countCharacters"then localsc=get_script_by_code(args[4]) check_empty(5) returnsc:countCharacters(args[3]or"") elseifitemname=="findBestScript"then check_empty(5) returnargs[1]:findBestScript(args[3]or"",args[4]):getCode() end end ) end -- Used by the following JS: -- * [[WT:ACCEL]] -- * [[WT:EDIT]] -- * [[WT:NEC]] functionexport.getByCode(frame) returngetByCode(frame,false) end functionexport.getByCodeAllowEtym(frame) returngetByCode(frame,true) end end functionexport.getByCanonicalName(frame) returnrequire("Module:parameters").process(frame.args,{ [1]={required=true,type="language",method="name"} })[1]:getCode()or"" end functionexport.getCanonicalName(frame) localargs=require("Module:parameters").process( require("Module:yesno")(frame.args.parent)andframe:getParent().argsorframe.args, { [1]={required=true}, ["return_if_invalid"]={type="boolean"}, } ) locallang=require("Module:languages").getByCode(args[1],nil,true) returnlangandlang:getCanonicalName()ornotargs.return_if_invalidand""orargs[1] end functionexport.getFull(frame) localargs=require("Module:parameters").process( require("Module:yesno")(frame.args.parent)andframe:getParent().argsorframe.args, { [1]={required=true,type="language"}, } ) returnargs[1]:getFullCode() end functionexport.getChildren(frame) localargs=require("Module:parameters").process( require("Module:yesno")(frame.args.parent)andframe:getParent().argsorframe.args, { [1]={required=true,type="language"}, } ) localchildren=args[1]:getChildren() sort(children,function(a,b) returna:getCanonicalName()<b:getCanonicalName() end) locallist={} for_,childinipairs(children)do insert(list,"* "..child:makeWikipediaLink()..": ".."<code>"..child:getCode().."</code>") end returnconcat(list,"\n") end returnexport
