Click here to Skip to main content
13,903,016 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 29 Mar 2018
Licenced CPOL

How to Use Google Translator in Python?

, 29 Mar 2018
Rate this:
Please Sign up or sign in to vote.
This article is about the use of Google Translation package in Python.


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.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead
India India
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 29 Mar 2018
Article Copyright 2018 by Sharad_Agrawal
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid