ข้ามไปเนื้อหา

มอดูล:Bali-translit

จาก วิกิพจนานุกรม พจนานุกรมเสรี

This module will transliterate text in the อักษรบาหลี. 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:Bali-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)
	["ᬵ"] = "า", ["ᬶ"] = "ิ", ["ᬷ"] = "ี", ["ᬸ"] = "ุ", ["ᬹ"] = "ู",
	["ᬺ"] = "ฺฤ", ["ᬻ"] = "ฺฤๅ", ["ᬼ"] = "ฺฦ", ["ᬽ"] = "ฺฦๅ",
	["ᬂ"] = "ํ", ["ᬃ"] = "รฺ", ["ᬄ"] = "ะ", ["᭄"] = "ฺ",
	["᬴"] = u(0x0331), -- macron below
	["ᬀ"] = "ํ์", -- fermata (inverted candrabindu)
	["ᬁ"] = "ํ์", -- candrabindu
	-- 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)] = "‼",
}

local adjust1 = {
	-- dependent vowels (front type)
	["ᬾ"] = "เ%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

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

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

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

end

return export