Модуль:wikimedia languages
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
[түзөтүү]Тилдерди издөөнүн ар кандай жолдорунун ортосундагы айырмачылыктарды кээ бир мисалдар менен бул жерден көрүүгө болот.
Code | Module:languages.getByCode
|
Module:languages.getByCode():getWikimediaLanguages()
|
Module:wikimedia languages.getByCode
|
Module:wikimedia languages.getByCodeWithFallback
|
Notes |
---|---|---|---|---|---|
fr
|
fr /Франсузча
|
fr /Франсузча
|
fr /Франсузча
|
|
Код Викимедиа жана Уикисөздүк тарабынан колдонулат. |
bs
|
nil
|
(error) | bs /Bosnian
|
bs /Bosnian
|
Код Викимедиа жана Уикисөздүк тарабынан колдонулат. |
sh
|
sh /Сербче-Хорватча
|
sh /Сербче-Хорватчаbs /Бошнакчаhr /Хорватчаsr /Сербче
|
sh /Сербче-Хорватча
|
sh /Сербче-Хорватча
|
Код Викимедиа жана Уикисөздүк тарабынан колдонулат. Module:wikimedia languages кайтарат sh анткени бул жарактуу Wikimedia коду.
|
cmn
|
cmn/Мандарин
|
zh /Кытайча
|
nil
|
zh /Кытайча
|
Код Викимедиа тарабынан эмес, Wiktionary тарабынан гана колдонулат. Код :getWikimediaLanguages() тарабынан эквиваленттүү Wikimedia кодун zh менен салыштырылган.
|
wym
|
wym /Виламовианче
|
nil
|
nil
|
nil
|
Код Викимедиа тарабынан эмес, Wiktionary тарабынан гана колдонулат. Код :getWikimediaLanguages() тарабынан башка код менен салыштырылган эмес, андыктан nil кайтарылып берилет.
|
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:
local m_wmlanguages = require("Module:wikimedia languages")
local lang = m_wmlanguages.getByCode("bs")
local name = 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:getRawData
[түзөтүү]:getRawData()
- 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.
local export = {}
local WikimediaLanguage = {}
function WikimediaLanguage:getCode()
return self._code
end
function WikimediaLanguage:getCanonicalName()
return self._rawData.canonicalName
end
--function WikimediaLanguage:getAllNames()
-- return self._rawData.names
--end
--[==[Given a list of types as strings, returns true if the Wikimedia language has all of them. Possible types are explained in [[Module:wikimedia languages/data]].]==]
function WikimediaLanguage:hasType(...)
if not self._type then
self._type = {["Wikimedia language"] = true}
if self._rawData.type then
for _, type in ipairs(mw.text.split(self._rawData.type, "%s*,%s*")) do
self._type[type] = true
end
end
end
for _, type in ipairs{...} do
if not self._type[type] then
return false
end
end
return true
end
function WikimediaLanguage:getWiktionaryLanguage()
if not self._wiktionaryLanguageObject then
self._wiktionaryLanguageObject = require("Module:languages").getByCode(self._rawData.wiktionary_code, nil, "allow etym")
end
return self._wiktionaryLanguageObject
end
-- Do NOT use this method!
-- All uses should be pre-approved on the talk page!
function WikimediaLanguage:getRawData()
return self._rawData
end
WikimediaLanguage.__index = WikimediaLanguage
function export.getByCode(code)
-- Only accept codes the software recognises
if not mw.language.isKnownLanguageTag(code) then
return nil
end
local rawData = mw.loadData("Module:wikimedia languages/data")[code]
-- If there is no specific Wikimedia code, then "borrow" the information
-- from the general Wiktionary language code
if not rawData then
local lang = require("Module:languages").getByCode(code, nil, "allow etym")
if not lang then
return nil
end
rawData = {canonicalName = lang:getCanonicalName(), wiktionary_code = code}
elseif not rawData.canonicalName then
rawData = {
canonicalName = require("Module:languages").getByCode(rawData.wiktionary_code, nil, "allow etym"):getCanonicalName(),
wiktionary_code = rawData.wiktionary_code
}
end
return setmetatable({_rawData = rawData, _code = code}, WikimediaLanguage)
end
function export.getByCodeWithFallback(code)
local object = export.getByCode(code)
if object then
return object
end
local lang = require("Module:languages").getByCode(code, nil, "allow etym")
if not lang then
return nil
end
return lang:getWikimediaLanguages()[1]
end
return export