Jump to content


From Wikimedia Incubator

Add search text after the "incategory..." filter:

This module is used to retrieve and manage the languages that can have Wiktionary entries, and the information associated with them. See Wiktionary:Languages for more information.

For the languages and language varieties that may be used in etymologies, see Module:Wt/haw/etymology languages. For language families, which sometimes also appear in etymologies, see Module:Wt/haw/families.

This module provides access to other modules. To access the information from within a template, see Module:Wt/haw/languages/templates.

The information itself is stored in the various data modules that are subpages of this module. They are listed in Category:Language data modules. These modules should not be used directly by any other module, the data should only be accessed through the functions provided by Module:languages.

Finding and retrieving languages

[edit source]

The module exports a number of functions that are used to find languages.


[edit source]


Finds the language whose code matches the one provided. If it exists, it returns a Language object representing the language. Otherwise, it returns nil.


[edit source]


Finds the language whose canonical name (the name used to represent that language on Wiktionary) or other name matches the one provided. If it exists, it returns a Language object representing the language. Otherwise, it returns nil.

The canonical name of languages should always be unique (it is an error for two languages on Wiktionary to share the same canonical name), so this is guaranteed to give at most one result.

This function and the following are powered by Module:Wt/haw/languages/by name, which goes through the Category:Language data modules for non-etymology languages and creates one table of canonical names with their language codes and another table with both canonical names and other names.


[edit source]


Like getByCanonicalName(), except it also looks at the otherNames listed in the non-etymology language data modules.


[edit source]


This function is expensive

Returns a table containing Language objects for all languages, sorted by code.

This function searches through the whole database of languages, and is therefore relatively resource-intensive. It should be used sparingly.

Language objects

[edit source]

A Language object is returned from one of the functions above. It is a Lua representation of a 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_languages = require("Module:Wt/haw/languages")
local lang = m_languages.getByCode("fr")
local name = lang:getCanonicalName()
-- "name" will now be "French"


[edit source]


Returns the language code of the language. Example: "fr" for French.


[edit source]


Returns the canonical name of the language. This is the name used to represent that language on Wiktionary, and is guaranteed to be unique to that language alone. Example: "French" for French.


[edit source]


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.


[edit source]


Returns the type of language, which can be "regular", "reconstructed" or "appendix-constructed".


[edit source]


Returns a table containing WikimediaLanguage objects (see Module:Wt/haw/wikimedia languages), which represent languages and their codes as they are used in Wikimedia projects for interwiki linking and such. More than one object may be returned, as a single Wiktionary language may correspond to multiple Wikimedia languages. For example, Wiktionary's single code sh (Serbo-Croatian) maps to four Wikimedia codes: sh (Serbo-Croatian), bs (Bosnian), hr (Croatian) and sr (Serbian).

The code for the Wikimedia language is retrieved from the wikimedia_codes property in the data modules. If that property is not present, the code of the current language is used. If none of the available codes is actually a valid Wikimedia code, an empty table is returned.


[edit source]


Returns the name of the Wikipedia article for the language. This is either the wikipedia_article property in the data modules, or the category name returned by :getCategoryName.


[edit source]


Returns a table of Script objects for all scripts that the language is written in. See Module:Wt/haw/scripts.


[edit source]


Returns a Family object for the language family that the language belongs to. See Module:Wt/haw/families.


[edit source]


Returns a table of Language objects for all languages that this language is directly descended from. Generally this is only a single language, but creoles, pidgins and mixed languages can have multiple ancestors.


[edit source]


Returns the name of the main category of that language. Example: "French language" for French, whose category is at Category:French language.


[edit source]


Converts the given term into the form used in the names of entries. This removes diacritical marks from the term if they are not considered part of the normal written form of the language, and which therefore are not permitted in page names. It also removes certain punctuation characters like final question marks or periods which are never present in page names. Example for Latin: "amō""amo" (macron is removed).

The replacements made by this function are defined by the entry_name setting for each language in the data modules.


[edit source]


Creates a sort key for the given, following the rules appropriate for the language. This removes diacritical marks from the term if they are not considered significant for sorting, and may perform some other changes. Any initial hyphen is also removed, and anything parentheses is removed as well.

