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.
- Using package management system PIP.
C:\>py -3 -m pip install googletrans
- 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)
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)
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.