Translator Failed
Key RequiredUniversal

Translator Failed

Universal Scriptlumpiasalladlumpiasallad 2.5K views 19 executesUpdated May 24, 2025
Unrated
00
Lua Script•258 lines (10853 chars)
--client side
local DEBUG = true
local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")
local StarterGui = game:GetService("StarterGui")

local LocalPlayer = Players.LocalPlayer
local currentTargetLanguage = "en"  -- Default lang:English

-- Lang table: 
local languages = {
    af = "Afrikaans", sq = "Albanian", am = "Amharic", ar = "Arabic", hy = "Armenian",
    az = "Azerbaijani", eu = "Basque", be = "Belarusian", bn = "Bengali", bs = "Bosnian",
    bg = "Bulgarian", ca = "Catalan", ceb = "Cebuano", ny = "Chichewa", ['zh-cn'] = "Chinese Simplified",
    ['zh-tw'] = "Chinese Traditional", co = "Corsican", hr = "Croatian", cs = "Czech", da = "Danish",
    nl = "Dutch", en = "English", eo = "Esperanto", et = "Estonian", tl = "Filipino", fi = "Finnish",
    fr = "French", fy = "Frisian", gl = "Galician", ka = "Georgian", de = "German", el = "Greek",
    gu = "Gujarati", ht = "Haitian Creole", ha = "Hausa", haw = "Hawaiian", iw = "Hebrew", hi = "Hindi",
    hmn = "Hmong", hu = "Hungarian", is = "Icelandic", ig = "Igbo", id = "Indonesian", ga = "Irish",
    it = "Italian", ja = "Japanese", jw = "Javanese", kn = "Kannada", kk = "Kazakh", km = "Khmer",
    ko = "Korean", ku = "Kurdish (Kurmanji)", ky = "Kyrgyz", lo = "Lao", la = "Latin", lv = "Latvian",
    lt = "Lithuanian", lb = "Luxembourgish", mk = "Macedonian", mg = "Malagasy", ms = "Malay",
    ml = "Malayalam", mt = "Maltese", mi = "Maori", mr = "Marathi", mn = "Mongolian", my = "Myanmar (Burmese)",
    ne = "Nepali", no = "Norwegian", ps = "Pashto", fa = "Persian", pl = "Polish", pt = "Portuguese",
    pa = "Punjabi", ro = "Romanian", ru = "Russian", sm = "Samoan", gd = "Scots Gaelic", sr = "Serbian",
    st = "Sesotho", sn = "Shona", sd = "Sindhi", si = "Sinhala", sk = "Slovak", sl = "Slovenian", so = "Somali",
    es = "Spanish", su = "Sundanese", sw = "Swahili", sv = "Swedish", tg = "Tajik", ta = "Tamil", te = "Telugu",
    th = "Thai", tr = "Turkish", uk = "Ukrainian", ur = "Urdu", uz = "Uzbek", vi = "Vietnamese", cy = "Welsh",
    xh = "Xhosa", yi = "Yiddish", yo = "Yoruba", zu = "Zulu", auto = "Automatic"
}

-- ALIASES table: maps alternative shorthand to the proper language code.
local aliases = {
    jp = "ja"  -- when the user types "-jp", we treat it as "ja" (Japanese)
    -- You can add more aliases if needed.
}

-- CUSTOM UI: Create or get a ScreenGui to display our system messages.
local function ensureTranslatorUI()
    local playerGui = LocalPlayer:WaitForChild("PlayerGui")
    local screenGui = playerGui:FindFirstChild("TranslatorUI")
    if not screenGui then
        screenGui = Instance.new("ScreenGui")
        screenGui.Name = "TranslatorUI"
        screenGui.Parent = playerGui

        -- Create a Frame 
        local frame = Instance.new("Frame")
        frame.Name = "MessageFrame"
        frame.BackgroundTransparency = 0.5
        frame.BackgroundColor3 = Color3.new(0, 0, 0)
        frame.Size = UDim2.new(0.3, 0, 0.2, 0)
        frame.Position = UDim2.new(0.35, 0, 0.7, 0)
        frame.Parent = screenGui

        -- UIListLayout for vertical alignment of messages
        local layout = Instance.new("UIListLayout")
        layout.FillDirection = Enum.FillDirection.Vertical
        layout.SortOrder = Enum.SortOrder.LayoutOrder
        layout.Parent = frame
    end
    return screenGui:FindFirstChild("MessageFrame")
end

-- custom UI
local function sendSystemMessage(text)
    if DEBUG then
        print("[DEBUG] sendSystemMessage:", text)
    end
    local frame = ensureTranslatorUI()
    local label = Instance.new("TextLabel")
    label.Text = text
    label.TextColor3 = Color3.new(1, 1, 1)
    label.BackgroundTransparency = 1
    label.TextScaled = true
    label.Size = UDim2.new(1, 0, 0, 30)
    label.Parent = frame
    delay(6, function() if label then label:Destroy() end end)
end

-- Display an initial message when the script loads.
sendSystemMessage("Chat Translator Active.\nType '-help' for instructions.\nCurrent target language: " .. languages[currentTargetLanguage])


