Module:Mong-translit

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

This module will transliterate text in the Mongolian script. It is used to transliterate Buryat, Classical Mongolian, Daur, Mongolian, Sanskrit, Dukhan, Classical Tibetan, Middle Mongol, and Khamnigan Mongol. 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:Mong-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 oneChar = {
	["ᠠ"] = "a", ["ᠡ"] = "ä", ["ᠢ"] = "i", ["ᠣ"] = "o", ["ᠤ"] = "u", ["ᠥ"] = "ö", ["ᠦ"] = "ü", ["ᠧ"] = "e", ["ᠨ"] = "n", ["ᠩ"] = "ŋ", ["ᠪ"] = "b", ["ᠫ"] = "p", ["ᠬ"] = "q", ["ᠭ"] = "ɣ", ["ᠮ"] = "m", ["ᠯ"] = "l", ["ᠰ"] = "s", ["ᠱ"] = "š", ["ᠲ"] = "t", ["ᠳ"] = "d", ["ᠴ"] = "č", ["ᡸ"] = "š̤", ["ᠵ"] = "ǰ", ["ᠶ"] = "y", ["ᠷ"] = "r", ["ᠸ"] = "w", ["ᠹ"] = "f", ["ᠺ"] = "k", ["ᠻ"] = "kh", ["ᠼ"] = "c", ["ᠽ"] = "z", ["ᠾ"] = "h", ["ᠿ"] = "ž", ["ᡀ"] = "lh", ["ᡁ"] = "zh", ["ᡂ"] = "ch",
	["᠐"] = "0", ["᠑"] = "1", ["᠒"] = "2", ["᠓"] = "3", ["᠔"] = "4", ["᠕"] = "5", ["᠖"] = "6", ["᠗"] = "7", ["᠘"] = "8", ["᠙"] = "9",
	["᠂"] = ",", ["᠃"] = ".", ["᠄"] = ":", ["᠁"] = "…", ["︖"] = "?", ["︕"] = "!", [" "] = "-", ["᠊"] = "-",
	["᠋"] = "", ["᠌"] = "", ["᠍"] = "", ["᠏"] = "", ["᠎"] = "-"
}

local twoChars = {
	["ᠠᠢ"] = "ai̯", ["ᠡᠢ"] = "äi̯", ["ᠣᠢ"] = "oi̯", ["ᠤᠢ"] = "ui̯", ["ᠦᠢ"] = "üi̯", ["ᠧᠢ"] = "ei̯", ["ᠬᠡ"] = "kä", ["ᠭᠡ"] = "gä", ["ᠬᠢ"] = "ki", ["ᠭᠢ"] = "gi", ["ᠬᠧ"] = "ke", ["ᠭᠧ"] = "ge", ["ᠬᠥ"] = "kö", ["ᠭᠥ"] = "gö", ["ᠬᠦ"] = "kü", ["ᠭᠦ"] = "gü"
}

function export.tr(text, lang, sc)
	if sc ~= "Mong" then
		return nil
	elseif mw.ustring.match(text, "ᠥᠢ") or mw.ustring.match(text, "ᠥᠶᠢ") then
		local diphthong = mw.ustring.match(text, "ᠥᠶ?ᠢ")
		error("Diphthong " .. diphthong .. " does not exist. Please replace with " .. mw.ustring.gsub(diphthong, "ᠥ", "ᠦ") .. ".")
	end
	for digraph, replacement in pairs(twoChars) do
		text = string.gsub(text, digraph, replacement)
	end
	text = mw.ustring.gsub(text, ".", oneChar)
	text = mw.ustring.gsub(text, "q([eöü ])", "k%1")
	text = mw.ustring.gsub(text, "q$", "k")
	text = mw.ustring.gsub(text, "ɣ([eöü ])", "g%1")
	text = mw.ustring.gsub(text, "ɣ$", "g")
	return text
end

return export