Модуль:languages: відмінності між версіями

9390 байтів вилучено ,  1 рік тому
м
нема опису редагування
[перевірена версія][перевірена версія]
Немає опису редагування
мНемає опису редагування
-- Модуль для создания списка переводов по языкам
local export = {}
-- загрузка модуля данных с таблицей языков
local languages = mw.loadData("Module:languages/data");
local p = {};
 
-- вспомогательная функция, удаляет пробелы
--[=[ This function checks for things that could plausibly be a language code:
local function trimstr(s)
two or three lowercase letters, two or three groups of three lowercase
return (s:gsub("^%s*(.-)%s*$", "%1"));
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, template_tag)
local ordinals = {
"first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth",
"thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth",
"eighteenth", "nineteenth", "twentieth"
}
if not template_tag then
template_tag = ""
else
if type(template_tag) ~= "string" then
template_tag = template_tag()
end
template_tag = " (Original template: " .. template_tag .. ")"
end
local paramType = type(param)
if paramType == "number" then
ordinal = ordinals[param]
param = ordinal .. ' parameter'
elseif paramType == "string" then
param = 'parameter "' .. param .. '"'
else
error("The parameter name is "
.. (paramType == "table" and "a table" or tostring(param))
.. ", but it should be a number or a string." .. template_tag, 2)
end
-- Can use string.find because language codes only contain ASCII.
if not langCode or langCode == "" then
error("The " .. param .. " (" .. (text or "language code") .. ") is missing." .. template_tag, 2)
elseif langCode:find("^%l%l%l?$")
or langCode:find("^%l%l%l%-%l%l%l$")
or langCode:find("^%l%l%l%-%l%l%l%-%l%l%l$") then
error("The language code \"" .. langCode .. "\" is not valid." .. template_tag, 2)
else
error("Please enter a " .. (text or "language code") .. " in the " .. param .. "." .. template_tag, 2)
end
end
 
local function do_entry_name_or_sort_key_replacementsformatLang(textcode, replacementsname, index, extention)
local result = '';
if replacements.from then
local margin = '';
for i, from in ipairs(replacements.from) do
result = result .. "* [[" .. mw.ustring.lower(name) .. "#Русский|" .. name .. "]]";
local to = replacements.to[i] or ""
if index ~= nil and index ~= "" then
text = mw.ustring.gsub(text, from, to)
local indexes = {
["в"] = "восстановленный язык",
["и"] = "искусственный язык",
["р"] = "реконструированный язык",
["ф"] = "фантастический язык",
["†"] = "мёртвый язык"
}
local title = indexes[index] or '';
if title ~= '' then
title = " title='" .. title .. "'";
end
result = result .. "<sup style='color:#66C033'" .. title .. ">" .. index .. "</sup>";
margin = ';margin-left: -5px';
end
if code ~= nil and code ~= "" then
result = result .. "<sub style='color:#33C066" .. margin .. "'>" .. code .. "</sub>";
if replacements.remove_diacritics then
text = mw.ustring.toNFD(text)
text = mw.ustring.gsub(text,
'[' .. replacements.remove_diacritics .. ']',
'')
text = mw.ustring.toNFC(text)
end
if extention ~= nil and extention ~= "" then
result = result .. " " .. extention;
return text
end
 
local Language = {}
 
function Language:getCode()
return self._code
end
 
 
function Language:getCanonicalName()
return self._rawData[1] or self._rawData.canonicalName
end
 
 
function Language:getOtherNames()
return self._rawData.otherNames or {}
end
 
 
function Language:getType()
return self._rawData.type or "regular"
end
 
 
function Language:getWikimediaLanguages()
if not self._wikimediaLanguageObjects then
local m_wikimedia_languages = require("Module: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))
end
end
return result;
return self._wikimediaLanguageObjects
end
 
function Language:getWikipediaArticlep.list(frame)
local result = "";
if self._rawData.wikipedia_article then
local names = {};
return self._rawData.wikipedia_article
local nnames = {};
elseif self._wikipedia_article then
local key = "";
return self._wikipedia_article
local cnt = 0;
elseif self:getWikidataItem() and mw.wikibase then
local demo = frame.args['demo'];
self._wikipedia_article = mw.wikibase.sitelink(self:getWikidataItem(), 'enwiki')
if demo ~= nil and demo ~= "" then
end
for code,v in pairs(languages) do
if not self._wikipedia_article then
key = v[2];
self._wikipedia_article = mw.ustring.gsub(self:getCategoryName(), "Creole language", "Creole")
if v[4] ~= nil and v[4] ~= "" then
end
key = key .. v[4];
return self._wikipedia_article
end
table.insert(nnames, key);
names[key] = { code = v[1], name = v[2], index = v[3], extention = v[4], value = code};
cnt = cnt +1;
end;
else
for code,value in pairs(frame:getParent().args) do
if type(code)=='string' then
local trimmed = trimstr(code);
local v = trimstr(value);
local l = languages[trimmed];
if l ~= nil and l ~= "" and v ~= nil and v ~= "" then
key = l[2];
if l[4] ~= nil and l[4] ~= "" then
key = key .. l[4];
end
table.insert(nnames, key);
names[key] = { code = l[1], name = l[2], index = l[3], extention = l[4], value = v};
cnt = cnt +1;
end
end
end
end
if cnt > 0 then
table.sort(nnames);
for i, key in ipairs(nnames) do
local v = names[key];
result = result .. formatLang(v.code, v.name, v.index, v.extention);
result = result .. ": " .. v.value .. "\n";
end
end
return result
end
 
function Language:makeWikipediaLinkp.ref(frame)
local result = "";
return "[[w:" .. self:getWikipediaArticle() .. "|" .. self:getCanonicalName() .. "]]"
local args = frame.args;
end
if args[1] == nil then
 
args = frame:getParent().args;
function Language:getWikidataItem()
return self._rawData[2] or self._rawData.wikidata_item
end
 
function Language:getScripts()
if not self._scriptObjects then
local m_scripts = require("Module:scripts")
self._scriptObjects = {}
for _, sc in ipairs(self._rawData.scripts or { "None" }) do
table.insert(self._scriptObjects, m_scripts.getByCode(sc))
end
end
local code = args[1];
if type(code) == 'string' then
return self._scriptObjects
local trimmed = trimstr(code);
end
local l = languages[trimmed];
 
if l ~= nil then
function Language:getScriptCodes()
result = result .. formatLang(l[1], l[2], l[3], l[4]);
return self._rawData.scripts or { "None" }
end
 
function Language:getFamily()
if self._familyObject then
return self._familyObject
end
local family = self._rawData[3] or self._rawData.family
if family then
self._familyObject = require("Module:families").getByCode(family)
end
return self._familyObject
end
 
 
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:etymology languages").getByCode(ancestor))
end
else
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
end
while not protoLang and not (not fam or fam:getCode() == "qfa-not") do
fam = fam:getFamily()
protoLang = fam and fam:getProtoLanguage() or nil
end
table.insert(self._ancestorObjects, protoLang)
end
end
if result == '' then
result = tostring(code);
return self._ancestorObjects
end
 
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
end
end
end
end
 
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
end
end
return self._ancestorChain
end
 
 
function Language:hasAncestor(otherlang)
local function compare(ancestor)
return ancestor:getCode() == otherlang:getCode()
end
return iterateOverAncestorTree(self, compare) or false
end
 
 
function Language:getCategoryName()
local name = self:getCanonicalName()
-- 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:getStandardCharacters()
return self._rawData.standardChars
end
 
 
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
end
end
if type(self._rawData.entry_name) == "table" then
text = do_entry_name_or_sort_key_replacements(text, self._rawData.entry_name)
end
return text
end
 
 
-- Add to data tables?
local has_dotted_undotted_i = {
["az"] = true,
["crh"] = true,
["gag"] = true,
["kaa"] = true,
["tt"] = true,
["tr"] = true,
["zza"] = true,
}
 
function Language:makeSortKey(name, sc)
if has_dotted_undotted_i[self:getCode()] then
name = name:gsub("I", "ı")
end
name = mw.ustring.lower(name)
-- Remove initial hyphens and *
local hyphens_regex = "^[-־ـ*]+(.)"
name = mw.ustring.gsub(name, hyphens_regex, "%1")
-- If there are language-specific rules to generate the key, use those
if type(self._rawData.sort_key) == "table" then
name = do_entry_name_or_sort_key_replacements(name, self._rawData.sort_key)
elseif type(self._rawData.sort_key) == "string" then
name = require("Module:" .. self._rawData.sort_key).makeSortKey(name, self:getCode(), sc and sc:getCode())
end
-- 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 has_dotted_undotted_i[self:getCode()] then
name = name:gsub("i", "İ")
end
return mw.ustring.upper(name)
end
 
function Language:overrideManualTranslit()
if self._rawData.override_translit then
return true
else
return false
end
end
 
 
function Language:transliterate(text, sc, module_override)
if not ((module_override or self._rawData.translit_module) and text) then
return nil
end
if module_override then
require("Module:debug").track("module_override")
end
return require("Module:" .. (module_override or self._rawData.translit_module)).tr(text, self:getCode(), sc and sc:getCode() or nil)
end
 
function Language:hasTranslit()
return self._rawData.translit_module and true or false
end
 
 
function Language:link_tr()
return self._rawData.link_tr and true or false
end
 
 
function Language:toJSON()
local entryNamePatterns = nil
local entryNameRemoveDiacritics = nil
if self._rawData.entry_name then
entryNameRemoveDiacritics = self._rawData.entry_name.remove_diacritics
if self._rawData.entry_name.from 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 })
end
end
end
local ret = {
ancestors = self._rawData.ancestors,
canonicalName = self:getCanonicalName(),
categoryName = self:getCategoryName(),
code = self._code,
entryNamePatterns = entryNamePatterns,
family = self._rawData[3] or self._rawData.family,
otherNames = self:getOtherNames(),
scripts = self._rawData.scripts,
type = self:getType(),
wikimediaLanguages = self._rawData.wikimedia_codes,
wikidataItem = self:getWikidataItem(),
}
return require("Module:JSON").toJSON(ret)
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
 
 
function export.getDataModuleName(code)
if code:find("^%l%l$") then
return "languages/data2"
elseif code:find("^%l%l%l$") then
local prefix = code:sub(1, 1)
return "languages/data3/" .. prefix
elseif code:find("^[%l-]+$") then
return "languages/datax"
else
return nil
end
end
 
 
local function getRawLanguageData(code)
local modulename = export.getDataModuleName(code)
return modulename and mw.loadData("Module:" .. modulename)[code] or nil
end
 
 
function export.makeObject(code, data)
if data and data.deprecated then
require("Module:debug").track {
"languages/deprecated",
"languages/deprecated/" .. code
}
end
return data and setmetatable({ _rawData = data, _code = code }, Language) or nil
end
 
 
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)) .. ".")
end
return export.makeObject(code, getRawLanguageData(code))
end
 
 
function export.getByName(name)
local byName = mw.loadData("Module:languages/by name")
local code = byName.all and byName.all[name] or byName[name]
if not code then
return nil
end
return export.makeObject(code, getRawLanguageData(code))
end
 
 
function export.getByCanonicalName(name)
local byName = mw.loadData("Module:languages/canonical names")
local code = byName and byName[name]
if not code then
return nil
end
return export.makeObject(code, getRawLanguageData(code))
end
 
 
function export.iterateAll()
mw.incrementExpensiveFunctionCount()
local m_data = mw.loadData("Module:languages/alldata")
local func, t, var = pairs(m_data)
return function()
local code, data = func(t, var)
return export.makeObject(code, data)
end
end
 
--[[ If language is an etymology language, iterates through parent languages
until it finds a non-etymology language. ]]
function export.getNonEtymological(lang)
while lang:getType() == "etymology language" do
local parentCode = lang:getParentCode()
local parent = export.getByCode(parentCode)
or require("Module:etymology languages").getByCode(parentCode)
or require("Module:families").getByCode(parentCode)
lang = parent
-- mw.log(terminfo.lang:getCode() .. " " .. terminfo.lang:getType())
end
return result;
return lang
end
 
return exportp