Module:Cyrs-translit

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module will transliterate text in the Old Cyrillic script. It is used to transliterate Old Church Slavonic, Old East Slavic, and Old Novgorodian. The module should preferably not be called directly from templates or other modules. To use it from a template, use {{xlit}}. Within a module, use Module:languages#Language:transliterate.

For testcases, see Module:Cyrs-translit/testcases.

Functions

tr(text, lang, sc)
Transliterates a given piece of text written in the script specified by the code sc, and language specified by the code lang.
When the transliteration fails, returns nil.

local export = {}

local numbers = mw.loadData("Module:Cyrs-translit/numbers")

local U = mw.ustring.char

local double_grave = U(0x30F)

local letters = {
	["common"] = {
		["А"] = 'A',
		["а"] = 'a',
		["Б"] = 'B',
		["б"] = 'b',
		["В"] = 'V',
		["в"] = 'v',
		["Г"] = 'G',
		["г"] = 'g',
		["Ґ"] = 'G',
		["ґ"] = 'g',
		["Д"] = 'D',
		["д"] = 'd',
		["Ꙣ"] = 'Dʹ',
		["ꙣ"] = 'dʹ',
		["Е"] = 'E',
		["е"] = 'e',
		["Є"] = 'E',
		["є"] = 'e',
		["Э"] = 'E',
		["э"] = 'e',
		["Ж"] = 'Ž',
		["ж"] = 'ž',
		["Ѕ"] = 'Dz',
		["ѕ"] = 'dz',
		["Ꙃ"] = 'Dz',
		["ꙃ"] = 'dz',
		["Ꙅ"] = 'Dz',
		["ꙅ"] = 'dz',
		["З"] = 'Z',
		["з"] = 'z',
		["Ꙁ"] = 'Z',
		["ꙁ"] = 'z',
		["И"] = 'I',
		["и"] = 'i',
		["І"] = 'I',
		["і"] = 'i',
		["Ӥ"] = 'I',
		["ӥ"] = 'i',
		["Ї"] = 'I',
		["ї"] = 'i',
		["Ꙇ"] = 'I',
		["ꙇ"] = 'i',
		["Ѝ"] = 'Ì',
		["ѝ"] = 'ì',
		["Й"] = 'J',
		["й"] = 'j',
		["Ꙉ"] = 'Đ',
		["ꙉ"] = 'đ',
		["К"] = 'K',
		["к"] = 'k',
		["Л"] = 'L',
		["л"] = 'l',
		["Ꙥ"] = 'Lʹ',
		["ꙥ"] = 'lʹ',
		["М"] = 'M',
		["м"] = 'm',
		["Ꙧ"] = 'Mʹ',
		["ꙧ"] = 'mʹ',
		["Н"] = 'N',
		["н"] = 'n',
		["Ҥ"] = 'Nʹ',
		["ҥ"] = 'nʹ',
		["О"] = 'O',
		["о"] = 'o',
		["Ѻ"] = 'O',
		["ѻ"] = 'o',
		["Ꙩ"] = 'O',
		["ꙩ"] = 'o',
		["Ꙫ"] = 'O',
		["ꙫ"] = 'o',
		["Ꚛ"] = 'O',
		["ꚛ"] = 'o',
		["Ꚙ"] = 'O',
		["ꚙ"] = 'o',
		["Ꙭ"] = 'O',
		["ꙭ"] = 'o',
		["ꙮ"] = 'o',
		["П"] = 'P',
		["п"] = 'p',
		["Р"] = 'R',
		["р"] = 'r',
		["С"] = 'S',
		["с"] = 's',
		["Т"] = 'T',
		["т"] = 't',
		["Ѹ"] = 'U',
		["ѹ"] = 'u',
		["Ꙋ"] = 'U',
		["ꙋ"] = 'u',
		["У"] = 'U',
		["у"] = 'u',
		[""] = 'u',
		["Ф"] = 'F',
		["ф"] = 'f',
		["Х"] = 'X',
		["х"] = 'x',
		["Ѡ"] = 'O',
		["ѡ"] = 'o',
		["Ѿ"] = 'Otŭ',
		["ѿ"] = 'otŭ',
		["Ꙍ"] = 'O',
		["ꙍ"] = 'o',
		["Ѽ"] = 'O',
		["ѽ"] = 'o',
		["Ц"] = 'C',
		["ц"] = 'c',
		["Ꙡ"] = 'C',
		["ꙡ"] = 'c',
		["Ч"] = 'Č',
		["ч"] = 'č',
		["Ш"] = 'Š',
		["ш"] = 'š',
		["Щ"] = 'Št',
		["щ"] = 'št',
		["Ъ"] = 'Ŭ',
		["ъ"] = 'ŭ',
		["Ꙑ"] = 'Y',
		["ꙑ"] = 'y',
		["Ы"] = 'Y',
		["ы"] = 'y',
		["Ь"] = 'Ĭ',
		["ь"] = 'ĭ',
		["Ѣ"] = 'Ě',
		["ѣ"] = 'ě',
		["Ꙓ"] = 'Jě',
		["ꙓ"] = 'jě',
		["Ꙗ"] = 'Ja',
		["ꙗ"] = 'ja',
		["Я"] = 'Ja',
		["я"] = 'ja',
		["Ѥ"] = 'Je',
		["ѥ"] = 'je',
		["Ю"] = 'Ju',
		["ю"] = 'ju',
		["Ꙕ"] = 'Ju',
		["ꙕ"] = 'ju',
		["Ѫ"] = 'Ǫ',
		["ѫ"] = 'ǫ',
		[""] = 'ǫ',
		[""] = 'ǫ',
		["Ѭ"] = 'Jǫ',
		["ѭ"] = 'jǫ',
		[""] = 'jǫ',
		["Ѧ"] = 'Ę',
		["ѧ"] = 'ę',
		["Ꙙ"] = 'Ę',
		["ꙙ"] = 'ę',
		["Ѩ"] = 'Ję',
		["ѩ"] = 'ję',
		["Ꙝ"] = 'Ję',
		["ꙝ"] = 'ję',
		["Ѯ"] = 'Ks',
		["ѯ"] = 'ks',
		[""] = 'ks',
		["Ѱ"] = 'Ps',
		["ѱ"] = 'ps',
		["Ѳ"] = 'Θ',
		["ѳ"] = 'θ',
		["Ѵ"] = 'Ü',
		["ѵ"] = 'ü',
		["Ѷ"] = 'Ü' .. double_grave,
		["ѷ"] = 'ü' .. double_grave,
		["Ҁ"] = 'Q',
		["ҁ"] = 'q',
	},
	["orv"] = {
		["Щ"] = 'Šč',
		["щ"] = 'šč',
		["Ѣ"] = 'Jě',
		["ѣ"] = 'jě',
		["Ѧ"] = 'Ję',
		["ѧ"] = 'ję',
		["Ꙙ"] = 'Ję',
		["ꙙ"] = 'ję',
	},
	["zle-ono"] = {
		["Ц"] = 'Ć',
		["ц"] = 'ć',
		["Ꙡ"] = 'Ć',
		["ꙡ"] = 'ć',
		["Ч"] = 'Ć',
		["ч"] = 'ć',
		["Щ"] = 'Ść',
		["щ"] = 'ść',
		["Ѣ"] = 'Jě',
		["ѣ"] = 'jě',
		["Ѧ"] = 'Ję',
		["ѧ"] = 'ję',
		["Ꙙ"] = 'Ję',
		["ꙙ"] = 'ję',
	},
	["zle-ops"] = {
		["Ж"] = 'Ź',
		["ж"] = 'ź',
		["Ѕ"] = 'Dź',
		["ѕ"] = 'dź',
		["Ꙃ"] = 'Dź',
		["ꙃ"] = 'dź',
		["Ꙅ"] = 'Dź',
		["ꙅ"] = 'dź',
		["З"] = 'Ź',
		["з"] = 'ź',
		["Ꙁ"] = 'Ź',
		["ꙁ"] = 'ź',
		["С"] = 'Ś',
		["с"] = 'ś',
		["Ц"] = 'Ć',
		["ц"] = 'ć',
		["Ꙡ"] = 'Ć',
		["ꙡ"] = 'ć',
		["Ч"] = 'Ć',
		["ч"] = 'ć',
		["Ш"] = 'Ś',
		["ш"] = 'ś',
		["Щ"] = 'Šk',
		["щ"] = 'šk',
		["Ѣ"] = 'Jě',
		["ѣ"] = 'jě',
		["Ѫ"] = 'Jǫ',
		["ѫ"] = 'jǫ',
		["Ѧ"] = 'Ję',
		["ѧ"] = 'ję',
		["Ꙙ"] = 'Ję',
		["ꙙ"] = 'ję',
	}
}

