Translate Texts with Google Translate API Using Golang
How to Translate Texts with Google Translate API Using Golang?
Google Translate API is a powerful tool for translating text from one language to another. In this tutorial, we will show you how to use the Google Translate API in Go (Golang) to translate text from one language to another.
Prerequisites
- Go (Golang) installed on your machine.
- An active Google Cloud Platform account.
- A Google Cloud Platform project.
- A service account and its credentials.
Step 1 – Create a Service Account
Before you can use the Google Translate API, you need to create a service account and set up your environment variables. A service account is a special type of Google account that allows applications to access Google Cloud Platform services without linking to any particular user’s account. To create a service account:
- Sign in to the Google Cloud Platform console.
- Select the project for which you want to create the service account.
- In the Google Cloud Platform console, select IAM & Admin > Service accounts from the left-hand menu.
- Click Create Service Account.
- Enter a name and description for the service account, and then click Create.
- Select the checkbox next to the service account, and then click Create Key.
- In the Create key dialog, select JSON as the key type and click Create.
- A JSON file containing the credentials for your service account will be downloaded. Copy this file to the directory where you will be running the Google Translate API code.
Step 2 – Set up Environment Variables
Next, set up environment variables so that your code can use the credentials from your service account. To do this, open the Terminal window and type in the following command:
export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/JSON/file
Where “path/to/your/JSON/file” is the path to the JSON file containing the credentials for your service account.
Step 3 – Install the Google Translate Client Library
Now, you need to install the Google Translate client library. This library provides a client for Google Translate API. To install it, open the Terminal window and run the following command:
go get -u cloud.google.com/go/translate
Step 4 – Write the Code to Translate Text
Now that you have the library installed, you are ready to write the code to translate text. To do this, create a new file called translate.go and add the following code:
// translate.go
package main
import (
“context”
“fmt”
"cloud.google.com/go/translate"
)
func main() {
ctx := context.Background()
// Creates a client.
client, err := translate.NewClient(ctx)
if err != nil {
// Handle error.
}
// Sets the target language.
target, err := language.Parse("ja")
if err != nil {
// Handle error.
}
// Translates the text into Japanese.
translations, err := client.Translate(ctx, []string{"Hello world!"}, target, nil)
if err != nil {
// Handle error.
}
fmt.Printf("Text: %v\n", translations[0].Text)
}
The code above uses the Google Translate API to translate the text “Hello world!” into Japanese.
Step 5 – Compile and Run the Code
Once you have written the code, you need to compile and run it. To do this, open the Terminal window and run the following command:
go run translate.go
If everything was setup correctly, you should see the translation printed to the terminal window:
Text: こんにちは世界!