Translating string

I’m working on a game that fetches a random word from an API dictionary and displays it on the player’s text UI. I need help finding an API that translates the word into Portuguese so I can check if the player correctly translates the English word into Portuguese.

Current Module :

local module = {}

local HttpService = game:GetService("HttpService")

function module.getRandomWord()
	local url = "https://random-word-api.herokuapp.com/word"
	local success, response = pcall(function()
		return HttpService:GetAsync(url)
	end)

	if success then
		local words = HttpService:JSONDecode(response)
		local randomWord = words[1]
		return randomWord
	else
		warn("Failed to fetch random word: " .. tostring(response))
		return nil
	end
end


return module
1 Like

There’s a ton of Translating APIs on google, even google has its own one, maybe try using theirs.

local HttpService = game:GetService("HttpService")

local apiKey = "APIKEY" 
local endpoint = "https://translation.googleapis.com/language/translate/v2"

local function translateText(text, targetLanguage)
	local url = endpoint .. "?key=" .. apiKey

	local body = HttpService:JSONEncode({
		q = text,
		target = targetLanguage,
	})

	local headers = {
		["Content-Type"] = "application/json"
	}

	local response = nil
	local success, err = pcall(function()
		response = HttpService:PostAsync(url, body, Enum.HttpContentType.ApplicationJson, false, headers)
	end)

	if success then
		local responseData = HttpService:JSONDecode(response)
		if responseData.data and responseData.data.translations then
			return responseData.data.translations[1].translatedText
		else
			return "Error: Translation not available"
		end
	else
		return "Error: " .. err
	end
end

local originalText = "Hello, how are you?"
local targetLanguage = "es"
local translatedText = translateText(originalText, targetLanguage)

print("Original Text: " .. originalText)
print("Translated Text: " .. translatedText)

Error: Translated Text: Error: Header “Content-Type” is not allowed!

It’s the script i tried with google api a while ago

not sure if this works, but test it out.

local HttpService = game:GetService("HttpService")
local apiKey = "APIKEY"
local endpoint = "https://translation.googleapis.com/language/translate/v2"

local function translateText(text, targetLanguage)
    local url = string.format(
        "%s?key=%s&q=%s&target=%s",
        endpoint,
        apiKey,
        HttpService:UrlEncode(text),
        targetLanguage
    )
    
    local success, response = pcall(function()
        return HttpService:GetAsync(url)
    end)
    
    if success then
        local responseData = HttpService:JSONDecode(response)
        if responseData.data and responseData.data.translations then
            return responseData.data.translations[1].translatedText
        else
            return "Error: Translation not available"
        end
    else
        return "Error: " .. response
    end
end

local originalText = "Hello, how are you?"
local targetLanguage = "es"
local translatedText = translateText(originalText, targetLanguage)
print("Original Text: " .. originalText)
print("Translated Text: " .. translatedText)

It worked thank you so much! Life saver

1 Like