Click here to Skip to main content
15,886,752 members
Articles / Programming Languages / Python
Tip/Trick

How to Use Google Translator in Python?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
29 Mar 2018CPOL2 min read 47K   493   6   3
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++
    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.

C++
# 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.

C++
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.

C++
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.

C++
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.

C++
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.

Python
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.

Python
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:

Python
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerPlease see this code as to translate your website with googletrans in any languages Pin
Rob Rob 202127-Aug-21 12:03
Rob Rob 202127-Aug-21 12:03 
GeneralGood article Pin
rammanusani26-Mar-19 8:07
rammanusani26-Mar-19 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.