Using google's translate api in roblox, is it possible?

Hey guys! As you may or may not know, I am working on a small administration commands script. I thought that adding translations would be a great idea, and I hit a roadblock once I stumbled upon google’s website.

  1. What do you want to achieve?
    I want to create a gui that allows players to translate chat, guis, and surfaceguis into a preferred language.
  2. What is the issue?
    Google’s api doesn’t have directions for lua and I have barely any experience with web apis.
  3. What solutions have you tried so far?
    I have tried looking for other apis I could use, however none seem to work.

There are 3 different apis:

  • Get language: Gets the language of a string.
  • Get supported languages: Gets a table of supported languages.
  • Translate: Translates text from one language to another.

So far, the only helpful information that I could find was on cloud.google.com, the website which lists code examples for many languages. Unfortunately, lua is not one of them. If anyone knows how to do this, I would appreciate the help!

4 Likes

You can’t really use the google translate api directly in Roblox, you will have to make use of an external language like NodeJS that supports the API and then use HttpService to get data from the NodeJS web App to your game, in and out.

1 Like

I see. How can I use NodeJs? I heard you can host it on a website called Glitch, however I found that code will “sleep” or stop working after a while, and they don’t allow pinging to keep it awake. Is there any other free host? Sorry if I seem pretty clueless.

You can self host, but it will require some work. For small game shouldn’t be a problem.

1 Like

You can use Heroku or Google Cloud Services to host them too, just do some research on the web. But actually first learn the basics of NodeJS or it is pretty hard to understand what the code will be doing for you itself, other than that its a really good language.

2 Likes

Ok then. Thank you so much for the help!

Also, NodeJS is basically JavaScript but it’s little different as it runs on server and not on client and not in the browser. For example, in JS you can do document.createElement(), in NodeJS you can’t.

2 Likes

Just adding on, you can learn the basics of NodeJS from W3C, its basically JS, except its a Runtime Environment.

Heres the link: Node.js Introduction

1 Like

Very disappointed with the replies in this thread. It is very well possible to interact with the google API from Roblox by using HTTPService. Using alternative languages to create a proxy won’t be easier since you have to understand the REST concept either way.
Google has put in clear instructions which work for any language which implement REST. In roblox you can use this function HttpService:RequestAsync to make requests. You won’t get around learning how these APIs work.

2 Likes

Wow, this is very helpful! I can’t thank you enough!

It looks like a couple more problems occured with my script. Here is what I have right now:

local response = http:RequestAsync(
		{
			Url = "https://translation.googleapis.com/language/translate/v2",  -- This website helps debug HTTP requests
			Method = "POST",
			Headers = {
				["Content-Type"] = "application/json"  -- When sending JSON, set this!
			},
			Body = http:JSONEncode({
				["q"]="Hola.",
				["target"]="en",
				["model"]="base"
			})
		}
	)
if response.Success then
	print("Status code:", response.StatusCode, response.StatusMessage)
	print("Response body:\n", response.Body)
else
		print("The request failed:", response.StatusCode, response.StatusMessage)
end

I just modified the script from the api reference to match the instructions. Right now, I am attempting to translate the word hola into english, however I keep getting the error The request failed: 403 Forbidden . How can I fix this and am I using the wrong formats?

403 means that you dont have permissions to use the resource. The google Translate API is only accessible to those who have a GCP account which you can sign up for. You have to activate the translate API in a project and create auth credentials. Setup  |  Cloud Translation  |  Google Cloud

Personally I think an API Key are the easiest auth method which you can create after activating the translate API. https://cloud.google.com/docs/authentication/api-keys
It’s transmitted as a parameter so you basically change your URL to: https://translation.googleapis.com/language/translate/v2?key=API_KEY
The first 500 000 characters in a month are free of charge. You can set quotas in GCP which disable the endpoint if it reaches a certain amount of requests.

1 Like

Are the translations going to be filtered? Google Translate has no problem translating things into well, let’s just say bad things.

1 Like

According to the website, it looks like You can be charged $20 for every 1 million characters for phrase-based translation predictions operations. The program is meant to be part of admin commands so it should be public use, however I don’t wish to pay for an api like this. Should I be worried and is there an alternative?

Translated chat will be filtered. As for in-game text, it will not be filtered as I would trust developers not to put swear words into games. If a bad word slips into someone’s game, there is always a way to report it. It might ruin a game if a lot of the roblox-appropriate text is filtered.

1 Like

Public software which use APIs, which require an API Key, almost always require the end user to insert their own key into a configuration file. Also I’m pretty sure that Google forbids you to make any keys public and will delete them regardless if they’re overused or not.
Perhaps you can find an alternative translation service which is completely free.

2 Likes

Alright, I’ll look around. Thanks so much for the help!

2 Likes