[Edge Translate] How to Translate a Foreign Web Page in Microsoft Edge

Google Translate

Google Translate is probably one of the most popular services among people in multilingual settings. It has received a lot of attention before and gaining more and more traction since it was introduced as a built in add on with many Google products like (Chrome, Android and more). It is often required to translate text in applications in an autonomous way, for various reasons. Google Translate API at rescue. In this article we”ll focus on Google Translate API implementation for Ruby based (specifically Rails) applications.

Watching: Edge translate

Edge translate
Edge translate

Features

The API supports 103 languages. With over 500 million daily users, it”s a widely recognized service. The accuracy is increasing over time, although, not as accurate as human translation as of 2019. The service has a large potential to be used in many applications. Although unfortunately Google stopped offering translation API for free, it requires a payment method to be utilized now, with a monthly free limit of first 500,000 characters. Let”s move on to see how it is integrated with a Rails application.

Integration

Google Cloud Configuration

In order to configure the Translate API, we need a Google account and a payment method. Once both of these are in hand, we”re ready to go.

First, let”s head to Google Cloud Console.

From the console, create a new project.

*

Once the project is created, go to “Navigation Menu > APIs & Services > Dashboard”, and select “ENABLE APIS AND SERVICES”.Search for “cloud translate api”, and select the service.

*

Now “Enable” the service.

*

We still need the API credentials to access. it. From the “Cloud Translation API” dashboard, use “Create Credentials” link, which will redirect to a credential creation wizard.

*

Choose, “what credentials do I need”.

*

Fill up the information. Select the role “Project > Owner” and “Continue”. A JSON credential file will be downloaded. We are gonna use this credential in the next section.

See Also:  Troubleshooting Outlook email setup

The JSON credential will look as follows.

Readmore: How To Embed Gifs In Gmail, Insert An Animated Gif Into An Email

{ “type”: “service_account”, “project_id”: “hopeful-flame-21536”, “private_key_id”: “1ba86b210a719bddc7ea6a24193fa8b93c586c5f”, “private_key”: “—–BEGIN PRIVATE KEY—-eOmBtmnPUwbVqp1naQ+uCLubF0yBf4=n—–END PRIVATE KEY—–n”, “client_email”: “”, “client_id”: “101743723146221348410”, “auth_uri”: “https://accounts.google.com/o/oauth2/auth”, “token_uri”: “https://oauth2.googleapis.com/token”, “auth_provider_x509_cert_url”: “https://www.googleapis.com/oauth2/v1/certs”, “client_x509_cert_url”: “https://www.googleapis.com/robot/v1/metadata/x509/root-565%40hopeful-flame-21536.iam.gserviceaccount.com”}

Rails Application Configuration

First, we need to generate a Rails project scaffold. Once the scaffold is ready, we”ll add the following gems toGemfile.

# Gemfilegem “google-cloud”gem “google-cloud-translate”And, finally run bundle install to install the dependencies.

We need to import the google translate gem in order to use it. This is done by,

# application.rbrequire “google/cloud/translate”Now, that the library is set, we it”s time to set up authentication. Regardless of what environment management gem we are using, let”sset up the following environment variables, so that the client library ( google-cloud-translate and google-cloud ) can authenticateusing it.

# CLOUD_PROJECT_ID: “”GOOGLE_APPLICATION_CREDENTIALS: “config/”Here, CREDENTIAL_JSON is the JSON key file we downloaded in the Google Cloud Configuration section. We need to put the file inRails.root.join(“config”) directory.

Don”t forget to add the JSON key file to .gitignore file to avoid the credential from accidentally being added to the source control.

And, now we are ready to utilize the API. The first step is to initiate a Google translate instance.

# source.rbtranslate = Google::Cloud::Translate.new project: ENVNow, a translation can be performed using,

# source.rbtranslation = translate.translate “Hello world!”, from: “en”, to: “vi” #=> Chào thế giới!The translation response is simply a string. It is also possible to omit the from field, in which case, the API will utilize the Detect Language feature.

See Also:  How long does mail stay at a distribution center

Alternative to the google-cloud-translate Gem

Other than using the gem, there”s also a convenient REST API which can be utilized in order to have a better control on the internals.

A simple API invocation looks like the following.

curl -X POST -H “Authorization: Bearer “ACCESS_TOKEN> -H “Content-Type: application/json” –data “{ source_language_code: “en”, target_language_code: “ru”, contents: }” “https://translation.googleapis.com/v3beta1/projects//locations/global:translateText”A response format is identical to the following.

{ “translations”: }An access token can be extracted by utilizing the Google Cloud SDK (or google-cloud gem).

Google Translate API is available for a number of languages other than Ruby, including Java, Python and several others. It”s a very convenient API to use. Although it was free earlier, Google recently turned it into a paid service which is a bit shocking. A version 3 API is currently in beta. We”ll have another post in the future on the differences introduced in the new API. That”s all for now. Happy hacking!

Categories: Mail

Leave a Reply

Your email address will not be published. Required fields are marked *