This module will transliterate text in the อักษรทิเบต. It is used to transliterate ทิเบต (bo), Nyenkha (neh), Chocangaca (cgk), Lunanakha (luk), สิกขิม (sip), Dakpa (dka), Brokkat (bro), Brokpake (sgt), Bokar (sit-bok), Zangskari (zau), Adap (adp), Balti (bft), Kurtöp (xkz), Gahri (bfu), ซองคา (dz), Sherpa (xsr), ทิเบตคลาสสิก (xct), Idu (clk), Khengkha (xkf), Kalaktang Monpa (kkf), ทิเบตเก่า (otb), Tshangla (tsj), Changthang (cna), Dzala (dzl), Manangba (nmm), สันสกฤต (sa), Tawang Monpa (twm), Olekha (ole), Kutang Ghale (ght), Chali (tgf), Layakha (lya), Ladakhi (lbj), and Bumthangkha (kjz). 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:Tibt-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
	['ཀ'] = 'ก', ['ཁ'] = 'ข', ['ག'] = 'ค', [u(0x0F43)] = 'ฆ', ['ང'] = 'ง',
	['ཅ'] = 'จ', ['ཆ'] = 'ฉ', ['ཇ'] = 'ช', ['ཉ'] = 'ญ',
	['ཊ'] = 'ฏ', ['ཋ'] = 'ฐ', ['ཌ'] = 'ฑ', [u(0x0F4D)] = 'ฒ', ['ཎ'] = 'ณ',
	['ཏ'] = 'ต', ['ཐ'] = 'ถ', ['ད'] = 'ท', [u(0x0F52)] = 'ธ', ['ན'] = 'น',
	['པ'] = 'ป', ['ཕ'] = 'ผ', ['བ'] = 'พ', [u(0x0F57)] = 'ภ', ['མ'] = 'ม',
	['ཙ'] = 'จ́', ['ཚ'] = 'ฉ́', ['ཛ'] = 'ช́', [u(0x0F5C)] = 'ฌ́', ['ཝ'] = 'ว',
	['ཞ'] = 'ซ̱', ['ཟ'] = 'ซ', ['འ'] = 'อ͓', ['ཡ'] = 'ย', ['ར'] = 'ร',
	['ལ'] = 'ล', ['ཤ'] = 'ศ', ['ཥ'] = 'ษ', ['ས'] = 'ส', ['ཧ'] = 'ห',
	['ཨ'] = 'อ', [u(0x0F69)] = 'กฺษ', ['ཪ'] = 'ร', ['ཫ'] = 'ก̱', ['ཬ'] = 'ร̱',
	-- subjoined consonants
	['ྐ'] = 'ฺก', ['ྑ'] = 'ฺข', ['ྒ'] = 'ฺค', [u(0x0F93)] = 'ฺฆ', ['ྔ'] = 'ฺง',
	['ྕ'] = 'ฺจ', ['ྖ'] = 'ฺฉ', ['ྗ'] = 'ฺช', ['ྙ'] = 'ฺญ',
	['ྚ'] = 'ฺฏ', ['ྛ'] = 'ฺฐ', ['ྜ'] = 'ฺฑ', [u(0x0F9D)] = 'ฺฒ', ['ྞ'] = 'ฺณ',
	['ྟ'] = 'ฺต', ['ྠ'] = 'ฺถ', ['ྡ'] = 'ฺท', [u(0x0FA2)] = 'ฺธ', ['ྣ'] = 'ฺน',
	['ྤ'] = 'ฺป', ['ྥ'] = 'ฺผ', ['ྦ'] = 'ฺพ', [u(0x0FA7)] = 'ฺภ', ['ྨ'] = 'ฺม',
	['ྩ'] = 'ฺจ́', ['ྪ'] = 'ฺฉ́', ['ྫ'] = 'ฺช́', [u(0x0FAC)] = 'ฺฌ́', ['ྭ'] = 'ฺว',
	['ྮ'] = 'ฺซ̱', ['ྯ'] = 'ฺซ', ['ྰ'] = 'ฺอ͓', ['ྱ'] = 'ฺย', ['ྲ'] = 'ฺร',
	['ླ'] = 'ฺล', ['ྴ'] = 'ฺศ', ['ྵ'] = 'ฺษ', ['ྶ'] = 'ฺส', ['ྷ'] = 'ฺห',
	['ྸ'] = 'ฺอ', [u(0x0FB9)] = 'ฺกฺษ', ['ྺ'] = 'ฺว', ['ྻ'] = 'ฺย', ['ྼ'] = 'ฺร',
	-- dependent vowels and diacritics (excluding front type)
	['ཱ'] = 'า', ['ི'] = 'ิ', [u(0x0F73)] = 'ี', ['ུ'] = 'ุ', [u(0x0F75)] = 'ู',
	[u(0x0F76)] = 'ฺฤ', [u(0x0F77)] = 'ฺฤๅ', [u(0x0F78)] = 'ฺฦ', [u(0x0F79)] = 'ฺฦๅ',
	['ཾ'] = 'ํ', ['ཿ'] = 'ห์', ['ྀ'] = 'ิ', [u(0x0F81)] = 'ี',
	['ྂ'] = 'ม̐', ['ྃ'] = 'ม̐', ['྄'] = 'ฺ', ['༹'] = u(0x0301),
	-- marks
	['྅'] = '-', ['་'] = u(0x2027), ['༌'] = u(0x2027), ['།'] = 'ฯ', ['༎'] = '๚',
	['༺'] = '[', ['༻'] = ']', ['༼'] = '{', ['༽'] = '}',
	['༵'] = '⁎', ['༶'] = '⁘', ['༷'] = '•',
	['ༀ'] = 'โอม̐',
	-- numerals
	['༠'] = '0', ['༡'] = '1', ['༢'] = '2', ['༣'] = '3', ['༤'] = '4',
	['༥'] = '5', ['༦'] = '6', ['༧'] = '7', ['༨'] = '8', ['༩'] = '9',
	['༪'] = '½', ['༫'] = '1½', ['༬'] = '2½', ['༭'] = '3½', ['༮'] = '4½',
	['༯'] = '5½', ['༰'] = '6½', ['༱'] = '7½', ['༲'] = '8½', ['༳'] = '−½',
	-- zero-width space (display it if it hides in a word)
	[u(0x200B)] = '‼',
}