--i wanted to use LibreTranslate API key since i thought it was free but the only free thing is the api which i have to run localy in my cmd and I don't want to get everyone's IP to make this work :<.
local LIBRE_TRANSLATE_URL = "https://libretranslate.com/translate"
local function translateText(text, callback)
    if DEBUG then
        print("[DEBUG] translateText called with text:", text)
    end
    local data = {
        q = text,
        source = "auto",
        target = currentTargetLanguage,
        format = "text",
        alternatives = 3,
        api_key = ""
    }
    local jsonData = HttpService:JSONEncode(data)
    if DEBUG then
        print("[DEBUG] JSON Data:", jsonData)
    end
    spawn(function()
        local success, response = pcall(function()
            return HttpService:PostAsync(LIBRE_TRANSLATE_URL, jsonData, Enum.HttpContentType.ApplicationJson)
        end)
        if DEBUG then
            print("[DEBUG] HTTP request success:", success)
        end
        if success then
            if DEBUG then
                print("[DEBUG] HTTP Response:", response)
            end
            local decoded
            local decodeSuccess = pcall(function()
                decoded = HttpService:JSONDecode(response)
            end)
            if not decodeSuccess then
                if DEBUG then
                    warn("[DEBUG] JSON decode failed")
                end
                callback("[Translation Error: JSON decode failed]")
                return
            end
            if decoded and decoded.translatedText then
                if DEBUG then
                    print("[DEBUG] Translated Text:", decoded.translatedText)
                end
                callback(decoded.translatedText)
            else
                if DEBUG then
                    warn("[DEBUG] No translatedText in response. Full response:", response)
                end
                callback("[Translation Error: No text returned]")
            end
        else
            local errorMessage = tostring(response)
            if DEBUG then
                warn("[DEBUG] HTTP Request failed:", errorMessage)
            end
            callback("[Translation API Error: " .. errorMessage .. "]")
        end
    end)
end


-- CHAT COMMAND PROCESS
local function onPlayerChatted(player, message)
    if DEBUG then
        print("[DEBUG] onPlayerChatted - Player:", player.DisplayName, "Message:", message)
    end

    -- Process commands only from the LocalPlayer.
    if player == LocalPlayer then
        local lowerMessage = message:lower()

        -- Help command
        if lowerMessage == "-help" then
            local helpMsg = "Chat Translator Instructions:\n" ..
                            "• This tool automatically translates chat messages from other players using LibreTranslate.\n" ..
                            "• Current target language: " .. languages[currentTargetLanguage] .. "\n" ..
                            "• To change the target language, use one of the following commands:\n" ..
                            "     -language <code> (e.g., -language ja)\n" ..
                            "     Or use shorthand commands (e.g., -jp for Japanese, -af for Afrikaans).\n" ..
                            "• Available language codes include: en, af, ja (or jp), fr, de, etc.\n" ..
                            "Enjoy seamless communication!"
            sendSystemMessage(helpMsg)
            if DEBUG then
                print("[DEBUG] Help command processed.")
            end
            return
        end

        -- Shorthand command processing: Match messages like "-en", "-af", "-jp", etc.
        local commandCode = message:match("^%-(%S+)$")
        if commandCode then
            commandCode = commandCode:lower()
            -- Check aliases first; for instance, map 'jp' to 'ja'
            if aliases[commandCode] then
                commandCode = aliases[commandCode]
            end
            if languages[commandCode] then
                currentTargetLanguage = commandCode
                sendSystemMessage("Target language updated to: " .. languages[currentTargetLanguage])
                if DEBUG then
                    print("[DEBUG] Shorthand language command updated to:", currentTargetLanguage)
                end
                return
            end
        end

        -- Full command: "-language <code>"
        if lowerMessage:sub(1, 9) == "-language" then
            local langCode = message:match("^%-language%s+(%S+)$")
            if langCode then
                langCode = langCode:lower()
                if languages[langCode] then
                    currentTargetLanguage = langCode
                    sendSystemMessage("Target language updated to: " .. languages[currentTargetLanguage])
                    if DEBUG then
                        print("[DEBUG] Language updated to:", currentTargetLanguage)
                    end
                else
                    sendSystemMessage("Invalid language code: " .. langCode)
                    if DEBUG then
                        print("[DEBUG] Invalid language code entered:", langCode)
                    end
                end
            else
                sendSystemMessage("Usage: -language <code> (e.g., -language ja)")
                if DEBUG then
                    print("[DEBUG] Language command syntax error.")
                end
            end
            return  -- Stop further processing for local player's commands.
        end

        -- Do not process further if it's a local player's non-command message.
        return
    end

    -- For messages from other players, automatically translate them.
    translateText(message, function(translated)
        local displayText = "[Translated] " .. player.DisplayName .. ": " .. translated
        sendSystemMessage(displayText)
        if DEBUG then
            print("[DEBUG] Final displayed translated message:", displayText)
        end
    end)
end

-- CONNECT CHAT EVENTS
for _, player in ipairs(Players:GetPlayers()) do
    if player ~= LocalPlayer then
        player.Chatted:Connect(function(message)
            onPlayerChatted(player, message)
        end)
        if DEBUG then
            print("[DEBUG] Connected Chatted event for player:", player.DisplayName)
        end
    end
end

Players.PlayerAdded:Connect(function(player)
    if player ~= LocalPlayer then
        player.Chatted:Connect(function(message)
            onPlayerChatted(player, message)
        end)
        if DEBUG then
            print("[DEBUG] PlayerAdded - Connected new player:", player.DisplayName)
        end
    end
end)

if DEBUG then
    print("[DEBUG] Chat Translator loaded successfully for", LocalPlayer.DisplayName)
end
-- Made by: Lumpia :<

Features & Information

I need a api key to make this work but I don't have any money to make it work so I'm giving this to be an open source for those who wants to make a translation script :<
#translator

Community Comments (1)

lumpiasalladlumpiasalladMay 24, 2025

props to https://i.qts.life/r/ChatInlineTranslator.lua. Which not working anymore; I wanted to fix it but failed. devs, please make this work Roblox inbuilt translator is trash.

More Scripts for Universal Scripts