local digraphs = {
	["common"] = {
		["([АаꙖꙗ])[ѴѶ]"] = "%1V",
		["([АаꙖꙗ])[ѵѷ]"] = "%1v",
		["О[УуѴѵѶѷ]"] = "U",
		["о[уѵѷ]"] = "u",
		["Ъ[иіӥїꙇ]"] = "Y",
		["ъ[иіӥїꙇ]"] = "y",
		["Ѿ([АаЕеЄєЭэИиІіӤӥЇїꙆꙇЍѝОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮУуꙊꙋѠѡѾѿꙌꙍѼѽЪъꙎꙏꙐꙑЫыЬьѢѣѪѫꙚꙛѦѧꙘꙙѨѩѴѵѶѷ])"] = "Ot%1",
		["ѿ([АаЕеЄєЭэИиІіӤӥЇїꙆꙇЍѝОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮУуꙊꙋѠѡѾѿꙌꙍѼѽЪъꙎꙏꙐꙑЫыЬьѢѣѪѫꙚꙛѦѧꙘꙙѨѩѴѵѶѷ])"] = "ot%1",
	},
	["orv"] = {
		["Ъ([иіӥїꙇ])"] = "Ŷ%1",
		["ъ([иіӥїꙇ])"] = "ŷ%1",
		["([АаꙢꙣЕеЄєЭэИиІіӤӥЇїꙆꙇЍѝꙤꙥꙦꙧҤҥОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮУуꙊꙋѠѡꙌꙍѼѽЪъꙎꙏꙐꙑЫыЬьѢѣꙒꙓꙖꙗЯяѤѥЮюꙔꙕѪѫꙚꙛѬѭѦѧꙘꙙѨѩꙜꙝѴѵѶѷ])[еєэ]"]="%1je",
		["([БбВвГгДдЖжЅѕꙂꙃꙄꙅЗзꙀꙁꙈꙉЙйКкЛлМмНнѾѿПпРрСсТтФфХхЦцꙠꙡЧчШшЩщѮѯѰѱѲѳҀҁ])ѣ"] = "%1ě",
		["([БбВвГгДдЖжЅѕꙂꙃꙄꙅЗзꙀꙁꙈꙉЙйКкЛлМмНнѾѿПпРрСсТтФфХхЦцꙠꙡЧчШшЩщѮѯѰѱѲѳҀҁ])[ѧꙙ]"] = "%1ę",
	},
	["zle-ono"] = {
		["У[ОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮѠѡꙌꙍѼѽ]"] = "U",
		["у[ОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮѠѡꙌꙍѼѽ]"] = "u",
		["Ъ([иіӥїꙇ])"] = "Ŷ%1",
		["ъ([иіӥїꙇ])"] = "ŷ%1",
		["([АаꙢꙣЕеЄєЭэИиІіӤӥЇїꙆꙇЍѝꙤꙥꙦꙧҤҥОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮУуꙊꙋѠѡꙌꙍѼѽЪъꙎꙏꙐꙑЫыЬьѢѣꙒꙓꙖꙗЯяѤѥЮюꙔꙕѪѫꙚꙛѬѭѦѧꙘꙙѨѩꙜꙝѴѵѶѷ])[еєэ]"]="%1je",
		["([БбВвГгДдЖжЅѕꙂꙃꙄꙅЗзꙀꙁꙈꙉЙйКкЛлМмНнѾѿПпРрСсТтФфХхЦцꙠꙡЧчШшЩщѮѯѰѱѲѳҀҁ])ѣ"] = "%1ě",
		["([БбВвГгДдЖжЅѕꙂꙃꙄꙅЗзꙀꙁꙈꙉЙйКкЛлМмНнѾѿПпРрСсТтФфХхЦцꙠꙡЧчШшЩщѮѯѰѱѲѳҀҁ])[ѧꙙ]"] = "%1ę",
	},
	["zle-ops"] = {
		["У[ОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮѠѡꙌꙍѼѽ]"] = "U",
		["у[ОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮѠѡꙌꙍѼѽ]"] = "u",
		["Ъ([иіӥїꙇ])"] = "Ŷ%1",
		["ъ([иіӥїꙇ])"] = "ŷ%1",
		["([АаꙢꙣЕеЄєЭэИиІіӤӥЇїꙆꙇЍѝꙤꙥꙦꙧҤҥОоѺѻꙨꙩꙪꙫꚚꚛꚘꚙꙬꙭꙮУуꙊꙋѠѡꙌꙍѼѽЪъꙎꙏꙐꙑЫыЬьѢѣꙒꙓꙖꙗЯяѤѥЮюꙔꙕѪѫꙚꙛѬѭѦѧꙘꙙѨѩꙜꙝѴѵѶѷ])[еєэ]"]="%1je",
		["([БбВвГгДдЖжЅѕꙂꙃꙄꙅЗзꙀꙁꙈꙉЙйКкЛлМмНнѾѿПпРрСсТтФфХхЦцꙠꙡЧчШшЩщѮѯѰѱѲѳҀҁ])ѣ"] = "%1ě",
		["([БбВвГгДдЖжЅѕꙂꙃꙄꙅЗзꙀꙁꙈꙉЙйКкЛлМмНнѾѿПпРрСсТтФфХхЦцꙠꙡЧчШшЩщѮѯѰѱѲѳҀҁ])ѫ"] = "%1ǫ",
		["([БбВвГгДдЖжЅѕꙂꙃꙄꙅЗзꙀꙁꙈꙉЙйКкЛлМмНнѾѿПпРрСсТтФфХхЦцꙠꙡЧчШшЩщѮѯѰѱѲѳҀҁ])[ѧꙙ]"] = "%1ę",
	}
}

function export.tr(text, lang, sc)
	if not sc then
		sc = require("Module:languages").getByCode(lang):findBestScript(text):getCode()
	end

	local function digraph_subst(digraphs)
		for key, repl in pairs(digraphs) do
			text = mw.ustring.gsub(text, key, repl)
		end
	end

	if sc ~= "Cyrs" then
		text = nil
	else
		-- Transliterate the kamora as prime
		text = mw.ustring.gsub(text, U(0x0484), "ʹ")

		if digraphs[lang] then
			digraph_subst(digraphs[lang])
		end
		digraph_subst(digraphs["common"])

		digraph_subst(numbers)

		if letters[lang] then
			text = mw.ustring.gsub(text, '.', letters[lang])
		end
		text = mw.ustring.gsub(text, '.', letters["common"])

		-- Transliterate the titlo and vzmet as colon.
		text = mw.ustring.gsub(text, "[" .. U(0x0483) .. U(0xA66F) .. "]", ":")
	end

	return text
end

return export