Jump to content

Module:ky-Cyrl-Arab-translit

From Wiktionary, the free dictionary

This module will transliterate Kyrgyz language text per WT:KY TR. 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:ky-Cyrl-Arab-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 tt = {
	["ү"] = "ۉ",
	["Ү"] = "ۉ",
	["т"] = "ت",
	["Т"] = "ت",
	["р"] = "ر",
	["Р"] = "ر",
	["ф"] = "ف",
	["Ф"] = "ف",
	["ө"] = "ۅ",
	["Ө"] = "ۅ",
	["ю"] = "يۇ",
	["Ю"] = "يۇ",
	["ш"] = "ش",
	["Ш"] = "ش",
	["ь"] = "",
	["Ь"] = "",
	["ъ"] = "",
	["Ъ"] = "",
	["н"] = "ن",
	["Н"] = "ن",
	["п"] = "پ",
	["П"] = "پ",
	["й"] = "ي",
	["Й"] = "ي",
	["л"] = "ل",
	["Л"] = "ل",
	["з"] = "ز",
	["З"] = "ز",
	["е"] = "ە",
	["Е"] = "ە",
	["г"] = "گ",
	["Г"] = "گ",
	["б"] = "ب",
	["Б"] = "ب",
	["у"] = "ۇ",
	["У"] = "ۇ",
	["с"] = "س",
	["С"] = "س",
	["х"] = "ح",
	["Х"] = "ح",
	["ч"] = "چ",
	["Ч"] = "چ",
	["щ"] = "شچ",
	["Щ"] = "شچ",
	["я"] = "يا",
	["Я"] = "يا",
	["ы"] = "ى",
	["Ы"] = "ى",
	["э"] = "ە",
	["Э"] = "ە",
	["м"] = "م",
	["М"] = "م",
	["о"] = "و",
	["О"] = "و",
	["и"] = "ى",
	["И"] = "ئ", -- a bit hacky but this is for vowel harmony
	["ё"] = "يو",
	["Ё"] = "يو",
	["ж"] = "ج",
	["Ж"] = "ج",
	["к"] = "ك",
	["К"] = "ك",
	["д"] = "د",
	["Д"] = "د",
	["в"] = "ۋ",
	["В"] = "ۋ",
	["ц"] = "تس",
	["Ц"] = "تس",
	["а"] = "ا",
	["А"] = "ا",
	["ң"] = "ڭ",
	["Ң"] = "ڭ",
	-- not used needed for some conversions
	["қ"] = "ق",
	["ғ"] = "ع"
}

local frontvowels = "ЭэЕеИиӨөҮү"
local backvowels = "АаЫыОоУуЁёЯяЮю"
local novowels = "[^" .. frontvowels .. backvowels .. "]"
local consonants = "БбВвГгДдЖжЗзЙйКкЛлМмНнҢңПпРрСсТтФфХхЦцЧчШшЩщЪъЬь"
local vc = "КкГг"
local e = "Ее" -- hamza isnt used if the naxt syllable has an e

function export.tr(text, lang, sc)
	if type(text) == "table" then
		options = {}
		text, script = text.args[1], text.args[2]
	end
	
	if not sc then
		sc = require("Module:languages").getByCode("ky"):findBestScript(text):getCode()
	end
	
	if sc ~= "Cyrl" then
		return nil
	end

	text =
		mw.ustring.gsub(
		text,
		"([АОӨҮУЫЕЯЁЮИЕаоөүуыэяёюиеъь%A][́̀]?)([Ее])",
		function(a, e)
			return a .. (e == "е" and "йе" or "Йе")
		end
	):gsub("^Е", "Йе"):gsub("^е", "йе")
	-- conversons for КкГг -> ғқ
	text = mw.ustring.gsub(text, "К", "к")
	text = mw.ustring.gsub(text, "Г", "г")
	text = mw.ustring.gsub(text, "кк", "КК")
	text = mw.ustring.gsub(text, "гг", "ГГ")
	text = mw.ustring.gsub(text, "к([" .. frontvowels .. "])", "К%1")
	text = mw.ustring.gsub(text, "г([" .. frontvowels .. "])", "Г%1")
	text = mw.ustring.gsub(text, "к([" .. backvowels .. "])", "қ%1")
	text = mw.ustring.gsub(text, "г([" .. backvowels .. "])", "ғ%1")
	text = mw.ustring.gsub(text, "([" .. backvowels .. "])г", "%1ғ")
	text = mw.ustring.gsub(text, "([" .. backvowels .. "])к", "%1қ")
	-- other methods dont work at the end of the string
	-- so were gonna be lazy and capitals letters that shouldnt convert
	-- marking of Ии
	text = mw.ustring.gsub(text, "И", "и")
	text =
		mw.ustring.gsub(
		text,
		"([^" .. frontvowels .. consonants .. "])([" .. consonants .. "])и([" .. consonants .. "])([^" .. e .. "])",
		"%1%2И%3%4"
	)
	text = mw.ustring.gsub(text, "^([" .. consonants .. "])и([" .. consonants .. "])([^" .. e .. "])", "%1И%2%3")
	text = mw.ustring.gsub(text, "([" .. backvowels .. "])([" .. consonants .. "])и", "%1%2И")
	text = mw.ustring.gsub(text, "^и([" .. consonants .. "])([^" .. e .. "])", "И%1%2")
	text = mw.ustring.gsub(text, "([^" .. frontvowels .. "])([" .. consonants .. "])([" .. consonants .. "])и", "%1%2%3И")
	return (mw.ustring.gsub(text, ".", tt))
end

return export