The replacements made by this function are defined by the sort_key setting for each language in the data modules.


[edit source]

:transliterate(text, sc, module_override)

Transliterates the text from the given script into the Latin script (see Wiktionary:Transliteration and romanization). The language must have the translit_module property for this to work; if it is not present, nil is returned.

The sc parameter is handled by the transliteration module, and how it is handled is specific to that module. Some transliteration modules may tolerate nil as the script, others require it to be one of the possible scripts that the module can transliterate, and will show an error if it's not one of them. For this reason, the sc parameter should always be provided when writing non-language-specific code.

The module_override parameter is used to override the default module that is used to provide the transliteration. This is useful in cases where you need to demonstrate a particular module in use, but there is no default module yet, or you want to demonstrate an alternative version of a transliteration module before making it official. It should not be used in real modules or templates, only for testing. All uses of this parameter are tracked by Template:Wt/haw/tracking/module override.


[edit source]


Returns true if the language has a transliteration module, false if it doesn't.


[edit source]


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.

Error function

[edit source]

err(lang, param, text)

Looks at a supposed language code passed through a template parameter and returns a helpful error message depending on whether the language code has a valid form (two or three lowercase basic Latin letters, two or three groups of three lowercase basic Latin letters separated by hyphens).

Add the parameter value in argument #1 and the parameter name in argument #2. For instance, if parameter 1 of the template is supposed to be a language code, this function can be called the following way:

local m_languages = require("Module:Wt/haw/languages")
local lang = m_languages.getByCode(frame.args[1]) or m_languages.err(frame.args[1], 1)

If you would like the error message to say something other than "language code", place the phrase in argument #3.

local export = {}

local find = mw.ustring.find

--[=[	This function checks for things that could plausibly be a language code:
		two or three lowercase letters, two or three groups of three lowercase
		letters with hyphens between them. If such a pattern is not found,
		it is likely the editor simply forgot to enter a language code. ]=]

function export.err(langCode, param, text)
	local ordinals = { "first", "second", "third", "fourth" }
	if type(param) == "number" then
		ordinal = ordinals[param]
		param = ordinal .. ' parameter'
	elseif type(param) == "string" then
		param = 'parameter "' .. param .. '"'
		error("The parameter name is "
			.. ( type(param) == "table" and "a table" or tostring(param) )
			.. ", but it should be a number or a string.")
	--[[|	Can't use "%l" because that would include all Unicode
			lowercase letters; language codes only use ASCII.	]]
	local lower = "[a-z]"
	if not langCode or langCode == "" then
		error("The " .. param .. " (" .. (text or "language code" ) .. ") is missing.", 2)
	elseif find(langCode, "^" .. lower .. lower .. lower .. "?$")
		or find(langCode, "^" .. lower .. lower .. lower
			.. "%-" .. lower .. lower .. lower .. "$")
		or find(langCode, "^" .. lower .. lower .. lower
			.. "%-" .. lower .. lower .. lower
			.. "%-" .. lower .. lower .. lower .. "$") then
		error("The language code \"" .. langCode .. "\" is not valid.", 2)
		error("Please enter a " .. ( text or "language code" ) .. " in the " .. param .. ".", 2)

local Language = {}

function Language:getCode()
	return self._code

function Language:getCanonicalName()
	return self._rawData.canonicalName

-- Commented out; I don't think anything uses this, the presence/absence of script errors should confirm
--function Language:getAllNames()
--	return self._rawData.names

function Language:getOtherNames()
	return self._rawData.otherNames or {}

function Language:getType()
	return self._rawData.type or "regular"

function Language:getWikimediaLanguages()
	if not self._wikimediaLanguageObjects then
		local m_wikimedia_languages = require("Module:Wt/haw/wikimedia languages")
		self._wikimediaLanguageObjects = {}
		local wikimedia_codes = self._rawData.wikimedia_codes or {self._code}
		for _, wlangcode in ipairs(wikimedia_codes) do
			table.insert(self._wikimediaLanguageObjects, m_wikimedia_languages.getByCode(wlangcode))
	return self._wikimediaLanguageObjects