local adjust0 = {
	-- for convenience
	['ཱ'..'ི'] = u(0x0F73), ['ཱ'..'ུ'] = u(0x0F75), ['ཱ'..'ྀ'] = u(0x0F81),
	['ྲ'..'ྀ'] = u(0x0F76), ['ྲ'..u(0x0F81)] = u(0x0F77),
	['ླ'..'ྀ'] = u(0x0F78), ['ླ'..u(0x0F81)] = u(0x0F79),
	['ག'..'ྷ'] = u(0x0F43), ['ཌ'..'ྷ'] = u(0x0F4D), ['ད'..'ྷ'] = u(0x0F52),
	['བ'..'ྷ'] = u(0x0F57), ['ཛ'..'ྷ'] = u(0x0F5C), ['ཀ'..'ྵ'] = u(0x0F69),
	['ྒ'..'ྷ'] = u(0x0F93), ['ྜ'..'ྷ'] = u(0x0F9D), ['ྡ'..'ྷ'] = u(0x0FA2),
	['ྦ'..'ྷ'] = u(0x0FA7), ['ྫ'..'ྷ'] = u(0x0FAC), ['ྐ'..'ྵ'] = u(0x0FB9),
}

local adjust1 = {
	-- dependent vowels (front type)
	['ེ'] = 'เ%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)
	text = gsub(text, 'ชฺห', 'ฌ') -- ทวิอักษรที่ขาดไป

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

	text = gsub(text, 'ฯฯ', '๚')

	-- ย้ายสัญลักษณ์ขึ้นบน เมื่อมีสระล่าง (ยกเว้นตัวที่ไม่มี)
	text = gsub(text, u(0x0331)..'([ุ-ฺ])', u(0x0304)..'%1') -- macron below > macron above
	text = gsub(text, u(0x0353)..'([ุ-ฺ])', u(0x033D)..'%1') -- x below > x above

	-- ย้ายสัญลักษณ์ลงล่าง เมื่อมีสระบน (ยกเว้นตัวที่ไม่มี)
	text = gsub(text, u(0x0301)..'([ัิ-ื็-๎])', u(0x0317)..'%1') -- acute accent > acute accent below

	return text
 
end
 
return export