This module will transliterate text in the อักษรสิงหล. It is used to transliterate บาลี, สันสกฤต, and สิงหล. 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:Sinh-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
	["අ"] = "อ", ["ආ"] = "อา", ["ඇ"] = "แอ็", ["ඈ"] = "แอ",
	["ඉ"] = "อิ", ["ඊ"] = "อี", ["උ"] = "อุ", ["ඌ"] = "อู",
	["ඍ"] = "ฤ", ["ඎ"] = "ฤๅ", ["ඏ"] = "ฦ", ["ඐ"] = "ฦๅ",
	["එ"] = "เอ็", ["ඒ"] = "เอ", ["ඓ"] = "ไอ", ["ඔ"] = "โอ็", ["ඕ"] = "โอ", ["ඖ"] = "เอา",
	-- dependent vowels and diacritics (excluding front type)
	["ා"] = "า", ["ි"] = "ิ", ["ී"] = "ี", ["ු"] = "ุ", ["ූ"] = "ู",
	["ෘ"] = "ฺฤ", ["ෲ"] = "ฺฤๅ", ["ෟ"] = "ฺฦ", ["ෳ"] = "ฺฦๅ",
	["ං"] = "ํ", ["ඃ"] = "ะ", ["්"] = "ฺ",
	-- marks
	["෴"] = ".",
	-- numerals
	["෦"] = "0", ["෧"] = "1", ["෨"] = "2", ["෩"] = "3", ["෪"] = "4",
	["෫"] = "5", ["෬"] = "6", ["෭"] = "7", ["෮"] = "8", ["෯"] = "9",
	["𑇡"] = "1", ["𑇢"] = "2", ["𑇣"] = "3", ["𑇤"] = "4",
	["𑇥"] = "5", ["𑇦"] = "6", ["𑇧"] = "7", ["𑇨"] = "8", ["𑇩"] = "9",
	["𑇪"] = "[10]", ["𑇫"] = "[20]", ["𑇬"] = "[30]", ["𑇭"] = "[40]",
	["𑇮"] = "[50]", ["𑇯"] = "[60]", ["𑇰"] = "[70]", ["𑇱"] = "[80]", ["𑇲"] = "[90]",
	["𑇳"] = "[100]", ["𑇴"] = "[1000]",
	-- zero-width space (display it if it hides in a word)
	[u(0x200B)] = "‼",
	-- zero-width joiner (display it if it hides in a word)
	[u(0x200D)] = "₊",
}

local adjust0 = {
	-- for convenience
	["අ".."ා"] = "ආ", ["අ".."ැ"] = "ඇ", ["අ".."ෑ"] = "ඈ",
	["උ".."ෟ"] = "ඌ", ["ඍ".."ෘ"] = "ඎ", ["ඏ".."ෟ"] = "ඐ",
	["එ".."්"] = "ඒ", ["එ".."ෙ"] = "ඓ", ["ඔ".."ෟ"] = "ඖ",
	["ෙ".."්"] = "ේ", ["ෙ".."ෙ"] = "ෛ",
	["ෙ".."ා"] = "ො", ["ො".."්"] = "ෝ", ["ෙ".."ෟ"] = "ෞ",
}

local adjust1 = {
	-- dependent vowels (front type)
	["ැ"] = "แ%1็", ["ෑ"] = "แ%1",
	["ෙ"] = "เ%1็", ["ේ"] = "เ%1", ["ෛ"] = "ไ%1",
	["ො"] = "โ%1็", ["ෝ"] = "โ%1", ["ෞ"] = "เ%1า",
}

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

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

	for k, v in pairs(adjust0) do
		text = gsub(text, k, v)
	end
	text = gsub(text, ".", tt)

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

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

	if lang == "pi" or lang == "sa" then
		text = gsub(text, "็", "")
	end

	return text

end

return export