Module:kk-Cyrl-Arab-translit

From Linguifex
Jump to navigation Jump to search

This module will transliterate Kazakh language text. 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:kk-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 rsplit = mw.text.split
local U = mw.ustring.char
local rsubn = mw.ustring.gsub
local ulen = mw.ustring.len

local frontvowels = "үҮәӘіІөӨ"
local backvowels = "ӯӮАаоОЫыҰұ"
local allvowels = "үҮиИеЕәӘөӨӯӮАаоОЫыІіЯяЮюЁё"
local allchar = "АаӘәБбВвГгҒғДдЕеЁёЖжЗзИиЙйКкҚқЛлМмНнҢңОоӨөПпРрСсТтУуҰұҮүФфХхҺһЦцЧчШшЩщЪъЫыІіЬьЭэЮюЯя"
local hmzh = U(0x674)

local mapping = {
 ["ү"] = "ۇ"
,["Ү"] = "ۇ"
,["т"] = "ت"
,["Т"] = "ت"
,["һ"] = "ھ"
,["Һ"] = "ھ"
,["р"] = "ر"
,["Р"] = "ر"
,["ф"] = "ف"
,["Ф"] = "ف"
,["ш"] = "ش"
,["Ш"] = "ش"
,["н"] = "ن"
,["Н"] = "ن"
,["п"] = "پ"
,["П"] = "پ"
,["й"] = "ي"
,["Й"] = "ي"
,["л"] = "ل"
,["Л"] = "ل"
,["з"] = "ز"
,["З"] = "ز"
,["е"] = "ە"
,["Е"] = "ە"
,["г"] = "گ"
,["Г"] = "گ"
,["б"] = "ب"
,["Б"] = "ب"
,["у"] = "ۋ"
,["У"] = "ۋ"
,["с"] = "س"
,["С"] = "س"
,["х"] = "ح"
,["Х"] = "ح"
,["ы"] = "ى"
,["Ы"] = "ى"
,["м"] = "م"
,["М"] = "م"
,["о"] = "و"
,["О"] = "و"
,["и"] = "ي"
,["И"] = "ي"
,["ж"] = "ج"
,["Ж"] = "ج"
,["к"] = "ك"
,["К"] = "ك"
,["д"] = "د"
,["Д"] = "د"
,["в"] = "ۆ"
,["В"] = "ۆ"
,["а"] = "ا"
,["А"] = "ا"
,["ң"] = "ڭ"
,["Ң"] = "ڭ"
,["ә"] = "ا"
,["Ә"] = "ا"
,["э"] = "ە"
,["Э"] = "ە"
,["ұ"] = "ۇ"
,["Ұ"] = "ۇ"
,["қ"] = "ق"
,["Қ"] = "ق"
,["ғ"] = "ع"
,["Ғ"] = "ع"
,["і"] = "ى"
,["І"] = "ى"--?
,["ө"] = "و"
,["Ө"] = "و"


--mostly in loanwords from Russian
,["ё"] = "يو"
,["Ё"] = "يو"
,["ц"] = "تس"
,["Ц"] = "تس"
,["ч"] = "چ"
,["Ч"] = "چ"
,["щ"] = "شش"
,["Щ"] = "شش"
,["ъ"] = ""
,["Ъ"] = ""
,["ь"] = ""
,["Ь"] = ""
,["э"] = "ە"
,["Э"] = "ە"
,["ю"] = "يۋ"
,["Ю"] = "يۋ"
,["я"] = "يا"
,["Я"] = "يا"
--хостинг - hostiñ
,["нг"] = "ڭ"
,["Нг"] = "ڭ"
--is it used?
,["ӯ"] = "ۇ"
,["Ӯ"] = "ۇ"

--punctuation 
,[","] = "،"
,["%"] = "٪"
,["?"] = "؟"
}

--[==[ disabled: hamza ligatures have been depricated by unicode
local function fvowels(text)
	-- front vowels
	text = rsubn(text, hmzh .. "[үҮ]", "ٷ")
	text = rsubn(text, hmzh .. "[Әә]", "ٵ")
	text = rsubn(text, hmzh .. "[Іі]", "ٸ")
	text = rsubn(text, hmzh .. "[Өө]", "ٶ")
	text = rsubn(text, hmzh .. "([" .. backvowels .. "])", "%1")
	return text
end ]==]

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("kk"):findBestScript(text):getCode()
	end
	
	if sc ~= "Cyrl" then
		return nil
	end
	text = rsubn(text, "^" .. "[-]", "-")
	text = rsubn(text, "([" .. allchar .. "])([^" .. allchar .. "])", "%1#_#%2")
	text = rsubn(text, "([^" .. allchar .. "])([" .. allchar .. "])", "%1#_#%2")
	text = rsubn(text, " | ", "# | #")
	text = "##" .. rsubn(text, " ", "# #") .. "##"
	-- treat every word as an individual string
	rsplit( "#" .."%s" .. "#", ",")
	rsplit( "#" .."_" .. "#", ",")
	text = rsubn(text, "[иИ][яЯ]", "йа")
	text = rsubn(text, "[иИ]([".. allvowels .."])", "й%1")
	text = rsubn(text, "([".. allvowels .."])[яЯ]", "%1йа")
	text = rsubn(text, "#[яЯ]", "#йа")
	text = rsubn(text, "[яЯ]", "ә")
	text = rsubn(text, "#([" .. allchar .. "]+)([" .. frontvowels .. "])", "#" .. hmzh .. "%1%2")
	text = rsubn(text, "#([" .. frontvowels .. "])", "#" .. hmzh .. "%1")
	text = rsubn(text, "#" .. hmzh .. "([" .. allchar .. allvowels .. "]+)([эЭЕеГгҒғКкҚқ])", "#%1%2")
	text = rsubn(text, "#" .. hmzh .. "([эЭЕеГгКк])", "#%1")
	-- text = fvowels(text) -- hamza ligatures have been depricated by unicode
	text = rsubn(text, '.', mapping)
	text = rsubn(text,"-", "ـ") -- Kashida
	text = rsubn(text, "#", "")
	text = rsubn(text, "_", "")

	return text
end
return export