Google Translate API
In this article we look at activating and provisioning the Google Translate API to perform automated translations.
Table of contents
Since we also use the Google Translate API for the automatic translation of our articles, we wanted to take a closer look at the integration and use of the API in this article.
Introduction
If you want to translate texts from one language into another language, there are various services and websites available on the Internet today, such as Google Translate or DeepL Translator. But you don't always want to do the translations manually on the website; you might want to translate larger texts or even entire blogs automatically at the touch of a button. This makes sense especially if the text contains a lot of formatting that should be retained after translation. In this article we look at the Google Translate API in its simplest form and how you can use it for yourself.
Configuration
Before you can use the API, you need a Google account or, in effect, an API key to be able to call up the interface.
Google Cloud
For the Google Cloud you need an account in order to be able to consume APIs and other Google Cloud services. You can do this with your private Google account or register with a new account with Google. You then have to create a project to which you can later assign the service.
Cloud Translation API
After registering the account, you can log in and search for the API in the Cloud Marketplace. You can then provision the API using the "Activate" button. The project should have a payment method stored, even if using the API is free up to a certain number of characters.
API Key
Once you have activated the API in your project, you can choose the type of authentication. We would like to access the API via API key, as this is the easiest way. To do this, you can, for example, go to the API administration via the Marketplace.
In the "Login Details" area, you can select the appropriate method using the "Create Login Details" button.
After a short wait, the key has been created, which you can copy for further use. You can also use the settings to define whether the API key can only be used via a specific website, IP address or app. This also reduces the risk that if you lose the key, it will be misused and you will incur high costs.
Testing
In the next step, we want to test the API via POSTMAN to validate input, behavior and output for us. Since we are using an API key, we have to append it as a parameter to the URL for the call. The base URL therefore looks like this:
https://translation.googleapis.com/language/translate/v2?&key=<API-KEY>
In Postman we now configure a POST request to call the API. Don't forget to set the content type.
Finally, we give the request a payload that should be translated accordingly.
{
"q": [
"Test Artikel",
"Bitte diesen Text ins Englische übersetzen."
],
"target": "en"
}
If we send the request to the API, we will receive an HTTP Code 200 if successful and will receive the translated texts back. You can find more information about the API in the official documentation from Google for the API. The various parameters are described there again.
Conclusion
After a little effort in the configuration, we quickly get a result when using the API and have the option of translating one or many texts at once. Please note the limits of the API, which are described in the documentation.