
Global business
Software localization
Software development projects, including those in Python, often struggle with delivery as soon as an application needs to be made multilingual. For simple applications, for example, you might be using spreadsheets and a machine translation engine like Google Translate. However, if you have a complex application, with lots of text for translation, and want to rely on Google Translate, using spreadsheets for localization might turn into a nightmare: Copying and pasting text back and forth would take quite some time and most likely slow down your development process—but what if there is a better way to localize your Python app?
By creating a simple Python script, you can speed up the machine translation process from the start. There are multiple Python packages that offer these capabilities, but with the exception of Google's official API, all other packages are neither stable nor supported by Google. Hence, if you are looking for a more efficient way to use Google Translate for machine translation in your Python project, Google's official API can be a solid choice.
Google provides two different versions of the Cloud Translation API:
To keep things simple, this localization tutorial will stay focused on the Basic version. Shall we start?
First and foremost, you need to create a project via Google Cloud Console. You'll need a project number or ID when calling the Translation API. Feel free to check out the Google Cloud Translation setup guide to get more detailed information on:
By default, you can make a POST call to the following endpoint API using any programming language:
https://translation.googleapis.com/language/translate/v2
Just pass in the access token as the authorization bearer. The bearer token is passed as an HTTP header. Here is an example of how it will look like when using curl:
curl -X POST \ -H "Authorization: Bearer <access_token>" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{"q":["Hello world", "Welcome to Phrase blog"],"target":"de"}' \ https://translation.googleapis.com/language/translate/v2
You need to include a JSON body with the following fields:
{ "q": ["Hello world", "Welcome to Phrase blog"], "target": "de" }
Fortunately, Google provides its own Python SDK to make things easier for developers.
It is optional—but highly recommended—to create a virtual environment before you continue. Create the virtual environment as follows (replace ./myenv
based on your own preference):
python3 -m venv ./myenv
If you use Ubuntu or macOS, activate it using the following command:
source ./myenv/bin/activate
Those who rely on Windows should use the following command:
.\myenv\Scripts\activate
Run the following command to install the Python SDK:
pip install google-cloud-translate==2.0.1
Once you have installed the package, you can call it normally to translate any text.
In this section, we will implement:
Create a new Python file called test_translation.py.
Add the following import statement at the top of the file.
from google.cloud import translate_v2 as translate
Next, create a new function called translate_text
. The function accepts the following parameters:
Inside the function, instantiate a translate.Client()
object as follows:
def translate_text(text, target_language, source_language=None): translate_client = translate.Client() ...
Continue by calling the translate
function and return the output.
def translate_text(text, target_language, source_language=None): translate_client = translate.Client() result = translate_client.translate(text, target_language=target_language, source_language=source_language) return result
The output can be a dictionary or a list of dictionaries depending on your input text. The dictionary contains the following keys:
Now, you can call the function normally in the same Python file as follows:
... result = translate_text('Hello, world!', 'de') print(result['translatedText']) # Hallo Welt!
In addition, you can translate a list of strings by passing the list to the same function as follows:
... results = translate_text(['Hello, world!', 'What are you doing!'], 'de') for result in results: print(result['translatedText'])
The output will be a list of dictionaries instead.
Google Translation API also comes with additional functions for:
For the full list of supported languages and the corresponding ISO 639-1 language codes, you can use the get_languages
function:
... languages = translate_client.get_languages() for language in languages: print(u"{name} ({language})".format(**language))
It returns a list of dictionaries with the following fields:
Furthermore, the Python SDK comes with a detect_language
function that can identify the language of an input text. You can use it as follows:
... result = translate_client.detect_language(text) print("Confidence: {}".format(result["confidence"])) print("Language: {}".format(result["language"]))
The output is a dictionary with the following fields:
By now, you should be able to create a simple Python script to machine translate user-generated content with Google Translate. For more sensitive types of content, consider working with human translators on a software localization platform like Phrase. It will let you:
Sign up for a free 14-day trial, and see for yourself how it can make your life easier.
Last updated on October 20, 2022.