模組:languages

Ùi Wiktionary
跳至導覽 跳至搜尋

此模組的說明文件可於模組:languages/doc建立

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:scripts")
		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:families")
		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("[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 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:languages/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:languages/data2")[code]
	elseif code:find("^[a-z][a-z][a-z]$") then
		local pre = code:sub(1, 1)
		return mw.loadData("Module:languages/data3/" .. pre)[code]
	elseif code:find("^[a-z-]+$") then
		return mw.loadData("Module:languages/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.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.getByCanonicalName(name)
	local code = mw.loadData("Module:languages/by name")[name]
	
	if not code then
		return nil
	end
	
	return export.getByCode(code)	
end

function export.getAll()
	mw.incrementExpensiveFunctionCount()
	local m_data = mw.loadData("Module:languages/alldata")
	
	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.getByCode(code))
	end
	
	return ret
end

return export