local export = {}

local tab = {
	["А"]="A", ["Б"]="B", ["В"]="V", ["Г"]="G", ["Д"]="D", ["Е"]="E", ["Ё"]="O", ["Ж"]="Ž", ["З"]="Z", ["И"]="I", ["І"]="I", ["Й"]="J",
	["К"]="K", ["Л"]="L", ["М"]="M", ["Н"]="N", ["О"]="O", ["Ӧ"]="Ö", ["П"]="P", ["Р"]="R", ["С"]="S", ["Т"]="T", ["У"]="U", ["Ф"]="F",
	["Х"]="X", ["Ц"]="C", ["Ч"]="Ć", ["Ш"]="Š", ["Щ"]="Šč", ["Ъ"]="", ["Ы"]="Y", ["Ь"]="", ["Э"]="E", ["Ю"]="U", ["Я"]="A",
	['а']='a', ['б']='b', ['в']='v', ['г']='g', ['д']='d', ['е']='e', ['ё']='o', ['ж']='ž', ['з']='z', ['и']='i', ['і']='i', ['й']='j',
	['к']='k', ['л']='l', ['м']='m', ['н']='n', ['о']='o', ['ӧ']='ö', ['п']='p', ['р']='r', ['с']='s', ['т']='t', ['у']='u', ['ф']='f',
	['х']='x', ['ц']='c', ['ч']='ć', ['ш']='š', ['щ']='šč', ['ъ']='', ['ы']='y', ['ь']='', ['э']='e', ['ю']='u', ['я']='a',	['Ө']='Å', 
	['ө']='å', ['Ӱ']='Ü', ['ӱ']='ü',
}

function export.tr(text, lang, sc)
	local language = lang
    -- Ё needs converting if is decomposed
    text = text:gsub("ё","ё"):gsub("Ё","Ё")
    
    -- тш
    text = mw.ustring.gsub(text, "Тш", "Č")
    text = mw.ustring.gsub(text, "тш", "č")
    
    -- soft consonants
    text = mw.ustring.gsub(text, "([Дд])з", "%1зQ")
    text = mw.ustring.gsub(text, "([ДЗЛНСТдзлнст])([еёияюь])", "%1Q%2")
    if lang ~= "kpv" then 
    	text = mw.ustring.gsub(text, "([БВГЖКМПРФХЦЧШЩбвгжкмпрфхцчшщ])([ёяюь])", "%1Q%2") 
    end

    -- soft vowels after a vowel or at the beginning of a word become j-
    text = mw.ustring.gsub(text, "([АОÖӨУӰЫЕЯЁЮИIЕЪЬаоöөуӱыэяёюиiеъь%A][\204\129\204\128]?)([еёюя])", "%1j%2")
    text = mw.ustring.gsub(text, "^Е", "Jе")
    text = mw.ustring.gsub(text, "^Ё", "Jё")
    text = mw.ustring.gsub(text, "^Ю", "Jю")
    text = mw.ustring.gsub(text, "^Я", "jя")
    text = mw.ustring.gsub(text, "^([еёюя])", "j%1")
    
    -- palatalisation
    text = mw.ustring.gsub(text, "ДQ", "Ď")
    text = mw.ustring.gsub(text, "дQ", "ď")
    text = mw.ustring.gsub(text, "ЗQ", "Ź")
    text = mw.ustring.gsub(text, "зQ", "ź")
    text = mw.ustring.gsub(text, "ЛQ", "Ľ")
    text = mw.ustring.gsub(text, "лQ", "ľ")
    text = mw.ustring.gsub(text, "НQ", "Ň")
    text = mw.ustring.gsub(text, "нQ", "ň")
    text = mw.ustring.gsub(text, "СQ", "Ś")
    text = mw.ustring.gsub(text, "сQ", "ś")
    text = mw.ustring.gsub(text, "ТQ", "Ť")
    text = mw.ustring.gsub(text, "тQ", "ť")
    text = mw.ustring.gsub(text, "Q", "ʹ")
    
    return (mw.ustring.gsub(text,'.',tab))
end

return export