Advertisement
MayDayZen

ChatTranslator

Sep 1st, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.99 KB | None | 0 0
  1. --[[
  2. Message Translator
  3. Made by Aim, updated by evn
  4. Credits to Riptxde for the sending chathook
  5. --]]
  6.  
  7. if not game['Loaded'] then game['Loaded']:Wait() end; repeat wait(.06) until game:GetService('Players').LocalPlayer ~= nil
  8. local YourLang = "es" -- Language code that the messages are going to be translated to
  9.  
  10. local googlev = isfile'googlev.txt' and readfile'googlev.txt' or ''
  11. local request = request or syn.request
  12.  
  13. local function outputHook(fnc)
  14. return function(...)
  15. return fnc('[INLINE TRANSLATOR]', ...)
  16. end
  17. end
  18.  
  19. local print,warn = outputHook(print), outputHook(warn)
  20.  
  21. -- // GOOGLE TRANSLATE // --
  22.  
  23. local translate, getISOCode do
  24. function googleConsent(Body) -- Because google really said: fy
  25. local args = {}
  26.  
  27. for match in Body:gmatch('<input type="hidden" name=".-" value=".-">') do
  28. local k,v = match:match('<input type="hidden" name="(.-)" value="(.-)">')
  29. args[k] = v
  30. end
  31. googlev = args.v
  32. writefile('googlev.txt', args.v)
  33. end
  34.  
  35. local function got(url, Method, Body) -- Basic version of https://www.npmjs.com/package/got using synapse's request API for google websites
  36. Method = Method or "GET"
  37.  
  38. local res = request({
  39. Url = url,
  40. Method = Method,
  41. Headers = {cookie="CONSENT=YES+"..googlev},
  42. Body = Body
  43. })
  44.  
  45. if res.Body:match('https://consent.google.com/s') then
  46. print('consent')
  47. googleConsent(res.Body)
  48. res = request({
  49. Url = url,
  50. Method = "GET",
  51. Headers = {cookie="CONSENT=YES+"..googlev}
  52. })
  53. end
  54.  
  55. return res
  56. end
  57.  
  58. local languages = {
  59. auto = "Automatic",
  60. af = "Afrikaans",
  61. sq = "Albanian",
  62. am = "Amharic",
  63. ar = "Arabic",
  64. hy = "Armenian",
  65. az = "Azerbaijani",
  66. eu = "Basque",
  67. be = "Belarusian",
  68. bn = "Bengali",
  69. bs = "Bosnian",
  70. bg = "Bulgarian",
  71. ca = "Catalan",
  72. ceb = "Cebuano",
  73. ny = "Chichewa",
  74. ['zh-cn'] = "Chinese Simplified",
  75. ['zh-tw'] = "Chinese Traditional",
  76. co = "Corsican",
  77. hr = "Croatian",
  78. cs = "Czech",
  79. da = "Danish",
  80. nl = "Dutch",
  81. en = "English",
  82. eo = "Esperanto",
  83. et = "Estonian",
  84. tl = "Filipino",
  85. fi = "Finnish",
  86. fr = "French",
  87. fy = "Frisian",
  88. gl = "Galician",
  89. ka = "Georgian",
  90. de = "German",
  91. el = "Greek",
  92. gu = "Gujarati",
  93. ht = "Haitian Creole",
  94. ha = "Hausa",
  95. haw = "Hawaiian",
  96. iw = "Hebrew",
  97. hi = "Hindi",
  98. hmn = "Hmong",
  99. hu = "Hungarian",
  100. is = "Icelandic",
  101. ig = "Igbo",
  102. id = "Indonesian",
  103. ga = "Irish",
  104. it = "Italian",
  105. ja = "Japanese",
  106. jw = "Javanese",
  107. kn = "Kannada",
  108. kk = "Kazakh",
  109. km = "Khmer",
  110. ko = "Korean",
  111. ku = "Kurdish (Kurmanji)",
  112. ky = "Kyrgyz",
  113. lo = "Lao",
  114. la = "Latin",
  115. lv = "Latvian",
  116. lt = "Lithuanian",
  117. lb = "Luxembourgish",
  118. mk = "Macedonian",
  119. mg = "Malagasy",
  120. ms = "Malay",
  121. ml = "Malayalam",
  122. mt = "Maltese",
  123. mi = "Maori",
  124. mr = "Marathi",
  125. mn = "Mongolian",
  126. my = "Myanmar (Burmese)",
  127. ne = "Nepali",
  128. no = "Norwegian",
  129. ps = "Pashto",
  130. fa = "Persian",
  131. pl = "Polish",
  132. pt = "Portuguese",
  133. pa = "Punjabi",
  134. ro = "Romanian",
  135. ru = "Russian",
  136. sm = "Samoan",
  137. gd = "Scots Gaelic",
  138. sr = "Serbian",
  139. st = "Sesotho",
  140. sn = "Shona",
  141. sd = "Sindhi",
  142. si = "Sinhala",
  143. sk = "Slovak",
  144. sl = "Slovenian",
  145. so = "Somali",
  146. es = "Spanish",
  147. su = "Sundanese",
  148. sw = "Swahili",
  149. sv = "Swedish",
  150. tg = "Tajik",
  151. ta = "Tamil",
  152. te = "Telugu",
  153. th = "Thai",
  154. tr = "Turkish",
  155. uk = "Ukrainian",
  156. ur = "Urdu",
  157. uz = "Uzbek",
  158. vi = "Vietnamese",
  159. cy = "Welsh",
  160. xh = "Xhosa",
  161. yi = "Yiddish",
  162. yo = "Yoruba",
  163. zu = "Zulu"
  164. };
  165.  
  166. function find(lang)
  167. for i,v in pairs(languages) do
  168. if i == lang or v == lang then
  169. return i
  170. end
  171. end
  172. end
  173.  
  174. function isSupported(lang)
  175. local key = find(lang)
  176. return key and true or false
  177. end
  178.  
  179. function getISOCode(lang)
  180. local key = find(lang)
  181. return key
  182. end
  183.  
  184. function stringifyQuery(dataFields)
  185. local data = ""
  186. for k, v in pairs(dataFields) do
  187. if type(v) == "table" then
  188. for _,v in pairs(v) do
  189. data = data .. ("&%s=%s"):format(
  190. game.HttpService:UrlEncode(k),
  191. game.HttpService:UrlEncode(v)
  192. )
  193. end
  194. else
  195. data = data .. ("&%s=%s"):format(
  196. game.HttpService:UrlEncode(k),
  197. game.HttpService:UrlEncode(v)
  198. )
  199. end
  200. end
  201. data = data:sub(2)
  202. return data
  203. end
  204.  
  205. local reqid = math.random(1000,9999)
  206. local rpcidsTranslate = "MkEWBc"
  207. local rootURL = "https://translate.google.com/"
  208. local executeURL = "https://translate.google.com/_/TranslateWebserverUi/data/batchexecute"
  209. local fsid, bl
  210.  
  211. do -- init
  212. print('initialize')
  213. local InitialReq = got(rootURL)
  214. fsid = InitialReq.Body:match('"FdrFJe":"(.-)"')
  215. bl = InitialReq.Body:match('"cfb2h":"(.-)"')
  216. end
  217.  
  218. local HttpService = game:GetService("HttpService")
  219. function jsonE(o)
  220. return HttpService:JSONEncode(o)
  221. end
  222. function jsonD(o)
  223. return HttpService:JSONDecode(o)
  224. end
  225.  
  226. function translate(str, to, from)
  227. reqid+=10000
  228. from = from and getISOCode(from) or 'auto'
  229. to = to and getISOCode(to) or 'en'
  230.  
  231. local data = {{str, from, to, true}, {nil}}
  232.  
  233. local freq = {
  234. {
  235. {
  236. rpcidsTranslate,
  237. jsonE(data),
  238. nil,
  239. "generic"
  240. }
  241. }
  242. }
  243.  
  244. local url = executeURL..'?'..stringifyQuery{rpcids = rpcidsTranslate, ['f.sid'] = fsid, bl = bl, hl="en", _reqid = reqid-10000, rt = 'c'}
  245. local body = stringifyQuery{['f.req'] = jsonE(freq)}
  246.  
  247. local req = got(url, "POST", body)
  248.  
  249. local body = jsonD(req.Body:match'%[.-%]\n')
  250. local translationData = jsonD(body[1][3])
  251. local result = {
  252. text = "",
  253. from = {
  254. language = "",
  255. text = ""
  256. },
  257. raw = ""
  258. }
  259. result.raw = translationData
  260. result.text = translationData[2][1][1][6][1][1]
  261.  
  262. result.from.language = translationData[3]
  263. result.from.text = translationData[2][5][1]
  264.  
  265. return result
  266. end
  267. end
  268.  
  269. local Players = game:GetService("Players")
  270. local LP = Players.LocalPlayer
  271. local StarterGui = game:GetService('StarterGui')
  272. for i=1, 15 do
  273. local r = pcall(StarterGui["SetCore"])
  274. if r then break end
  275. game:GetService('RunService').RenderStepped:wait()
  276. end
  277. wait()
  278.  
  279. local properties = {
  280. Color = Color3.new(1,1,0);
  281. Font = Enum.Font.SourceSansItalic;
  282. TextSize = 16;
  283. }
  284.  
  285. game:GetService("StarterGui"):SetCore("SendNotification",
  286. {
  287. Title = "Chat Translator",
  288. Text = "Ported to Google Translate",
  289. Duration = 3
  290. }
  291. )
  292.  
  293. properties.Text = "[TR] To send messages in a language, say > followed by the target language/language code, e.g.: >ru or >russian. To disable (go back to original language), say >d."
  294. StarterGui:SetCore("ChatMakeSystemMessage", properties)
  295.  
  296. function translateFrom(message)
  297. local translation = translate(message, YourLang)
  298.  
  299. local text
  300. if translation.from.language ~= YourLang then
  301. text = translation.text
  302. end
  303.  
  304. return {text, translation.from.language}
  305. end
  306.  
  307. -- // CHAT FUNCTIONS // --
  308.  
  309. local CBar, Connected = LP['PlayerGui']:WaitForChild('Chat')['Frame'].ChatBarParentFrame['Frame'].BoxFrame['Frame'].ChatBar, {}
  310. local EventFolder = game:GetService('ReplicatedStorage'):WaitForChild('DefaultChatSystemChatEvents')
  311. local Chat do
  312. function Chat(Original, msg, Channel)
  313. CBar.Text = msg
  314. for i,v in pairs(getconnections(CBar.FocusLost)) do
  315. v:Fire(true, nil, true)
  316. end
  317. end
  318.  
  319. --[[
  320. local ChatMain = LP.PlayerScripts:FindFirstChild('ChatMain', true)
  321. local MessageSender if ChatMain then
  322. MessageSender = require(ChatMain.MessageSender)
  323. ChatMain = require(ChatMain)
  324. end
  325.  
  326. function Chat(Original, msg, Channel)
  327. Channel = Channel or "All"
  328. if MessageSender and ChatMain then
  329. ChatMain.MessagePosted:fire(Original)
  330. MessageSender:SendMessage(msg, Channel)
  331. else
  332. if not _G.SecureChat then
  333. game:GetService('Players'):Chat(Original);
  334. end
  335. EventFolder.SayMessageRequest:FireServer(msg, Channel)
  336. end
  337. end]]
  338. end
  339.  
  340. do -- :Chatted replacement!!
  341. function get(plr, msg)
  342. print(msg)
  343. local tab = translateFrom(msg)
  344. local translation = tab[1]
  345. print(translation)
  346. if translation then
  347. properties.Text = "("..tab[2]:upper()..") ".."[".. plr .."]: "..translation
  348. StarterGui:SetCore("ChatMakeSystemMessage", properties)
  349. end
  350. end
  351.  
  352. EventFolder:WaitForChild("OnMessageDoneFiltering").OnClientEvent:Connect(function(data)
  353. if data == nil then return end
  354.  
  355. local plr = Players:FindFirstChild(data.FromSpeaker)
  356. local msg = tostring(data.Message)
  357. local originalchannel = tostring(data.OriginalChannel)
  358.  
  359. if plr then
  360. plr = plr.DisplayName
  361. else
  362. plr = tostring(data.FromSpeaker)
  363. end
  364.  
  365. if originalchannel:find("To ") then
  366. plr = plr..originalchannel
  367. end
  368.  
  369. get(plr, msg)
  370. end)
  371. end
  372.  
  373. local sendEnabled = false
  374. local target = ""
  375.  
  376. function translateTo(message, target)
  377. target = target:lower()
  378. local translation = translate(message, target, "auto")
  379.  
  380. return translation.text
  381. end
  382.  
  383. function disableSend()
  384. sendEnabled = false
  385. properties.Text = "[TR] Sending Disabled"
  386. StarterGui:SetCore("ChatMakeSystemMessage", properties)
  387. end
  388.  
  389.  
  390. local HookChat = function(Bar)
  391. coroutine.wrap(function()
  392. if not table.find(Connected,Bar) then
  393. local Connect = Bar['FocusLost']:Connect(function(Enter, _, ignore)
  394. if ignore then return end
  395. if Enter ~= false and Bar.Text ~= '' then
  396. local Message = Bar.Text
  397. Bar.Text = ''
  398. if Message == ">d" then
  399. disableSend()
  400. elseif Message:sub(1,1) == ">" and not Message:find(" ") then
  401. if getISOCode(Message:sub(2)) then
  402. sendEnabled = true
  403. target = Message:sub(2)
  404. else
  405. properties.Text = "[TR] Invalid language"
  406. StarterGui:SetCore("ChatMakeSystemMessage", properties)
  407. end
  408. elseif sendEnabled and not (Message:sub(1,3) == "/e " or Message:sub(1,7) == "/emote ") then
  409. local og = Message
  410. Message = translateTo(Message, target)
  411. --Bar.Text = Message
  412. Chat(og, Message)
  413. else
  414. --Bar.Text = Message
  415. Chat(Message, Message)
  416. end
  417. end
  418. end)
  419. Connected[#Connected+1] = Bar; Bar['AncestryChanged']:Wait(); Connect:Disconnect()
  420. end
  421. end)()
  422. end
  423.  
  424. HookChat(CBar); local BindHook = Instance.new('BindableEvent')
  425.  
  426. local MT = getrawmetatable(game); local NC = MT.__namecall; setreadonly(MT, false)
  427.  
  428. MT.__namecall = newcclosure(function(...)
  429. local Method, Args = getnamecallmethod(), {...}
  430. if rawequal(tostring(Args[1]),'ChatBarFocusChanged') and rawequal(Args[2],true) then
  431. if LP['PlayerGui']:FindFirstChild('Chat') then
  432. BindHook:Fire()
  433. end
  434. end
  435. return NC(...)
  436. end)
  437.  
  438. BindHook['Event']:Connect(function()
  439. CBar = LP['PlayerGui'].Chat['Frame'].ChatBarParentFrame['Frame'].BoxFrame['Frame'].ChatBar
  440. HookChat(CBar)
  441. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement