This module will transliterate text in the อักษรอ็อลจิกิ. It is used to transliterate สันถาลี. 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:Olck-translit/testcases.

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 tt1 = {
	-- consonants
	["ᱠ"] = "ก", ["ᱜ"] = "ค", ["ᱝ"] = "ง",
	["ᱪ"] = "จ", ["ᱡ"] = "ช", ["ᱧ"] = "ญ",
	["ᱴ"] = "ฏ", ["ᱰ"] = "ฑ", ["ᱬ"] = "ณ",
	["ᱛ"] = "ต", ["ᱫ"] = "ท", ["ᱱ"] = "น",
	["ᱯ"] = "ป", ["ᱵ"] = "พ", ["ᱢ"] = "ม",
	["ᱭ"] = "ย", ["ᱨ"] = "ร", ["ᱞ"] = "ล", ["ᱣ"] = "ว",
	["ᱥ"] = "ส", ["ᱦ"] = "ห",
	["ᱲ"] = "ฑ̱", ["ᱶ"] = "ว̱", ["ᱷ"] = "ห̱",
	-- vowels
	["ᱚ"] = "็อ̂", ["ᱟ"] = "ะ", ["ᱤ"] = "ิ",
	["ᱩ"] = "ุ", ["ᱮ"] = "↶เะ", ["ᱳ"] = "↶โะ",
	-- diacritics and symbols
	["ᱸ"] = "ํ",
	["ᱹ"] = ".", ["ᱺ"] = ":", ["ᱻ"] = "~", -- fallback if fail to match digraphs
	["ᱼ"] = "-", ["ᱽ"] = "͓",
	["᱾"] = "ฯ", ["᱿"] = "๚",
	-- 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 tt2 = {
	-- consonant digraphs
	["ᱠᱷ"] = "ข", ["ᱜᱷ"] = "ฆ", ["ᱪᱷ"] = "ฉ", ["ᱡᱷ"] = "ฌ",
	["ᱴᱷ"] = "ฐ", ["ᱰᱷ"] = "ฒ", ["ᱛᱷ"] = "ถ", ["ᱫᱷ"] = "ธ",
	["ᱯᱷ"] = "ผ", ["ᱵᱷ"] = "ภ", ["ᱲᱷ"] = "ฒ̱",
	-- vowel digraphs
	["ᱟᱹ"] = "↶เอ̂ะ", ["ᱮᱹ"] = "↶แะ", ["ᱟᱺ"] = "↶เอ̂ะํ", ["ᱮᱺ"] = "↶แะํ",
	["ᱚᱻ"] = "อ̂", ["ᱟᱻ"] = "า", ["ᱤᱻ"] = "ี",
	["ᱩᱻ"] = "ู", ["ᱮᱻ"] = "↶เ", ["ᱳᱻ"] = "↶โ",
}

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, "([ᱚᱟᱤᱩᱮᱳ]ᱹ?ᱺ?ᱻ?)([ᱚᱟᱤᱩᱮᱳ])", "%1อ%2")

	text = gsub(text, ".ᱷ", tt2)
	text = gsub(text, ".[ᱹᱺᱻ]", tt2)
	text = gsub(text, ".", tt1)

	-- adds อ at the begining
	text = gsub(text, "^([ะัาิีุู็↶])", "อ%1")
	text = gsub(text, "([%s%p])([ะัาิีุู็↶])", "%1อ%2")
	text = gsub(text, "^(อ̂)", "อ%1")
	text = gsub(text, "([%s%p])(อ̂)", "%1อ%2")

	text = gsub(text, "([ก-ฮ]̱?͓?)↶([เแโ])", "%2%1")

	return text

end

return export