Module:Deva-Tibt-translit

This module will transliterate text in the Devanagari script. 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-Tibt-translit/testcases.

Functions edit

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 U = mw.ustring.char
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local sub = mw.ustring.sub

local conv = {
	['क']='ཀ', ['ख']='ཁ', ['ग']='ག', ['घ']='གྷ', ['ङ']='ང', 
	['च']='ཙ', ['छ']='ཚ', ['ज']='ཛ', ['झ']='ཛྷ', ['ञ']='ཉ', 
	['ट']='ཊ', ['ठ']='ཋ', ['ड']='ཌ', ['ढ']='ཌྷ', ['ण']='ཎ', 
	['त']='ཏ', ['थ']='ཐ', ['द']='ད', ['ध']='དྷ', ['न']='ན', 
	['प']='པ', ['फ']='ཕ', ['ब']='བ', ['भ']='བྷ', ['म']='མ',
	['य']='ཡ', ['र']='ར', ['ल']='ལ', ['व']='ཝ', ['श़']='ཞ', 
	['श']='ཤ', ['ष']='ཥ', ['स']='ས', ['ह']='ཧ', ['स़']='ཟ',

	['ा']='ཱ', ['ि']='ི', ['ी']='ཱི', ['ु']='ུ', ['ू']='ཱུ', ['े']='ེ',  ['ै']='ཻ', ['ो']='ོ', ['ौ']='ཽ', ['्']='྄', ['ृ']='ྲྀ', ['ॄ']='ྲཱྀ', ['ॢ']='ླྀ', ['ॣ']='ླཱྀ', 

	-- vowels
	['अ']='ཨ', ['आ']='ཨཱ', ['इ']='ཨི', ['ई']='ཨཱི', ['उ']='ཨུ', ['ऊ']='ཨཱུ', ['ए']='ཨེ', ['ऐ']='ཨཻ', ['ओ']='ཨོ', ['औ']='ཨཽ', 
['़']='', ['ऋ']='རྀ', ['ॠ']='རཱྀ', ['ऌ']='ལྀ', ['ॡ']='ལཱྀ', 
	-- chandrabindu    
	['ँ']='ྃ',
	-- anusvara    
	['ं']='ཾ',
	-- visarga    
	['ः']='ཿ',
   -- avagraha
    ['ऽ']='྅',
  -- om
    ['ॐ']='ༀ',
	--punctuation        
    ['॥']='༎',
	['।']='།',
['०']='༠', ['१']='༡', ['२']='༢', ['३']='༣', ['४']='༤', ['५']='༥', ['६']='༦', ['७']='༧', ['८']='༨', ['९']='༩',
[' ']='་', 
}

function export.tr(text, lang, sc)
	text = mw.ustring.gsub(
		text,
		".",
		function(c)
			return conv[c]
		end)
        text = gsub(text, 'ས़', "ཟ")
        text = gsub(text, 'ཤ़', "ཞ")
        text = mw.ustring.gsub(text, '྄ཡ', "ྱ")
        text = mw.ustring.gsub(text, '྄ར', "ྲ")
        text = mw.ustring.gsub(text, '྄ཝ', "ྭ")
        text = mw.ustring.gsub(text, '྄ཀ', "ྐ")
        text = mw.ustring.gsub(text, '྄ཁ', "ྑ")
        text = mw.ustring.gsub(text, '྄ག', "ྒ")
        text = mw.ustring.gsub(text, '྄ང', "ྔ")
        text = mw.ustring.gsub(text, '྄ཅ', "ྕ")
        text = mw.ustring.gsub(text, '྄ཆ', "ྖ")
        text = mw.ustring.gsub(text, '྄ཇ', "ྗ")
        text = mw.ustring.gsub(text, '྄ཉ', "ྙ")
        text = mw.ustring.gsub(text, '྄ཙ', "ྩ")
        text = mw.ustring.gsub(text, '྄ཚ', "ྪ")
        text = mw.ustring.gsub(text, '྄ཛ', "ྫ")
        text = mw.ustring.gsub(text, '྄ན', "ྣ")
        text = mw.ustring.gsub(text, '྄ཏ', "ྟ")
        text = mw.ustring.gsub(text, '྄ཐ', "ྠ")
        text = mw.ustring.gsub(text, '྄ད', "ྡ")
        text = mw.ustring.gsub(text, '྄པ', "ྤ")
        text = mw.ustring.gsub(text, '྄ཕ', "ྥ")
        text = mw.ustring.gsub(text, '྄བ', "ྦ")
        text = mw.ustring.gsub(text, '྄མ', "ྨ")
        text = mw.ustring.gsub(text, '྄ཞ', "ྮ")
        text = mw.ustring.gsub(text, '྄ཟ', "ྯ")
        text = mw.ustring.gsub(text, '྄ལ', "ླ")
        text = mw.ustring.gsub(text, '྄ཤ', "ྴ")
        text = mw.ustring.gsub(text, '྄ས', "ྶ")
        text = mw.ustring.gsub(text, '྄ཧ', "ྷ")
        text = mw.ustring.gsub(text, '྄ཥ', "ྵ")
        text = mw.ustring.gsub(text, '྄ཊ', "ྚ")
        text = mw.ustring.gsub(text, '྄ཋ', "ྛ")
        text = mw.ustring.gsub(text, '྄ཌ', "ྜ")
        text = mw.ustring.gsub(text, '྄ཎ', "ྞ")
        text = mw.ustring.gsub(text, 'ཙྱ', "ཅ")
        text = mw.ustring.gsub(text, 'ཚྱ', "ཆ")
        text = mw.ustring.gsub(text, 'ཛྱ', "ཇ")
        text = mw.ustring.gsub(text, 'ཱི', "ི")
        text = mw.ustring.gsub(text, 'ཱུ', "ུ")
	return text
end
 
return export