มอดูล:Deva-translit

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

This module will transliterate text in the อักษรเทวนาครี. It is used to transliterate Haryanvi (bgc), ปรากฤต (inc-pra), มราฐีเก่า (omr), Palya Bareli (bpx), Bagheli (bfy), Kurukh (kru), มราฐี (mr), Mundari (unr), Bundeli (bns), เนวาร์ (new), เนปาล (ne), Southeastern Kolami (nit), โบโด (brx), พรัช (bra), ฮินดีเก่า (inc-ohi), สันสกฤต (sa), ฮินดี (hi), กงกัณ (kok), บาลี (pi), อวัธ (awa), Bhadrawahi (bhd), Mahasu Pahari (bfz), and Kolami (kfb). 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:Deva-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(0x0929)] = 'น̱',
	['प'] = 'ป', ['फ'] = 'ผ', ['ब'] = 'พ', ['भ'] = 'ภ', ['म'] = 'ม',
	['य'] = 'ย', ['र'] = 'ร', [u(0x0931)] = 'ร̱', ['ल'] = 'ล', ['ळ'] = 'ฬ', [u(0x0934)] = 'ฬ̱', ['व'] = 'ว',
	['श'] = 'ศ', ['ष'] = 'ษ', ['स'] = 'ส', ['ह'] = 'ห',
	[u(0x0958)] = 'ก̱', [u(0x0959)] = 'ฃ', [u(0x095A)] = 'ฅ',
	[u(0x095B)] = 'ซ', [u(0x095C)] = 'ฑ̱', [u(0x095D)] = 'ฒ̱',
	[u(0x095E)] = 'ฝ', [u(0x095F)] = 'ย̱', ['ॹ'] = 'ซ̱', ['ॺ'] = 'ย͇',
	['ॻ'] = 'ค͓', ['ॼ'] = 'ช͓', ['ॽ'] = 'อ', ['ॾ'] = 'ฑ͓', ['ॿ'] = 'พ͓',
	-- independent vowels
	['अ'] = 'อ', ['आ'] = 'อา', ['इ'] = 'อิ', ['ई'] = 'อี',
	['ॶ'] = 'อึ', ['ॷ'] = 'อื', ['उ'] = 'อุ', ['ऊ'] = 'อู',
	['ऋ'] = 'ฤ', ['ॠ'] = 'ฤๅ', ['ऌ'] = 'ฦ', ['ॡ'] = 'ฦๅ',
	['ऍ'] = 'แอ', ['ऎ'] = 'เอะ', ['ए'] = 'เอ', ['ऐ'] = 'ไอ',
	['ऑ'] = 'ออ̂', ['ऒ'] = 'โอะ', ['ओ'] = 'โอ', ['औ'] = 'เอา',
	['ॳ'] = 'เออ̂ะ', ['ॴ'] = 'เออ̂',
	-- dependent vowels and diacritics (excluding front type)
	['ा'] = 'า', ['ि'] = 'ิ', ['ी'] = 'ี',
	['ॖ'] = 'ึ', ['ॗ'] = 'ื', ['ु'] = 'ุ', ['ू'] = 'ู',
	['ृ'] = 'ฺฤ', ['ॄ'] = 'ฺฤๅ', ['ॢ'] = 'ฺฦ', ['ॣ'] = 'ฺฦๅ',
	['ॉ'] = 'อ̂', ['ं'] = 'ํ', ['ः'] = 'ห์', ['्'] = 'ฺ',
	['़'] = u(0x0331), -- macron below
	['ऀ'] = 'ํ์', -- fermata (inverted candrabindu)
	['ँ'] = 'ํ์', -- candrabindu
	['॑'] = u(0x030D), -- vertical line above
	['॒'] = u(0x0332), -- low line
	['॓'] = u(0x0300), -- grave accent
	['॔'] = u(0x0301), -- acute accent
	-- 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 adjust0 = {
	-- for convenience
	['न'..'़'] = u(0x0929), ['र'..'़'] = u(0x0931), ['ळ'..'़'] = u(0x0934),
	['क'..'़'] = u(0x0958), ['ख'..'़'] = u(0x0959), ['ग'..'़'] = u(0x095A),
	['ज'..'़'] = u(0x095B), ['ड'..'़'] = u(0x095C), ['ढ'..'़'] = u(0x095D),
	['फ'..'़'] = u(0x095E), ['य'..'़'] = u(0x095F),
}

local adjust1 = {
	-- dependent vowels (front type)
	['ॅ'] = 'แ%1', ['ॆ'] = 'เ%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

	if lang == 'ne' then -- unused marks from ne-l
		text = gsub(text, '[<>•ॱ]', '')
	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')
	text = gsub(text, 'ฯฯ', '๚')
	
	-- ย้ายสัญลักษณ์ขึ้นบน เมื่อมีสระล่าง (ยกเว้นตัวที่ไม่มี)
	text = gsub(text, u(0x0331)..'([ุ-ฺ])', u(0x0304)..'%1') -- macron below > macron above
	text = gsub(text, u(0x0332)..'([ุ-ฺ])', u(0x0305)..'%1') -- low line > overline
	text = gsub(text, u(0x0353)..'([ุ-ฺ])', u(0x033D)..'%1') -- x below > x above
	
	-- ย้ายสัญลักษณ์ลงล่าง เมื่อมีสระบน (ยกเว้นตัวที่ไม่มี)
	text = gsub(text, u(0x030D)..'([ัิ-ื็-๎])', u(0x0329)..'%1') -- vertical line above > vertical line below
	text = gsub(text, u(0x0300)..'([ัิ-ื็-๎])', u(0x0316)..'%1') -- grave accent > grave accent below
	text = gsub(text, u(0x0301)..'([ัิ-ื็-๎])', u(0x0317)..'%1') -- acute accent > acute accent below
	
	return text

end

return export