Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Cyrilic <-> Latin transliterator for CC:T
- -- Made by timuzkas, donated thru CC0
- local Transliteration = {}
- Transliteration.__index = Transliteration
- local cyrillicAlphabet = {
- {"А", "а", "A", "a"}, {"Б", "б", "B", "b"}, {"В", "в", "V", "v"}, {"Г", "г", "G", "g"}, {"Д", "д", "D", "d"},
- {"Е", "е", "E", "e"}, {"Ё", "ё", "YO", "yo"}, {"Ж", "ж", "ZH", "zh"}, {"З", "з", "Z", "z"}, {"И", "и", "I", "i"},
- {"Й", "й", "Y", "y"}, {"К", "к", "K", "k"}, {"Л", "л", "L", "l"}, {"М", "м", "M", "m"}, {"Н", "н", "N", "n"},
- {"О", "о", "O", "o"}, {"П", "п", "P", "p"}, {"Р", "р", "R", "r"}, {"С", "с", "S", "s"}, {"Т", "т", "T", "t"},
- {"У", "у", "U", "u"}, {"Ф", "ф", "F", "f"}, {"Х", "х", "KH", "kh"}, {"Ц", "ц", "TS", "ts"}, {"Ч", "ч", "CH", "ch"},
- {"Ш", "ш", "SH", "sh"}, {"Щ", "щ", "SHCH", "shch"}, {"Ъ", "ъ", "", ""}, {"Ы", "ы", "Y", "y"}, {"Ь", "ь", "", ""},
- {"Э", "э", "E", "e"}, {"Ю", "ю", "YU", "yu"}, {"Я", "я", "YA", "ya"}
- }
- function Transliteration.new()
- local self = setmetatable({}, Transliteration)
- self.cyrillicToLatin = {}
- self.latinToCyrillic = {}
- self.isSetup = false
- return self
- end
- function Transliteration:setup()
- if self.isSetup then return end
- for _, pair in ipairs(cyrillicAlphabet) do
- self.cyrillicToLatin[utf8.codepoint(pair[1])] = pair[3]
- self.cyrillicToLatin[utf8.codepoint(pair[2])] = pair[4]
- if pair[3] ~= "" then
- if not self.latinToCyrillic[pair[3]] then
- self.latinToCyrillic[pair[3]] = {}
- end
- table.insert(self.latinToCyrillic[pair[3]], pair[1])
- table.insert(self.latinToCyrillic[pair[3]], pair[2])
- end
- if pair[4] ~= "" then
- if not self.latinToCyrillic[pair[4]] then
- self.latinToCyrillic[pair[4]] = {}
- end
- table.insert(self.latinToCyrillic[pair[4]], pair[1])
- table.insert(self.latinToCyrillic[pair[4]], pair[2])
- end
- end
- self.isSetup = true
- end
- function Transliteration:translate(str)
- if not self.isSetup then self:setup() end
- local result = ""
- local chars = {}
- for char in str:gmatch(utf8.charpattern) do
- table.insert(chars, char)
- end
- for _, char in ipairs(chars) do
- local codepoint = utf8.codepoint(char)
- local latin = self.cyrillicToLatin[codepoint]
- if latin then
- result = result .. latin
- else
- result = result .. char
- end
- end
- return result
- end
- function Transliteration:print(str)
- if not self.isSetup then self:setup() end
- print(self:translate(str))
- end
- function Transliteration:write(str, source)
- if not self.isSetup then self:setup() end
- if source and source.write then
- source:write(self:translate(str))
- else
- io.write(self:translate(str))
- end
- end
- function Transliteration:to_cyrilic(str)
- if not self.isSetup then self:setup() end
- local result = ""
- local i = 1
- while i <= #str do
- local found = false
- for latin, cyrillicChars in pairs(self.latinToCyrillic) do
- if str:sub(i, i + #latin - 1) == latin then
- result = result .. cyrillicChars[1]
- i = i + #latin
- found = true
- break
- end
- end
- if not found then
- result = result .. str:sub(i, i)
- i = i + 1
- end
- end
- return result
- end
- function Transliteration:overrideGlobal()
- if not self.isSetup then self:setup() end
- local originalPrint = _G.print
- _G.print = function(...)
- local args = {...}
- for i, arg in ipairs(args) do
- if type(arg) == "string" then
- args[i] = self:translate(arg)
- end
- end
- originalPrint(unpack(args))
- end
- if _G.term and _G.term.write then
- local originalTermWrite = _G.term.write
- _G.term.write = function(...)
- local args = {...}
- for i, arg in ipairs(args) do
- if type(arg) == "string" then
- args[i] = self:translate(arg)
- end
- end
- originalTermWrite(unpack(args))
- end
- end
- end
- return Transliteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement