Module:Deva-Knda-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-Knda-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 char = {
	["क"] = "ಕ", ["ख"] = "ಖ", ["ग"] = "ಗ", ["घ"] = "ಘ", ["ङ"] = "ಙ", ["च"] = "ಚ", ["छ"] = "ಛ", ["ज"] = "ಜ", ["झ"] = "ಝ", ["ञ"] = "ಞ", ["ट"] = "ಟ", ["ठ"] = "ಠ", ["ड"] = "ಡ", ["ढ"] = "ಢ", ["ण"] = "ಣ", ["त"] = "ತ", ["थ"] = "ಥ", ["द"] = "ದ", ["ध"] = "ಧ", ["न"] = "ನ", ["प"] = "ಪ", ["फ"] = "ಫ", ["ब"] = "ಬ", ["भ"] = "ಭ", ["म"] = "ಮ", ["य"] = "ಯ", ["र"] = "ರ", ["ल"] = "ಲ", ["ळ"] = "ಳ", ["व"] = "ವ", ["श"] = "ಶ", ["ष"] = "ಷ", ["स"] = "ಸ", ["ह"] = "ಹ",
	["अ"] = "ಅ", ["आ"] = "ಆ", ["इ"] = "ಇ", ["ई"] = "ಈ", ["उ"] = "ಉ", ["ऊ"] = "ಊ", ["ऋ"] = "ಋ", ["ॠ"] = "ೠ", ["ऌ"] = "ಌ", ["ॡ"] = "ೡ", ["ऎ"] = "ಎ", ["ए"] = "ಏ", ["ऐ"] = "ಐ", ["ऒ"] = "ಒ", ["ओ"] = "ಓ", ["औ"] = "ಔ",
	["ा"] = "ಾ", ["ि"] = "ಿ", ["ी"] = "ೀ", ["ु"] = "ು", ["ू"] = "ೂ", ["ृ"] = "ೃ", ["ॄ"] = "ೄ", ["ॢ"] = "ೢ", ["ॣ"] = "ೣ", ["ॆ"] = "ೆ", ["े"] = "ೇ", ["ै"] = "ೈ", ["ॊ"] = "ೊ", ["ो"] = "ೋ", ["ौ"] = "ೌ", ["्"] = "್",
	["ं"] = "ಂ", ["ः"] = "ಃ", ["ँ"] = "ಁ", ["़"] = "಼", ["ᳵ"] = "ೱ", ["ᳶ"] = "ೲ", ["ऽ"] = "ಽ", ["꣼"] = "಄", ["ॐ"] = "ಓಂ",
	["०"] = "೦", ["१"] = "೧", ["२"] = "೨", ["३"] = "೩", ["४"] = "೪", ["५"] = "೫", ["६"] = "೬", ["७"] = "೭", ["८"] = "೮", ["९"] = "೯"
}

-- Override returns text even if some characters cannot be transliterated.
function export.tr(text, lang, sc, override, nakaaraPollu, archaicLlla, archaicRra)
	local UTF8_char = "[%z\1-\127\194-\244][\128-\191]*"
	local Knda = require("Module:scripts").getByCode("Knda")
	text = mw.ustring.toNFD(text)
	
	text = string.gsub(text, UTF8_char, char)
	
	-- Nakaara Pollu is used in Sanskrit.
	if nakaaraPollu or lang == "sa" then
		text = mw.ustring.gsub(text, "ನ್([^ಕ-ಹೝೞ])", "ೝ%1")
		text = mw.ustring.gsub(text, "ನ್$", "ೝ")
	end
	
	if archaicLlla then
		text = mw.ustring.gsub(text, "ಳ಼", "ೞ")
	end
	
	if archaicRra then
		text = mw.ustring.gsub(text, "ರ಼", "ಱ")
	end
	
	text = mw.ustring.toNFC(text)
	local reducedText = mw.ustring.gsub(mw.ustring.gsub(text, "<.->", ""), "[%s%p\n]+", "")
	if (mw.ustring.len(reducedText) == Knda:countCharacters(reducedText) and not mw.ustring.find(text, "಼಼")) or override then
		return text
	else
		return nil
	end
end

return export