Google Translate
Use Bubble's API Connector to connect to Google's Translate API and automatically translate text strings in your app.
Last updated
Use Bubble's API Connector to connect to Google's Translate API and automatically translate text strings in your app.
Last updated
The API Connector is a powerful tool to connect to external applications and services.
In this article we'll look at how you can easily connect to Google Cloud and use their Translate service to translate strings of text in your app.
The first step to set up our API call is to ensure we have the proper authentication. From our Introduction to APIs article you'll remember that authentication is the process of identifying who the is in order to determine what they should have access to.
First, you'll need to find your Google Cloud API key. You'll find this in your Google Cloud dashboard (or set one up using our video tutorial on how to set up Google Cloud API keys):
API keys should be kept private to prevent unauthorized access and potential misuse of the associated API.
First, we will create a new API call by clicking the Add another API button in the API Connector settings. We can name the API Google Cloud, since this authentication covers all services that you have activated in the Cloud dashboard.
Now, let's study the Google Cloud API documentation to see what kind of authentication they require:
As you can see in the illustration above, Google asks us to pass the API key as a query parameter, which means we include it as a parameter in the endpoint URL.
Passing the API key in the URL is considered secure because the API key is routed through the Bubble server. This means that the API key is never visible to the users of your app. While in , the key is encrypted with HTTPS encryption.
Returning to the API Connector settings, let's set it up as follows:
Open the Authentication dropdown to select the authentication method
Pick Private key in URL
Set the Key name to key. This is the name of the URL parameter Google is looking for.
Finally, paste your API key in the Key value field.
Many API providers offer a test API key that let's you play around with the API without manipulating actual data. If you would like to use a test key in the Development version if your app, you can paste that key in the Development key value field visible in the screenshot above. Bubble will automatically use the test key for the development version and the live key for the live version.
That's all there is to it – we have now told Google who we are. When we authenticate with that API key, Google will proceed to check what we (the client) should have access to. Since we activated the Cloud Translation API, Google knows that we have access to that particular resource.
The next step is to set up the actuall call. The call we are looking for is one that lets us send a string of text, specify a language and then have Google return a translated version of the text we sent. Bubble will already have created an empty call called API call. Click the expand link on that call to expose its settings.
Again we need to consult the documentation. Google's Cloud Translation Quickstart guide shows us an example with the and the parameters:
Now let's use this information to set up the call in the API Connector.
First, we'll instruct the API Connector to seek out the resource we want to reach. As you can see in the screenshot above, Google spells it out for us in the header that says HTTP method and URL. This tells us that the is POST and the URL is https://translation.googleapis.com/language/translate/v2
Let's look at what that looks like in the API Connector:
First, we'll set Use as to Data, since we want to use the response (the translated string) as a data source
Next, we'll set the Data type to JSON, since the documentation told us that this is the format Google prefers
We'll set the HTTP method to be POST, since we are initiating an action (translating text)
Finally, we paste the URL from the documentation.
We have now set up the necessary settings for the call, and constructed the endpoint by combining the HTTP method and the resource URL.
The next step is to set up the needed parameters. After all, an API call that translates text won't get us far if we don't tell it what text to translate and what language to translate it to!
Looking again at the documentation (the same screenshot as earlier) we can see that Cloud Translate asks for a few parameters:
"q" = The string that we want to translate
"source" = the language we are translating from (optional)
"target" = the language we are translating to
"format" = the format we want returned (text) (optional)
If we don't include a source language, Cloud Translate will try to recognize the language on its own. Let's set up the two required parameters: the string (q) and the target language (target). We'll also include a value that we can use to test that the call works:
Initializing the call means that we run it once in order to get a response. Initializing serves two purposes:
It lets us know whether the call was successful or not
It provides us with a response that tells Bubble what the returned JSON data looks like. This in turn lets us set up the call as a data source, since Bubble knows its structure.
If everything is set up correctly, Bubble should display the following response. From here you can choose what fields to ignore, and what to include in the final call:
If you click the Show raw data link you can see the JSON-formatted text that Google returned:
As you can see, the translatedText parameter returned the translated string, and the detectedSourceLanguage returned the language code of the source language.
Now that the call has been set up to be a data source and we have initalized it, we can start using it in our application and translate texts on the fly. To find the data source we created, we use the Get data from external API data source:
That's how easily you can use the API Connector to set up an API call that you can then use around your app as a data source or an action.