Jump to content

Module:Mong-translit

From Wiktionary, the free dictionary

This module will transliterate text in the Mongolian script. 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 transliteration fails, returns nil.

local export = {}

local gsub = mw.ustring.gsub
local match = mw.ustring.match
local format = mw.ustring.format

local chars = {
	["ᠠ"] = "a", ["ᠡ"] = "e",
	["ᠢ"] = "i",
	["ᠣ"] = "o", ["ᠤ"] = "u",
	["ᠥ"] = "ö", ["ᠦ"] = "ü",
	["ᠧ"] = "ē",
	["ᠨ"] = "n",
	["ᠩ"] = "ng",
	["ᠪ"] = "b", ["ᠫ"] = "p",
	["ᠬ"] = "k", ["ᠭ"] = "g",
	["ᠮ"] = "m", ["ᠯ"] = "l",
	["ᠰ"] = "s", ["ᠱ"] = "š",
	["ᠲ"] = "t", ["ᠳ"] = "d",
	["ᠴ"] = "č", ["ᡸ"] = "š̤",
	["ᠵ"] = "ǰ", ["ᠶ"] = "y", ["ᠷ"] = "r",
	["ᠸ"] = "w",
	["ᠹ"] = "f",
	["ᠺ"] = "k", ["ᠻ"] = "k",
	["ᠼ"] = "c", ["ᠽ"] = "z",
	["ᠾ"] = "h",
	["ᠿ"] = "ř",
	["ᡀ"] = "lh",
	["ᡁ"] = "ž",
	["ᡂ"] = "č̭",
	["᠐"] = "0", ["᠑"] = "1", ["᠒"] = "2", ["᠓"] = "3",
	["᠔"] = "4", ["᠕"] = "5", ["᠖"] = "6", ["᠗"] = "7",
	["᠘"] = "8", ["᠙"] = "9",
	["᠀"] = "∞", ["᠁"] = "…", ["᠂"] = ",", ["᠃"] = ".", ["᠄"] = ":", ["᠅"] = "::",
	["︖"] = "?", ["︕"] = "!",
	[" "] = "-", ["᠊"] = "-", ["᠎"] = "-",
	["᠋"] = "", ["᠌"] = "", ["᠍"] = "", ["᠏"] = "",
}

local consonants_hg = "ᠬᠭ"

local consonants_hg2masculine = {
	["ᠬ"] = "q",
	["ᠭ"] = "ɣ",
}

local vowels_masculine = "ᠠᠣᠤ"
local vowels_feminine = "ᠡᠥᠦᠧ"

local vowels_before_yi = "ᠠᠡᠧᠣᠤᠥᠦ"

function export.tr(text, lang, sc)
	if sc ~= "Mong" then
		return nil
	end

	local bad_diphthong = match(text, "ᠥᠶ?ᠢ")
	if bad_diphthong then
		error(
			"Diphthong " .. bad_diphthong
			.. " does not exist. Please replace with "
			.. gsub(bad_diphthong, "ᠥ", "ᠦ") .. "."
		)
	end

	text = gsub(
		text,
		format("([%s][^%s]*)([%s])", vowels_masculine, vowels_feminine, consonants_hg),
		function(v, hg)
			return v .. consonants_hg2masculine[hg]
		end
	)

	text = gsub(
		text,
		format("([%s])([^%s]*[%s])", consonants_hg, vowels_feminine, vowels_masculine),
		function(hg, v)
			return consonants_hg2masculine[hg] .. v
		end
	)

	local plain_i = "@@PLAIN_I@@"
	local yi = "@@YI@@"

	text = gsub(text, "ᠢ᠍", plain_i)

	text = gsub(text, "ᠢ$", plain_i)

	text = gsub(
		text,
		format("([%s])ᠢ(.)", vowels_before_yi),
		"%1" .. yi .. "%2"
	)

	text = gsub(text, "ᠢ", plain_i)

	text = gsub(text, ".", chars)

	text = gsub(text, plain_i, "i")
	text = gsub(text, yi, "yi")

	return text
end

return export