Module:Deva-Bhks-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-Bhks-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)
	local UTF8_char = "[%z\1-\127\194-\244][\128-\191]*"
	local Bhks = require("Module:scripts").getByCode("Bhks")
	
	text = string.gsub(text, UTF8_char, char)
	text = mw.ustring.gsub(text, "([𑰀-𑱀𑱐-𑱬]) ([𑰀-𑱀𑱐-𑱬])", "%1𑱃%2")
	
	local reducedText = mw.ustring.gsub(mw.ustring.gsub(text, "<.->", ""), "[%s%p\n]+", "")
	if mw.ustring.len(reducedText) == Bhks:countCharacters(reducedText) or override then
		return text
	else
		return nil
	end
end

return export