This module will transliterate Bashkir language text per WT:BA TR. 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:ba-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 tt = {
 ["ү"]="ü",  ['Ү']='Ü',  ["т"]="t",  ['Т']='T',  ["р"]="r",  ['Р']='R',  ["ф"]="f",  ['Ф']='F',  ["ө"]="ö", ['Ө']='Ö',
 ["ю"]="yu", ['Ю']='Yu', ["ш"]="ş",  ['Ш']='Ş',  ["ь"]="ʹ",  ['Ь']='ʹ',  ["ъ"]="ʺ",  ['Ъ']='ʺ',   ["н"]="n", ['Н']='N', 
 ["п"]="p",  ['П']='P',  ["й"]="y",  ['Й']='Y',  ["л"]="l",  ['Л']='L',  ["з"]="z",  ['З']='Z',  ["е"]="e", ['Е']='E', 
 ["г"]="g",  ['Г']='G',  ["б"]="b",  ['Б']='B',  ["у"]="u",  ['У']='U',  ["с"]="s",  ['С']='S',  ["х"]="x", ['Х']='X',
 ["ч"]="ç",  ['Ч']='Ç',  ["щ"]="şç", ['Щ']='Şç', ["я"]="ya", ['Я']='Ya', ["ы"]="ı",  ['Ы']='I',  ["э"]="e", ['Э']='E', 
 ["м"]="m",  ['М']='M',  ["о"]="o",  ['О']='O',  ["и"]="i",  ['И']='İ',  ["ё"]="yo", ['Ё']='Yo', ["ж"]="j", ['Ж']='j',
 ["к"]="k",  ['К']='K',  ["д"]="d",  ['Д']='D',  ["в"]="v",  ['В']='V',  ["ц"]="ts", ['Ц']='Ts', ["а"]="a", ['А']='A',
 ["ң"]="ñ",  ['Ң']='Ñ',  ["ғ"]="ğ",  ['Ғ']='Ğ',  ["ҙ"]="ź",  ['Ҙ']='Ź',  ["ҡ"]="q",  ['Ҡ']='Q',  ["ҫ"]="ś", ['Ҫ']='Ś',
 ["һ"]="h",  ['Һ']='H',  ["ә"]="ə",  ['Ә']='Ə'
};

local iotated = {
 ['е'] = 'ye',
 ['Е'] = 'Ye',
}

function export.tr(text, lang, sc)
 local str_gsub = string.gsub
 local ugsub = mw.ustring.gsub
 -- ү/у should be transliterated as w before uniotated vowels and after all vowels
 text = ugsub(text, "([АаЕеЭэЁёИиОоӨөУуҮүЫыӘәЮюЯя])[уү]", "%1w")
 text = ugsub(text, "[уү]([АаЭэИиОоӨөЫыӘә])", "w%1")
 
 text = ugsub(text,
   "([АаЕеЭэЁёИиОоӨөУуҮүЪъЫыЬьӘәЮюЯя%A][‌‌]?)([Ее])",
   function(a, e)
    return a .. iotated[e]
   end)
 text = ugsub(text,
  "^[Ее]",
  iotated)
 text = str_gsub(text, '[1-127194-244][128-191]*', tt)

 -- Tranliterate "и" at the end of the word as "iy"
 text = str_gsub(text, "(%a+)и$", "%1iy")

 -- Tranliterate "иә" as "iyə"
 text = str_gsub(text, "иә", "iyə")

 -- Transliterate "И" or "и" in the first syllable as "E" or "e"
 text = str_gsub(text, "^([А-Яа-я]+)([Ии])", function(first_syllable, letter)
    if string.upper(letter) == "И" then
      return first_syllable .. "E"
    else
      return first_syllable .. "e"
    end
  end)

 -- Transliterate "У" or "у" in the first syllable as "O" or "o"
 text = str_gsub(text, "^([А-Яа-я]+)([Уу])", function(first_syllable, letter)
    if string.upper(letter) == "У" then
      return first_syllable .. "O"
    else
      return first_syllable .. "o"
    end
  end)

 -- Transliterate "Ү" or "ү" in the first syllable as "Ö" or "ö"
 text = str_gsub(text, "^([А-Яа-я]+)([Үү])", function(first_syllable, letter)
    if string.upper(letter) == "Ү" then
      return first_syllable .. "Ö"
    else
      return first_syllable .. "ö"
    end
  end)

 -- Transliterate "Ю" or "ю" in the first syllable as "Yo" or "yo"
 text = str_gsub(text, "^([А-Яа-я]+)([Юю])", function(first_syllable, letter)
    if string.upper(letter) == "Ю" then
      return first_syllable .. "Yo"
    else
      return first_syllable .. "yo"
    end
  end)

 return text
end

return export