- The following documentation is located at Module:wikimedia languages/documentation. [edit]
- Useful links: subpage list • links • transclusions • testcases • sandbox
This module is used to retrieve and manage Wikimedia languages.
Until phab:T10217 is fixed there is a mis-match between the codes used by ISO 639, and those used by Wikimedia, for certain languages. There are also some languages that are recognised as distinct by Wikimedia, but are combined into other languages on Wiktionary (see Wiktionary:Language treatment). This module aids in mapping between the two.
The information itself is stored in Module:wikimedia languages/data. This modules should not be used directly by any other module, the data should only be accessed through the functions provided by Module:wikimedia languages.
Finding and retrieving languages
The module exports a number of functions that are used to find languages.
getByCode
getByCode(code)
Finds the Wikimedia language whose code matches the one provided. If it exists, it returns a WikimediaLanguage object representing the language. Otherwise, it returns nil.
getByCodeWithFallback
getByCodeWithFallback(code)
This does the same as getByCode. However, if that function returns nil, then the regular language with that code is retrieved, if it exists. The function then returns the first element of the list returned by a call to the :getWikimediaLanguages() method of that language. If that method returns no elements, the function returns nil.
Language objects' getWikimediaLanguages method
The :getWikimediaLanguages() method is available on regular Language objects (as returned by Module:languages). This method returns WikimediaLanguage objects that represent that language outside Wiktionary.
Comparison
The differences between the various ways of retrieving languages can be seen here, using some examples.
| Code | Module:languages.getByCode
|
Module:languages.getByCode():getWikimediaLanguages()
|
.getByCode
|
.getByCodeWithFallback
|
Notes |
|---|---|---|---|---|---|
fr
|
fr/French
|
fr/French
|
fr/French
|
fr/French
|
Code used by both Wikimedia and Wiktionary. |
bs
|
nil
|
(error) | bs/Bosnian
|
bs/Bosnian
|
Code used only by Wikimedia, not Wiktionary. |
sh
|
sh/Serbo-Croatian
|
sh/Serbo-Croatianbs/Bosnianhr/Croatiansr/Serbian
|
sh/Serbo-Croatian
|
sh/Serbo-Croatian
|
Code used by both Wikimedia and Wiktionary. returns sh as that is a valid Wikimedia code.:getWikimediaLanguages() maps the code to multiple possible Wikimedia codes.
|
cmn
|
cmn/Mandarin
|
zh/Chinese
|
nil
|
zh/Chinese
|
Code is used only by Wiktionary, not Wikimedia. The code is mapped by :getWikimediaLanguages() to the equivalent Wikimedia code zh.
|
wym
|
wym/Vilamovian
|
nil
|
nil
|
nil
|
Code is used only by Wiktionary, not Wikimedia. The code is not mapped onto another by :getWikimediaLanguages(), so nil is returned.
|
WikimediaLanguage objects
A WikimediaLanguage object is returned from one of the functions above. It is a Lua representation of a Wikimedia language and the data associated with it. It has a number of methods that can be called on it, using the : syntax. For example:
localm_wmlanguages=require("Module:wikimedia languages") locallang=m_wmlanguages.getByCode("bs") localname=lang:getCanonicalName() -- "name" will now be "Bosnian"
Language:getCode
:getCode()
Returns the language code of the language. Example: "fr" for French.
Language:getCanonicalName
:getCanonicalName()
Returns the canonical name of the language. This is the name used to represent that language on Wiktionary. Example: "French" for French.
Language:getAllNames
:getAllNames()
Returns a table of all names that the language is known by, including the canonical name. The names are not guaranteed to be unique, sometimes more than one language is known by the same name. Example: {"French", "Modern French"} for French.
Language:getType
:getType()
Returns "Wikimedia".
Language:getWiktionaryLanguage
:getWiktionaryLanguage()
Returns a Language object (see Module:languages) that represents the Wiktionary-native language that is equivalent to this Wikimedia language. In most cases, this will be the same code and name as the original Wikimedia language, but a few of them differ.
Note that unlike the :getWikimediaLanguages method in on Language objects, this only returns a single object. This is done so that the application of tags and script formatting is unambiguous.
Language:getData
:getData()
- This function is not for use in entries or other content pages.
Returns a blob of data about the language. The format of this blob is undocumented, and perhaps unstable; it's intended for things like the module's own unit-tests, which are "close friends" with the module and will be kept up-to-date as the format changes.
localexport={} locallanguages_module="Module:languages" locallanguage_like_module="Module:language-like" localload_module="Module:load" localwm_languages_data_module="Module:wikimedia languages/data" localget_by_code-- Defined below. localgmatch=string.gmatch localis_known_language_tag=mw.language.isKnownLanguageTag localmake_object-- Defined below. localrequire=require localsetmetatable=setmetatable localtype=type --[==[ Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==] localfunctionget_lang(...) get_lang=require(languages_module).getByCode returnget_lang(...) end localfunctionget_lang_data_module_name(...) get_lang_data_module_name=require(languages_module).getDataModuleName returnget_lang_data_module_name(...) end localfunctionload_data(...) load_data=require(load_module).load_data returnload_data(...) end --[==[ Loaders for objects, which load data (or some other object) into some variable, which can then be accessed as "foo or get_foo()", where the function get_foo sets the object to "foo" and then returns it. This ensures they are only loaded when needed, and avoids the need to check for the existence of the object each time, since once "foo" has been set, "get_foo" will not be called again.]==] localwm_languages_data localfunctionget_wm_languages_data() wm_languages_data,get_wm_languages_data=load_data(wm_languages_data_module),nil returnwm_languages_data end localWikimediaLanguage={} WikimediaLanguage.__index=WikimediaLanguage functionWikimediaLanguage:getCode() returnself._code end functionWikimediaLanguage:getCanonicalName() returnself._data[1] end --function WikimediaLanguage:getAllNames() -- return self._data.names --end --[==[Returns a table of types as a lookup table (with the types as keys). Currently, the only possible type is {Wikimedia language}.]==] functionWikimediaLanguage:getTypes() localtypes=self._types iftypes==nilthen types={["Wikimedia language"]=true} localrawtypes=self._data.type ifrawtypesthen fortingmatch(rawtypes,"[^,]+")do types[t]=true end end self._types=types end returntypes end --[==[Given a list of types as strings, returns true if the Wikimedia language has all of them.]==] functionWikimediaLanguage:hasType(...) WikimediaLanguage.hasType=require(language_like_module).hasType returnself:hasType(...) end functionWikimediaLanguage:getWiktionaryLanguage() localobject=self._wiktionaryLanguageObject ifobject==nilthen object=get_lang(self._data.wiktionary_code,nil,"allow etym") self._wiktionaryLanguageObject=object end returnobject end -- Do NOT use this method! -- All uses should be pre-approved on the talk page! functionWikimediaLanguage:getData() returnself._data end --[==[Returns the name of the module containing the Wikimedia language's data (if any). Currently, this is always [[Module:wikimedia languages/data]].]==] functionWikimediaLanguage:getDataModuleName() returnwm_languages_data_module end functionexport.makeObject(code,data) localdata_type=type(data) ifdata_type~="table"then error(("bad argument #2 to 'makeObject' (table expected, got %s)"):format(data_type)) end returnsetmetatable({_data=data,_code=code},WikimediaLanguage) end make_object=export.makeObject functionexport.getByCode(code) -- Only accept codes the software recognises. ifnotis_known_language_tag(code)then returnnil end localdata=(wm_languages_dataorget_wm_languages_data())[code] -- If there is no specific Wikimedia code, then "borrow" the information -- from the general Wiktionary language code. localname,wiktionary_code ifdata~=nilthen name,wiktionary_code=data[1],data.wiktionary_code ifnot(name==nilorwiktionary_code==nil)then returnmake_object(code,data) end end -- Get the associated Wiktionary language, using the wiktionary_code key or -- else the input code. ifwiktionary_code==nilthen wiktionary_code=code end locallang=get_lang(wiktionary_code,nil,"allow etym","allow family") iflang~=nilthen returnmake_object(code,{ name==nilandlang:getCanonicalName()orname, wiktionary_code=wiktionary_code, }) end -- If there's no Wiktionary language for the relevant code, throw an error. -- This should never happen. localmsg,arg3 ifdata==nilthen msg="code '%s' is a valid Wikimedia language code, but there is no corresponding data in [[%s]], [[%s]] or [[Module:families/data]]" elseifwiktionary_code~=codethen msg="code '%s' is a valid Wikimedia language code and has data in [[%s]], but its 'wiktionary_code' key '%s' is not valid" arg3=wiktionary_code else msg="code '%s' is a valid Wikimedia language code and has data in [[%s]], but no corresponding data in [[%s]] or [[Module:families/data]]" end error(msg:format(code,wm_languages_data_module,arg3orget_lang_data_module_name(code))) end get_by_code=export.getByCode functionexport.getByCodeWithFallback(code) localobject=get_by_code(code) ifobject~=nilthen returnobject end locallang=get_lang(code,nil,"allow etym") returnlang~=nilandlang:getWikimediaLanguages()[1]ornil end returnexport
