Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Use Google Translator in Python?

0.00/5 (No votes)
29 Mar 2018 1  
This article is about the use of Google Translation package in Python.

Introduction

The main objective of this article is to use Google translation in Python script, to achieve an easy way to translate string from one language to another.

Background

Basic knowledge of Python language is required. Google provides language translation package for Python.

Following are the ways to install Google trans package in your system.

  1. Using package management system PIP.
    C:\>py -3 -m pip install googletrans
  2. We can download it from here and put "googletrans" directory at the python install path in Lib directory.

Using the Code

Google trans package contains many functionalities, but here we are discussing about googletrans translate functionality.

Basic Use: First, we need to create Translator class object.

# Language Translator
from googletrans import Translator  # Import Translator module from googletrans package

translator = Translator() # Create object of Translator.

Now call the translate API to translate the source string, if we do not define source language, Google translate auto detects the source language and translates string into English by default.

translated = translator.translate('안녕하세요') 

# Source language auto detect by google trans
# By default destination language is English

Return value of translate API is a Translated class object, which has the following member variables.

  • src – source language (default: auto)
  • dest – destination language (default: en)
  • origin – original text
  • text – translated text
  • pronunciation – pronunciation

To access the translated string and source language, access member variables of translated object.

print(" Source Language:" + translated.src) 
# Output: Source Language: ko

print(" Translated string:" + translated.text)
# Output: Translated string: Good evening

We can also provide the source and destination language in translate API.

translated = translator.translate('안녕하세요', src='ko') # Pass only source language
translated = translator.translate('안녕하세요', dest='en') # Pass only destination language
translated = translator.translate('안녕하세요', src='ko', dest='en') # Pass both source and destination

We can also get the pronunciation of the source string.

translated = translator.translate('안녕하세요', src='ko', dest='ja')

print(" Source Language:" + translated.src)
# Output: Source Language: ko

print(" Translated string:" + translated.text) 
# Output: Translated string: こんにちは

print(" Pronunciation:", translated.pronunciation)
# Output: Pronunciation: Kon'nichiwa​​​​

Bulk request: We can pass list of source string to translate in destination language.

translatedList = translator.translate(['Hello Friends','Welcome on Codeproject',
'Have a good day'], dest='ja')

for translated in translatedList:
           print(translated.origin, '->', translated.text)

# Output:
# Hello Friends -> 皆さん、こんにちは
# Welcome on Codeproject -> Codeprojectへようこそ
# Have a good day -> 良い一日を過ごしてください

Detect Language: Google trans also provides detect API, as its name implies, identifies the language also provide the confidence.

detected = translator.detect(' 皆さん、こんにちは')

Return value of detect API is a Detected class object, which has the following member variables.

  • lang – detected language
  • confidence – the confidence of detection result (0.00 to 1.00)

To access the detected language and the confidence, perform:

print('Detected Language:', detected.lang, ' with confidence: ', detected.confidence)

# Detected Language: ja  with confidence:  1

Points of Interest

Python is a very strong language with Google trans module we can create verity of script which can fulfill language translation and language detection (from text) requirements very efficiently and easily.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here