Module:fa-translit

From Linguifex
Jump to navigation Jump to search

This module will transliterate Persian language text. 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:fa-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 m_str_utils = require("Module:string utilities")
local m_table = require("Module:table")
local concat = m_table.concat
local find = m_str_utils.find
local gsub = m_str_utils.gsub
local sub = m_str_utils.sub
local match = m_str_utils.match
local split = m_str_utils.split
local EOW = "([" .. "%s" .. "\n" .. "%p" .. "%x" .. "])"
local POW = "([" .. "%S" .. "]+)"
local hyphen = "<span class=\"Zsym mention\" style=\"font-size:100%;\">&nbsp;/ </span>"

local function basetr(text, lang, sc)
	return require("Module:fa-cls-translit").tr(text, lang, sc)
end

function s_split(text, delimiter)
    local can_split = find(text, delimiter) 
    if not can_split then
        return text, nil  
    end
    return sub(text, 1, can_split-1), sub(text, can_split+2)
end 

function export.CLS_tr(text)
	-- if // is present, ignore // and everything AFTER
	text = gsub(text, "%%", "")
	local cls, ira = s_split(text, "//")
	if cls then
	text = cls
	end
	text = basetr(text, lang, sc)
	if text == nil then
		return nil
		end
	return text
end

function export.IRA_tr(text)
	-- if // is present, ignore // and everything BEFORE
	local cls, ira = s_split(text, "//")
	if ira then
	text = ira
	end
	text = gsub(text, "%%", "#")
	text = basetr(text, lang, sc)
	if text == nil then
		return nil
	end
	text = gsub(text, "%.", "/1")
	text = gsub(text, "([^%a']+)" , "#%1#")
	text = require('Module:fa-IPA').romanize_ira(text)
	text = gsub(text, "#", "")
	text = gsub(text, "/1", ".")
	return text
end

function export.tr(text)
	cls_result = export.CLS_tr(text)
	ira_result = export.IRA_tr(text)
	if cls_result == nil or ira_result == nil then
		return nil
		else
		if cls_result == ira_result then
			return cls_result
		end
		combined = {cls_result .. hyphen .. ira_result}
		return concat(combined)
	end
end

-- I dont think anything uses this but Im too scared to remove it
function export.frame_xlit(text1, text2)
	text = text1 .. hyphen .. text2
	return text
	end

return export