function Language:getWikipediaArticle()
	return self._rawData.wikipedia_article or self:getCategoryName()

function Language:makeWikipediaLink()
	return "[[w:" .. self:getWikipediaArticle() .. "|" .. self:getCanonicalName() .. "]]"

function Language:getScripts()
	if not self._scriptObjects then
		local m_scripts = require("Module:Wt/haw/scripts")
		self._scriptObjects = {}
		for _, sc in ipairs(self._rawData.scripts or {"None"}) do
			table.insert(self._scriptObjects, m_scripts.getByCode(sc))
	return self._scriptObjects

function Language:getScriptCodes()
	return self._rawData.scripts or {"None"}

function Language:getFamily()
	if self._rawData.family and not self._familyObject then
		self._familyObject = require("Module:Wt/haw/families").getByCode(self._rawData.family)
	return self._familyObject

function Language:getAncestors()
	if not self._ancestorObjects then
		self._ancestorObjects = {}
		if self._rawData.ancestors then
			for _, ancestor in ipairs(self._rawData.ancestors) do
				table.insert(self._ancestorObjects, export.getByCode(ancestor) or require("Module:Wt/haw/etymology languages").getByCode(ancestor))
			local fam = self:getFamily()
			local protoLang = fam and fam:getProtoLanguage() or nil
			-- For the case where the current language is the proto-language
			-- of its family, we need to step up a level higher right from the start.
			if protoLang and protoLang:getCode() == self:getCode() then
				fam = fam:getFamily()
				protoLang = fam and fam:getProtoLanguage() or nil
			while not protoLang and not (not fam or fam:getCode() == "qfa-not") do
				fam = fam:getFamily()
				protoLang = fam and fam:getProtoLanguage() or nil
			table.insert(self._ancestorObjects, protoLang)
	return self._ancestorObjects

local function iterateOverAncestorTree(node, func)
	for _, ancestor in ipairs(node:getAncestors()) do
		if ancestor then
			local ret = func(ancestor) or iterateOverAncestorTree(ancestor, func)
			if ret then
				return ret

function Language:getAncestorChain()
	if not self._ancestorChain then
		self._ancestorChain = {}
		local step = #self:getAncestors() == 1 and self:getAncestors()[1] or nil
		while step do
			table.insert(self._ancestorChain, 1, step)
			step = #step:getAncestors() == 1 and step:getAncestors()[1] or nil
	return self._ancestorChain

function Language:hasAncestor(otherlang)
	local function compare(ancestor)
		return ancestor:getCode() == otherlang:getCode()
	return iterateOverAncestorTree(self, compare) or false

function Language:getCategoryName()
	local name = self._rawData.canonicalName
	-- If the name already has "language" in it, don't add it.
	if name:find("[Ll]anguage$") then
		return name
		return name .. " language"

function Language:getStandardCharacters()
	return self._rawData.standardChars

function Language:makeEntryName(text)
	text = mw.ustring.gsub(text, "^[¿¡]", "")
	text = mw.ustring.gsub(text, "(.)[؟?!;՛՜ ՞ ՟?!︖︕।॥။၊་།]$", "%1")
	if self:getCode() == "ar" then
		local U = mw.ustring.char
		local taTwiil = U(0x640)
		local waSla = U(0x671)
		-- diacritics ordinarily removed by entry_name replacements
		local Arabic_diacritics = U(0x64B, 0x64C, 0x64D, 0x64E, 0x64F, 0x650, 0x651, 0x652, 0x670)
		if text == waSla or mw.ustring.find(text, "^" .. taTwiil .. "?[" .. Arabic_diacritics .. "]" .. "$") then
			return text
	if self._rawData.entry_name then
		for i, from in ipairs(self._rawData.entry_name.from) do
			local to = self._rawData.entry_name.to[i] or ""
			text = mw.ustring.gsub(text, from, to)
	--[=[ For instance, ᾰ (alpha-breve) + combining smooth breathing is converted
		to alpha + combining smooth breathing by the entry_name replacements.
		It must be re-combined to alpha-smooth breathing (ἀ) so that
		allowSelfLink in [[Module:links]] will work properly. ]=]
	if self:getCode() == "grc"
		then text = mw.ustring.toNFC(text)
	return text

function Language:makeSortKey(name)
	name = mw.ustring.lower(name)
	-- Remove initial hyphens and *
	local hyphens_regex = "^[-־ـ*]+(.)"
	name = mw.ustring.gsub(name, hyphens_regex, "%1")

	-- Remove parentheses, as long as they are either preceded or followed by something
	name = mw.ustring.gsub(name, "(.)[()]+", "%1")
	name = mw.ustring.gsub(name, "[()]+(.)", "%1")
	-- If there are language-specific rules to generate the key, use those
	if self._rawData.sort_key then
		for i, from in ipairs(self._rawData.sort_key.from) do
			local to = self._rawData.sort_key.to[i] or ""
			name = mw.ustring.gsub(name, from, to)
	return mw.ustring.upper(name)

function Language:overrideManualTranslit()
	if self._rawData.override_translit then
		return true
		return false

function Language:transliterate(text, sc, module_override)
	if not ((module_override or self._rawData.translit_module) and text) then
		return nil
	if module_override then
	return require("Module:Wt/haw/" .. (module_override or self._rawData.translit_module)).tr(text, self:getCode(), sc and sc:getCode() or nil)

function Language:hasTranslit()
	return self._rawData.translit_module and true or false

function Language:link_tr()
	return self._rawData.link_tr and true or false

function Language:toJSON()
	local entryNamePatterns = nil
	if self._rawData.entry_name then
		entryNamePatterns = {}
		for i, from in ipairs(self._rawData.entry_name.from) do
			local to = self._rawData.entry_name.to[i] or ""
			table.insert(entryNamePatterns, {from = from, to = to})
	local ret = {
		ancestors = self._rawData.ancestors,
		canonicalName = self:getCanonicalName(),
		categoryName = self:getCategoryName(),
		code = self._code,
		entryNamePatterns = entryNamePatterns,
		family = self._rawData.family,
		otherNames = self:getOtherNames(),
		scripts = self._rawData.scripts,
		type = self:getType(),
		wikimediaLanguages = self._rawData.wikimedia_codes,
	return require("Module:Wt/haw/JSON").toJSON(ret)

-- Do NOT use this method!
-- All uses should be pre-approved on the talk page!
function Language:getRawData()
	return self._rawData

Language.__index = Language

function export.getDataModuleName(code)
	if code:find("^[a-z][a-z]$") then
		return "languages/data2"
	elseif code:find("^[a-z][a-z][a-z]$") then
		local prefix = code:sub(1, 1)
		return "languages/data3/" .. prefix
	elseif code:find("^[a-z-]+$") then
		return "languages/datax"
		return nil

local function getRawLanguageData(code)
	local modulename = export.getDataModuleName(code)
	return modulename and mw.loadData("Module:Wt/haw/" .. modulename)[code] or nil

function export.makeObject(code, data)
	if data and data.deprecated then
		require("Module:Wt/haw/debug").track("languages/deprecated/" .. code)
	return data and setmetatable({ _rawData = data, _code = code }, Language) or nil

function export.getByCode(code)
	if type(code) ~= "string" then
		error("The function getByCode expects a string as its first argument, but received " .. ( code == nil and "nil" or "a " .. type(code) ) .. ".")
	return export.makeObject(code, getRawLanguageData(code))

function export.getByName(name)
	local byName = mw.loadData("Module:Wt/haw/languages/by name")
	local code = byName.all and byName.all[name] or byName[name]
	if not code then
		return nil
	return export.makeObject(code, getRawLanguageData(code))

function export.getByCanonicalName(name)
	local byName = mw.loadData("Module:Wt/haw/languages/canonical names")
	local code = byName and byName[name]
	if not code then
		return nil
	return export.makeObject(code, getRawLanguageData(code))

function export.iterateAll()
	local m_data = mw.loadData("Module:Wt/haw/languages/alldata")
	local func, t, var = pairs(m_data)
	return function()
		local code, data = func(t, var)
		return export.makeObject(code, data)

return export