Module:Deva-Mlym-translit

From Wiktionary, the free dictionary
Jump to navigation Jump to search

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-Mlym-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 twoChars = {
	["त़"] = "ഺ", ["ऩ"] = "ഩ", ["ऱ"] = "റ", ["ऴ"] = "ഴ"
}

local oneChar = {
	["क"] = "ക", ["ख"] = "ഖ", ["ग"] = "ഗ", ["घ"] = "ഘ", ["ङ"] = "ങ", ["च"] = "ച", ["छ"] = "ഛ", ["ज"] = "ജ", ["झ"] = "ഝ", ["ञ"] = "ഞ", ["ट"] = "ട", ["ठ"] = "ഠ", ["ड"] = "ഡ", ["ढ"] = "ഢ", ["ण"] = "ണ", ["त"] = "ത", ["थ"] = "ഥ", ["द"] = "ദ", ["ध"] = "ധ", ["न"] = "ന", ["प"] = "പ", ["फ"] = "ഫ", ["ब"] = "ബ", ["भ"] = "ഭ", ["म"] = "മ", ["य"] = "യ", ["र"] = "ര", ["ल"] = "ല", ["ळ"] = "ള", ["व"] = "വ", ["श"] = "ശ", ["ष"] = "ഷ", ["स"] = "സ", ["ह"] = "ഹ",
	["अ"] = "അ", ["आ"] = "ആ", ["इ"] = "ഇ", ["ई"] = "ഈ", ["उ"] = "ഉ", ["ऊ"] = "ഊ", ["ऋ"] = "ഋ", ["ॠ"] = "ൠ", ["ऌ"] = "ഌ", ["ॡ"] = "ൡ", ["ऎ"] = "എ", ["ए"] = "ഏ", ["ऐ"] = "ഐ", ["ऒ"] = "ഒ", ["ओ"] = "ഓ", ["औ"] = "ഔ",
	["ा"] = "ാ", ["ि"] = "ി", ["ी"] = "ീ", ["ु"] = "ു", ["ू"] = "ൂ", ["ृ"] = "ൃ", ["ॄ"] = "ൄ", ["ॢ"] = "ൢ", ["ॣ"] = "ൣ", ["ॆ"] = "െ", ["े"] = "േ", ["ै"] = "ൈ", ["ॊ"] = "ൊ", ["ो"] = "ോ", ["ौ"] = "ൗ", ["्"] = "്",
	["ं"] = "ം", ["ः"] = "ഃ", ["ँ"] = "ഁ", ["ऽ"] = "ഽ", ["ॐ"] = "ഓം",
	["०"] = "൦", ["१"] = "൧", ["२"] = "൨", ["३"] = "൩", ["४"] = "൪", ["५"] = "൫", ["६"] = "൬", ["७"] = "൭", ["८"] = "൮", ["९"] = "൯"
}

-- Override returns text even if some characters cannot be transliterated.
function export.tr(text, lang, sc, override)
	local UTF8_char = "[%z\1-\127\194-\244][\128-\191]*"
	local Mlym = require("Module:scripts").getByCode("Mlym")
	text = mw.ustring.toNFD(text)
	
	for digraph, replacement in pairs(twoChars) do
		text = string.gsub(text, digraph, replacement)
	end
	
	text = string.gsub(text, UTF8_char, oneChar)
	
	text = mw.ustring.toNFC(text)
	local reducedText = mw.ustring.gsub(mw.ustring.gsub(text, "<.->", ""), "[%s%p\n]+", "")
	if mw.ustring.len(reducedText) == Mlym:countCharacters(reducedText) or override then
		return text
	else
		return nil
	end
end

return export