Advertisement
TIMAS_Bro

Cyrilic transliterator

Mar 31st, 2025 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. -- Cyrilic <-> Latin transliterator for CC:T
  2. -- Made by timuzkas, donated thru CC0
  3.  
  4. local Transliteration = {}
  5. Transliteration.__index = Transliteration
  6.  
  7. local cyrillicAlphabet = {
  8. {"А", "а", "A", "a"}, {"Б", "б", "B", "b"}, {"В", "в", "V", "v"}, {"Г", "г", "G", "g"}, {"Д", "д", "D", "d"},
  9. {"Е", "е", "E", "e"}, {"Ё", "ё", "YO", "yo"}, {"Ж", "ж", "ZH", "zh"}, {"З", "з", "Z", "z"}, {"И", "и", "I", "i"},
  10. {"Й", "й", "Y", "y"}, {"К", "к", "K", "k"}, {"Л", "л", "L", "l"}, {"М", "м", "M", "m"}, {"Н", "н", "N", "n"},
  11. {"О", "о", "O", "o"}, {"П", "п", "P", "p"}, {"Р", "р", "R", "r"}, {"С", "с", "S", "s"}, {"Т", "т", "T", "t"},
  12. {"У", "у", "U", "u"}, {"Ф", "ф", "F", "f"}, {"Х", "х", "KH", "kh"}, {"Ц", "ц", "TS", "ts"}, {"Ч", "ч", "CH", "ch"},
  13. {"Ш", "ш", "SH", "sh"}, {"Щ", "щ", "SHCH", "shch"}, {"Ъ", "ъ", "", ""}, {"Ы", "ы", "Y", "y"}, {"Ь", "ь", "", ""},
  14. {"Э", "э", "E", "e"}, {"Ю", "ю", "YU", "yu"}, {"Я", "я", "YA", "ya"}
  15. }
  16.  
  17. function Transliteration.new()
  18. local self = setmetatable({}, Transliteration)
  19. self.cyrillicToLatin = {}
  20. self.latinToCyrillic = {}
  21. self.isSetup = false
  22. return self
  23. end
  24.  
  25. function Transliteration:setup()
  26. if self.isSetup then return end
  27. for _, pair in ipairs(cyrillicAlphabet) do
  28. self.cyrillicToLatin[utf8.codepoint(pair[1])] = pair[3]
  29. self.cyrillicToLatin[utf8.codepoint(pair[2])] = pair[4]
  30. if pair[3] ~= "" then
  31. if not self.latinToCyrillic[pair[3]] then
  32. self.latinToCyrillic[pair[3]] = {}
  33. end
  34. table.insert(self.latinToCyrillic[pair[3]], pair[1])
  35. table.insert(self.latinToCyrillic[pair[3]], pair[2])
  36. end
  37. if pair[4] ~= "" then
  38. if not self.latinToCyrillic[pair[4]] then
  39. self.latinToCyrillic[pair[4]] = {}
  40. end
  41. table.insert(self.latinToCyrillic[pair[4]], pair[1])
  42. table.insert(self.latinToCyrillic[pair[4]], pair[2])
  43. end
  44. end
  45. self.isSetup = true
  46. end
  47.  
  48. function Transliteration:translate(str)
  49. if not self.isSetup then self:setup() end
  50. local result = ""
  51.  
  52. local chars = {}
  53. for char in str:gmatch(utf8.charpattern) do
  54. table.insert(chars, char)
  55. end
  56.  
  57. for _, char in ipairs(chars) do
  58. local codepoint = utf8.codepoint(char)
  59. local latin = self.cyrillicToLatin[codepoint]
  60. if latin then
  61. result = result .. latin
  62. else
  63. result = result .. char
  64. end
  65. end
  66.  
  67. return result
  68. end
  69.  
  70. function Transliteration:print(str)
  71. if not self.isSetup then self:setup() end
  72. print(self:translate(str))
  73. end
  74.  
  75. function Transliteration:write(str, source)
  76. if not self.isSetup then self:setup() end
  77. if source and source.write then
  78. source:write(self:translate(str))
  79. else
  80. io.write(self:translate(str))
  81. end
  82. end
  83.  
  84. function Transliteration:to_cyrilic(str)
  85. if not self.isSetup then self:setup() end
  86. local result = ""
  87. local i = 1
  88. while i <= #str do
  89. local found = false
  90. for latin, cyrillicChars in pairs(self.latinToCyrillic) do
  91. if str:sub(i, i + #latin - 1) == latin then
  92. result = result .. cyrillicChars[1]
  93. i = i + #latin
  94. found = true
  95. break
  96. end
  97. end
  98. if not found then
  99. result = result .. str:sub(i, i)
  100. i = i + 1
  101. end
  102. end
  103. return result
  104. end
  105.  
  106. function Transliteration:overrideGlobal()
  107. if not self.isSetup then self:setup() end
  108.  
  109. local originalPrint = _G.print
  110. _G.print = function(...)
  111. local args = {...}
  112. for i, arg in ipairs(args) do
  113. if type(arg) == "string" then
  114. args[i] = self:translate(arg)
  115. end
  116. end
  117. originalPrint(unpack(args))
  118. end
  119.  
  120. if _G.term and _G.term.write then
  121. local originalTermWrite = _G.term.write
  122. _G.term.write = function(...)
  123. local args = {...}
  124. for i, arg in ipairs(args) do
  125. if type(arg) == "string" then
  126. args[i] = self:translate(arg)
  127. end
  128. end
  129. originalTermWrite(unpack(args))
  130. end
  131. end
  132. end
  133.  
  134. return Transliteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement