Module:ml-translit

From Linguifex
Revision as of 00:39, 1 April 2026 by wikt>Theknightwho (Avoid explicit u-diacritic + virama displaying as "ŭŭ", and only elide before a vowel if there's no u-diacritic before the virama. Also bugfixes.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This module will transliterate Malayalam language text. It is also used to transliterate Malavedan, Mannan, and Muduga. 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:ml-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 ugsub = mw.ustring.gsub

local VIRAMA = '്'
local CHANDRAKKALA = '\1'
 
local consonants = {
	['ക']='k', ['ഖ']='kh', ['ഗ']='g', ['ഘ']='gh', ['ങ']='ṅ', 
	['ച']='c', ['ഛ']='ch', ['ജ']='j', ['ഝ']='jh', ['ഞ']='ñ', 
	['ട']='ṭ', ['ഠ']='ṭh', ['ഡ']='ḍ', ['ഢ']='ḍh', ['ണ']='ṇ', 
	['ത']='t', ['ഥ']='th', ['ദ']='d', ['ധ']='dh', ['ന']='n', 
	['പ']='p', ['ഫ']='ph', ['ബ']='b', ['ഭ']='bh', ['മ']='m', 
	['യ']='y', ['ര']='r', ['ല']='l', ['വ']='v', 
	['ശ']='ś', ['ഷ']='ṣ', ['സ']='s', ['ഹ']='h',
	['ള']='ḷ', ['ഴ']='ḻ', ['റ']='ṟ', ['ഩ']='ṉ', ['ഺ']='ṯ',
}

local diacritics = {
    ['\224\181\129\224\181\141'] = CHANDRAKKALA,
	['\224\180\190'] = 'ā' ,
	['\224\180\191'] = 'i' ,
	['\224\181\128'] = 'ī' ,
	['\224\181\129'] = 'u' ,
	['\224\181\130'] = 'ū' ,
	['\224\181\131'] = 'r̥' ,
	['\224\181\132'] = 'r̥̄' ,
	['\224\181\134'] = 'e' ,
	['\224\181\135'] = 'ē' ,
	['\224\181\136'] = 'ai',
	['\224\181\138'] = 'o' ,
	['\224\181\139'] = 'ō' ,
	['\224\181\140'] = 'au', --archaic au
	['\224\181\151'] = 'au',
	['\224\181\162'] = 'l̥',
	['\224\181\163'] = 'l̥̄' ,
	--virama, supresses the inherent vowel "a"
	['\224\181\141'] = '',
	-- no diacritic
	[''] = 'a'
}

local nonconsonants = {
	-- vowels
	['അ']='a' , ['ആ']='ā' , ['ഇ']='i' , ['ഈ']='ī' , ['ഉ']='u' , ['ഊ']='ū' , 
	['ഋ']='r̥' , ['ൠ']='r̥̄' , ['ഌ']='l̥' , ['ൡ']='l̥̄', ['എ']='e' , ['ഏ']='ē' ,
	['ഐ']='ai' , ['ഒ']='o' , ['ഓ']='ō' , ['ഔ']='au' , ['ൟ']='ī' ,
	-- other symbols
	['ം']='ṁ', -- anusvara
	['ഃ']='ḥ' ,  -- visarga
	['ഽ']='’', -- praślēṣam
	-- chillus, consonants that never take vowels
	['ൺ']='ṇ' , ['ൻ']='ṉ' , ['ർ']='ṟ' , ['ൽ']='l' , ['ൾ']='ḷ' , ['ൿ']='k' , ['ൔ']='m', ['ൕ']='y', ['ൖ']='ḻ',
	['ൎ']='ṟ',
	-- digits
	['൦'] = '0', ['൧'] = '1', ['൨'] = '2', ['൩'] = '3', ['൪'] = '4',
	['൫'] = '5', ['൬'] = '6', ['൭'] = '7', ['൮'] = '8', ['൯']= '9',
	['൰']='10', ['൱']='100', ['൲']='1000', 
	['൳']='¼', ['൴']='½', ['൵']='¾', ['൞']='⅕', ['൷']='⅛', 
	['൜']='⅒', ['൶']='¹⁄₁₆', ['൸']='3⁄16', ['൛']='1⁄20',
	['൝']='3⁄20', ['൙']='1⁄40', ['൚']='3⁄80', ['൘']='1⁄160',

}

-- translit any words or phrases
function export.tr(text, lang, sc)
	-- final virama rule
	if lang == "ml" or lang == "tcy" then
		text = ugsub(text, VIRAMA .. "([,.!?:;]?)%f[%s%z]", VIRAMA .. "ŭ%1")
	end

	text = ugsub(
		text,
		'([കഖഗഘങചഛജഝഞടഠഡഢണതഥദധനപഫബഭമയരലവശഷസഹളഴറഩഺ])'..
		'(\224\181\129?[\224\180\190\224\180\191\224\181\128\224\181\129\224\181\130\224\181\131\224\181\132\224\181\162\224\181\163\224\181\134\224\181\135\224\181\138\224\181\139\224\181\140\224\181\136\224\181\151\224\181\141]?)',
		function(c, d)
			return consonants[c] .. (diacritics[d] or d)
		end)
	
	text = ugsub(text, '.', nonconsonants)
	
	-- anusvara
	text = ugsub(text, 'ṁ([kgṅ])', 'ṅ%1')
	text = ugsub(text, 'ṁ([cjñ])', 'ñ%1')
	text = ugsub(text, 'ṁ([ṭḍṇ])', 'ṇ%1')
	text = ugsub(text, 'ṁ([tdn])', 'n%1')
	text = ugsub(text, 'ṁ([pbm])', 'm%1')
	
	-- ŭ is elided before vowels
	text = ugsub(text, "ŭ([,.!?:;]?%s?[aāiīuūeēoō])", "%1")
	text = ugsub(text, "ŭ([,.!?:;]?%s?[lr]\204\165\204\132?)", "%1")
	
	-- but not with explicit chandrakkala
	-- also avoid ŭŭ if ŭ was already added by the final virama rule
	text = ugsub(text, CHANDRAKKALA .. "ŭ?", "ŭ")

	return text
end
 
return export