Advertisement
rrixh

xhat translator

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