This module is used to retrieve and manage Wiktionary's various languages and the information associated with them. See વિક્શનરી:ભાષાઓ for more information.

This module provides access to other modules. To access the information from within a template, see Module:ભાષાઓ/ઢાંચાઓ.

The information itself is stored in the various data modules that are subpages of this module. They are listed in શ્રેણી: ભાષા આંકડા પ્રતિરૂપકો. 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

ફેરફાર કરો

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

getLanguageByCode(code)

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

getLanguageByCanonicalName

ફેરફાર કરો

getLanguageByCanonicalName(name)

This function is expensive

Finds the language whose canonical name (the name used to represent that language on Wiktionary) 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 searches through the whole database of languages, and is therefore relatively resource-intensive. It should be used sparingly.

findLanguagesByName(name, inexact)

This function is expensive

Finds languages which have the provided name among their list of possible names (including their canonical name). It returns a table containing Language objects for the languages found, or an empty table if none were found.

The inexact parameter can be given as true to perform a substring search of the name instead of an exact match. The result will then contain all languages that have the provided name as part of one of their possible names.

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

getAllLanguages()

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.

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:ભાષાઓ")
local lang = m_languages.getLanguageByCode("fr")
local name = lang:getCanonicalName()
-- "name" will now be "French"

: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, and is guaranteed to be unique to that language alone. 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.

:getType()

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

:getScripts()

Returns a table of Script objects for all scripts that the language is written in. See Module:લિપિઓ.

:getFamily()

Returns a Family object for the language family that the language belongs to. See Module:પરિવારો.

Language:getCategoryName

ફેરફાર કરો

:getCategoryName()

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

Language:makeEntryName

ફેરફાર કરો

:makeEntryName(term)

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.

Language:makeSortKey

ફેરફાર કરો

:makeSortKey(term)

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.

ભાષા:લિપ્યંતરણ

ફેરફાર કરો

:transliterate(text, sc)

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.

: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 Language = {}

function Language:getCode()
	return self._code
end

function Language:getCanonicalName()
	return self._rawData.names[1]
end

function Language:getAllNames()
	return self._rawData.names
end

function Language:getType()
	return self._rawData.type
end

function Language:getScripts()
	if not self._scriptObjects then
		local m_scripts = require("Module:લિપિઓ")
		self._scriptObjects = {}
		
		for _, sc in ipairs(self._rawData.scripts) do
			table.insert(self._scriptObjects, m_scripts.getByCode(sc))
		end
	end
	
	return self._scriptObjects
end

function Language:getFamily()
	if not self._familyObject then
		local m_families = require("Module:પરિવારો")
		self._familyObject = m_families.getByCode(self._rawData.family)
	end
	
	return self._familyObject
end

function Language:getCategoryName()
	local name = self._rawData.names[1]
	
	-- If the name already has "language" in it, don't add it.
	if name:find("ભાષા$") then
		return name
	else
		return name .. " ભાષા"
	end
end

function Language:makeEntryName(text)
	text = mw.ustring.gsub(text, "^[¿¡]", "")
	text = mw.ustring.gsub(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)
		end
	end
	
	return text
end

function Language:makeSortKey(name)
	name = mw.ustring.lower(name)
	
	-- Remove initial hyphens and *
	name = mw.ustring.gsub(name, "^[-־ـ*]+(.)",
		"%1")
	-- Remove anything in 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)
		end
	end
	
	return mw.ustring.upper(name)
end

function Language:transliterate(text, sc)
	if type(sc) == "string" then
		require("Module:ભૂલકાઢ").track("લિપિ નામ/લિપ્યંતરણ")
		sc = require("Module:લિપિઓ").getByCode(sc)
	end
	
	if not self._rawData.translit_module or not text then
		return nil
	end
	
	return require("Module:" .. self._rawData.translit_module).tr(text, self:getCode(), sc and sc:getCode() or nil)
end

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

Language.__index = Language

local function getRawLanguageData(code)
	local stable = mw.loadData("Module:ભાષાઓ/સ્થિર")[code]
	
	if stable then
		return stable
	end
	
	local len = string.len(code)
	
	if code:find("^[a-z][a-z]$") then
		return mw.loadData("Module:ભાષાઓ/આંકડા૨")[code]
	elseif code:find("^[a-z][a-z][a-z]$") then
		local pre = code:sub(1, 1)
		return mw.loadData("Module:ભાષાઓ/આંકડા૩/" .. pre)[code]
	elseif code:find("^[a-z-]+$") then
		return mw.loadData("Module:ભાષાઓ/આંકડાx")[code]
	else
		return nil
	end
end

-- The object cache implements memoisation, and is used to avoid duplication
-- of objects. If you request the same language code twice, you should also get
-- the same object twice, not two different objects with identical data.
-- It might also speed things up a bit.
local object_cache = {}

function export.getByCode(code)
	if object_cache[code] then
		return object_cache[code]
	end
	
	local rawData = getRawLanguageData(code)
	
	if not rawData then
		return nil
	end
	
	local object = setmetatable({ _rawData = rawData, _code = code }, Language)
	object_cache[code] = object
	return object
end

function export.getLanguageByCode(code)
	require("Module:ભૂલકાઢ").track("getLanguageByCode")
	return export.getByCode(code)
end


-- Lua implementation of [[Template:langrev]]
-- We could optimise this by prioritising stable and data2 modules,
-- as they are more frequently used and thus more likely to contain what the user
-- is looking for.
function export.getByCanonicalName(name)
	local m_data = mw.loadData("Module:ભાષાઓ/સર્વઆંકડા")
	
	for code, data in pairs(m_data) do
		if data.names[1] == name then
			return export.getLanguageByCode(code)
		end
	end
	
	return nil
end

function export.findByName(name, inexact)
	mw.incrementExpensiveFunctionCount()
	local m_data = mw.loadData("Module:ભાષાઓ/સર્વઆંકડા")
	local found = {}
	
	for code, data in pairs(m_data) do
		for _, n in ipairs(data.names) do
			if inexact and n:find(name, nil, true) or n == name then
				table.insert(found, export.getLanguageByCode(code))
				break
			end
		end
	end
	
	return found
end

function export.getAll()
	mw.incrementExpensiveFunctionCount()
	local m_data = mw.loadData("Module:ભાષાઓ/સર્વઆંકડા")
	
	local ret = {}
	
	for code, data in pairs(m_data) do
		-- This isn't the most efficient way to do it, but it works for now.
		table.insert(ret, export.getLanguageByCode(code))
	end
	
	return ret
end

return export