Ravêr zerreki

Modul:Zıwani

Websitey Wikiqısebend ra

Seba na module şıma şenê yû pela dokumani vırazê Modul:Zıwani/dok

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()
	local m_scripts = require("Module:scripts")
	local ret = {}
	
	for _, sc in ipairs(self._rawData.scripts) do
		table.insert(ret, m_scripts.getScriptByCode(sc))
	end
	
	return ret
end

function Language:getFamily()
	local m_families = require("Module:families")
	return m_families.getFamilyByCode(self._rawData.family)
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("[Ll]anguage$") then
		return name
	else
		return name .. " language"
	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 not self._rawData.translit_module or not text then
		return nil
	end
	
	return require("Module:" .. self._rawData.translit_module).tr(text, self:getCode(), sc)
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:Zıwani/stable")[code]
	
	if stable then
		return stable
	end
	
	local len = string.len(code)
	
	if code:find("^[a-z][a-z]$") then
		return mw.loadData("Module:Zıwani/data2")[code]
	elseif code:find("^[a-z][a-z][a-z]$") then
		local pre = code:sub(1, 1)
		return mw.loadData("Module:Zıwani/data3/" .. pre)[code]
	elseif code:find("^[a-z-]+$") then
		return mw.loadData("Module:Zıwani/datax")[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.getLanguageByCode(code)
	if object_cache[code] then
		return object_cache[code]
	end
	
	local rawData = getRawLanguageData(code)
	
	if rawData then
		local object = setmetatable({ _rawData = rawData, _code = code }, Language)
		object_cache[code] = object
		return object
	else
		return nil
	end
end

-- Lua implementation of [[Şablon: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.getLanguageByCanonicalName(name)
	mw.incrementExpensiveFunctionCount()
	local m_data = mw.loadData("Module:Zıwani/heme")
	
	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.findLanguagesByName(name, inexact)
	mw.incrementExpensiveFunctionCount()
	local m_data = mw.loadData("Module:Zıwani/heme")
	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.getAllLanguages()
	mw.incrementExpensiveFunctionCount()
	local m_data = mw.loadData("Module:Zıwani/heme")
	
	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