This module will transliterate Hindi text per WT:HI TR through the function tr.


--[[
Transliteration for Hindi (possibly Sanskrit, Nepali, Marathi, any language using Devanagari script)
]]
local export = {}
 
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',
    ['क़']='q', ['ख़']='x', ['ग़']='ġ', ['ज़']='z', ['झ़']='ž', ['ड़']='ṛ', ['फ़']='f',  ['ढ़']='ṛh'
}
local diacritics = {
    ['ि']='i', ['ु']='u', ['ृ']='ŕ', ['े']='e', ['ो']='o', ['ा']='ā', ['ी']='ī', ['ू']='ū', ['ै']='ai', ['ौ']='au'
}
local tt = {
    -- vowels
    ['अ']='a', ['इ']='i', ['उ']='u', ['ऋ']='ŕ', ['ए']='e', ['ओ']='o', ['आ']='ā', ['ई']='ī', ['ऊ']='ū', ['ऐ']='ai', ['औ']='au', 
    -- chandrabindu    
    ['ँ']='m̐', --until a better method is found
    -- anusvara    
    ['ं']='ṁ', --until a better method is found
    -- visarga    
    ['ः']='ḥ',
    --numerals
    ['०']='0', ['१']='1', ['२']='2', ['३']='3', ['४']='4', ['५']='5', ['६']='6', ['७']='7', ['८']='8', ['९']='9',
    --punctuation        
    ['।']='.', --danda
    -- additional characters
    ['ळ']='ḷ',
}
 
-- translit any words or phrases
function export.tr(f)
    text = f.args[1]
 
    text = mw.ustring.gsub(text, '([कखगघङचछजझञटठडढणतथदधनपफबभमयरलवशषसह]़?)्‍?', consonants) -- conjucts (<consonant><virama>[<ZWJ>])
    text = mw.ustring.gsub(text, '([कखगघङचछजझञटठडढणतथदधनपफबभमयरलवशषसह]़?)([िुृेोाीूैौ]?)', function(c, d)
            if d == "" then
                return consonants[c] .. 'a'
            else
                return consonants[c] .. diacritics[d]
            end
        end)
    text = mw.ustring.gsub(text, '.', tt)
 
    return text
end
 
return export