Module:txg-translit

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

This module will transliterate Tangut language text. It is also used to transliterate Classical Tibetan. 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:txg-translit/testcases.

Functions

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 LFW2008 = mw.loadData("Module:txg/data/LFW2008")

local meta = {
	[" "] = true,
	["-"] = true,
	["…"] = true
}

local meta_concat = {}
for char in pairs(meta) do
	table.insert(meta_concat, char)
end
meta_concat = require("Module:string utilities").pattern_escape(table.concat(meta_concat))

function export.tr(text, lang, sc)
	local UTF8_char = "[%z\1-\127\194-\244][\128-\191]*"
	local trtab = {}
	
	local i = 0
	for char in mw.ustring.gmatch(text, ".") do
		i = i + 1
		if meta[char] then
			table.insert(trtab, char)
		elseif char == "𖿠" and i > 1 then
			table.insert(trtab, trtab[i-1])
		else
			table.insert(trtab, LFW2008[char] or "?")
		end
	end
	text = table.concat(trtab, " ")
	
	text = text:gsub(" ?([" .. meta_concat .. "]) ?", "%1")
	
	local _, count = mw.ustring.gsub(text, "[" .. meta_concat .. "%?]", "")
	if count == mw.ustring.len(text) then
		return nil
	else
		return "*" .. text
	end
end

return export