Can we use google translate in python?
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? Show
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?
SetupFirst 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 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 python3 -m venv ./myenv Ubuntu / macOSIf you use Ubuntu or macOS, activate it using the following command: source ./myenv/bin/activate WindowsThose 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. ImplementationIn this section, we will implement:
Create a new Python file called test_translation.py. ImportAdd the following import statement at the top of the file. from google.cloud import translate_v2 as translate Translate functionNext, create a new function called
Inside the function, instantiate a def translate_text(text, target_language, source_language=None): translate_client = translate.Client() ... Continue by calling the 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:
Translating a single stringNow, you can call the function normally in the same Python file as follows: ... result = translate_text('Hello, world!', 'de') print(result['translatedText']) # Hallo Welt! Translating a list of stringsIn 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. Other useful functionsGoogle Translation API also comes with additional functions for:
Getting a list of supported languagesFor the full list of supported languages and the corresponding ISO 639-1 language codes, you can use the ... 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:
Detecting the input languageFurthermore, the Python SDK comes with a ... 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:
ConclusionBy 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. Is Google Translate python free?Googletrans is a free python library that uses Google Translate API.
Which translator is used in python?Python googletrans is a module to translate text. It uses the Google Translate Ajax API to detect langauges and translate text.
How do you translate text in python?Introduction. from googletrans import Translator.. translator = Translator(). translated_text = translator. translate('안녕하세요. '). print(translated_text. text). translated_text = translator. translate('안녕하세요.', dest='ja'). How do I use google API for translation in python?Using the Translation API with Python. Overview.. Setup and requirements.. Enable the API.. Authenticate API requests.. Install the client library.. Start Interactive Python.. List available languages.. Translate text.. |