This module will transliterate text in the อักษรเขมร. It is used to transliterate สันสกฤต (sa), บาลี (pi), เขมรเก่า (okz), เขมร (km), and เขมรกลาง (xhm). 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:Khmr-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 gsub = mw.ustring.gsub
local u = mw.ustring.char
local letter_with_mark = "(.["..u(0x0300).."-"..u(0x036F).."]?)"

local tt = {
	-- consonants
	["ក"] = "ก", ["ខ"] = "ข", ["គ"] = "ค", ["ឃ"] = "ฆ", ["ង"] = "ง",
	["ច"] = "จ", ["ឆ"] = "ฉ", ["ជ"] = "ช", ["ឈ"] = "ฌ", ["ញ"] = "ญ",
	["ដ"] = "ฎ", ["ឋ"] = "ฐ", ["ឌ"] = "ฑ", ["ឍ"] = "ฒ", ["ណ"] = "ณ",
	["ត"] = "ต", ["ថ"] = "ถ", ["ទ"] = "ท", ["ធ"] = "ธ", ["ន"] = "น",
	["ប"] = "บ", ["ផ"] = "ผ", ["ព"] = "พ", ["ភ"] = "ภ", ["ម"] = "ม",
	["យ"] = "ย", ["រ"] = "ร", ["ល"] = "ล", ["វ"] = "ว", ["ឝ"] = "ศ",
	["ឞ"] = "ษ", ["ស"] = "ส", ["ហ"] = "ห", ["ឡ"] = "ฬ", ["អ"] = "อ",
	-- independent vowels
	["ឣ"] = "อ", ["ឤ"] = "อา", -- discouraged use
	["ឥ"] = "อิ", ["ឦ"] = "อี",
	["ឧ"] = "อุ", ["ឨ"] = "อุก", ["ឩ"] = "อู", ["ឪ"] = "อัว",
	["ឫ"] = "ฤ", ["ឬ"] = "ฤๅ", ["ឭ"] = "ฦ", ["ឮ"] = "ฦๅ",
	["ឯ"] = "เอ", ["ឰ"] = "ไอ", ["ឱ"] = "โอ", ["ឲ"] = "โอ", ["ឳ"] = "เอา",
	-- dependent vowels and diacritics (excluding front type)
	[u(0x17B4)] = "า̍", [u(0x17B5)] = "า", -- invisible
	["ា"] = "า", ["ិ"] = "ิ", ["ី"] = "ี",
	["ឹ"] = "ึ", ["ឺ"] = "ื", ["ុ"] = "ุ", ["ូ"] = "ู", ["ួ"] = "ัว",
	["ំ"] = "ํ", ["ះ"] = "ะ", ["ៈ"] = "ะ̤", ["៉"] = "̎", ["៊"] = "̃",
	["់"] = "̍", ["៍"] = "์", ["៎"] = "๋", ["៏"] = "็", ["័"] = "ั",
	["៑"] = "ฺ", ["្"] = "ฺ",
	-- marks
	["។"] = "ฯ", ["៕"] = "๚", ["៖"] = ":-", ["ៗ"] = "ๆ", ["៘"] = "ฯลฯ",
	["៙"] = "๏", ["៚"] = "๛", ["៛"] = "ร̵", ["ៜ"] = "-",
	-- numerals
	["០"] = "๐", ["១"] = "๑", ["២"] = "๒", ["៣"] = "๓", ["៤"] = "๔",
	["៥"] = "๕", ["៦"] = "๖", ["៧"] = "๗", ["៨"] = "๘", ["៩"] = "๙",
	["៰"] = "0", ["៱"] = "1", ["៲"] = "2", ["៳"] = "3", ["៴"] = "4",
	["៵"] = "5", ["៶"] = "6", ["៷"] = "7", ["៸"] = "8", ["៹"] = "9",
	-- zero-width space (display it if it hides in a word)
	[u(0x200B)] = "‼", [u(0x200C)] = "‼", [u(0x200D)] = "‼",
}

local adjust1 = {
	["าํ"] = "ำ", ["าั"] = "ัา",
}

local adjust2 = {
	-- dependent vowels (front type)
	["ើ"] = "เ%1ิ", ["ឿ"] = "เ%1ือ", ["ៀ"] = "เ%1ีย", ["េ"] = "เ%1",
	["ែ"] = "แ%1", ["ៃ"] = "ไ%1", ["ោ"] = "โ%1", ["ៅ"] = "เ%1า",
	-- other
	["៌"] = "รฺ%1",
}

function export.tr(text, lang, sc, debug_mode)

	if type(text) == "table" then -- called directly from a template
		text = text.args[1]
	end

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

	for k, v in pairs(adjust1) do
		text = gsub(text, k, v)
	end
	for k, v in pairs(adjust2) do
		text = gsub(text, letter_with_mark..k, v)
	end

	text = gsub(text, "([เแไโ])อฺ", "อฺ%1")

	if lang == "pi" or lang == "sa" or lang == "okz" or lang == "xhm" then
		text = gsub(text, "ฎ", "ฏ")
		text = gsub(text, "บ", "ป")
	end

	-- ย้ายสัญลักษณ์ลงล่าง เมื่อมีสระบนและวรรณยุกต์
	text = gsub(text, u(0x0303).."([ัำิ-ื็-๎])", u(0x0330).."%1") -- tilde above > tilde below
	text = gsub(text, u(0x030E).."([ัำิ-ื็-๎])", u(0x0348).."%1") -- fang above > fang below

	return text

end

return export