June 14, 2020, 11:31 a.m.

Language detection & translation using python | How to translate text from one language to other

There are many ways to translate one language into another one. Some people may suggest machine learning, deep learning or may build some neural networks to translate one language to another language.
But if you don't want to go deep in coding then google is always there for us. We can translate any language i.e known language in this world into other languages by the help of google.

Today i am going to show you how you can detect any language text and how you can translate any language text into another language. For this purpose i will tell you two methods. You can use any one you like. I will keep the process step by step to make a clear understanding .

1) Method 1 - Textlblob
2) Method 2 - Google Translate

Method 1 In this method we will use a python library called Textlblob . Textblob is a python module used for text processing and it is mainly used for processing the known natural languages.
Oh!! MY GOD, You are genius. Yes you are guessing right. First of all we have to install/download the python package i.e Textlblob.
Use the following command to download the python package for manipulating the languages :

pip install textblob

Language Detection :

First we will learn how we can detect a language i.e from text. Try the following code to detect the language from text.
from textblob import TextBlob
x = TextBlob('I can do this bro')

hence you can pass any language as text/string in Textblob class as argument and detect the language by the help of detect_language() function.

Language Translation :

I am making a function which takes 2 arguments. The first argument is the text or a string and the second argument is the destination language. The function returns the destination language value text as string. You can use the following code directly if you wish to do so.
from textblob import TextBlob
def MyFunction(value,dest):
   x = TextBlob(str(value))
   return x

Using the function for translation :
text="i am genius"

output: "मैं प्रतिभाशाली हूं"

Method 2 Now we will use Google Translate to perform translation operations. This python library is also free and unlimited for use. So lets download the python package first. Use the pip command to download the package :
pip install googletrans

Language Detection :

Detection of language is same as the previous method, just use the functions and classes from googletrans package by passing the required arguments to them. For language detection you can use the following code -
from googletrans import Translator
translator = Translator()
print(translator.detect("who am i"))

Output of the above code is :
output : Detected(lang=en, confidence=1.0)

The confidence is nothing but the value of accuracy. If the input text is correct then the confidence value will be 1.0 . Remember that 1.0 is the highest value.

Language Translation :

You can translate the language by using the functions listed in the following example -
from googletrans import Translator
translator = Translator()
print(translator.translate('तुम कौन हो',dest="en"))
print(translator.translate('तुम कौन हो',dest="en").text)

Output of the above code is :
output : Translated(src=hi, dest=en, text=Who are you, pronunciation=None, extra_data="{'translat...")
output : "Who are you"

Now again i am making a function which takes 2 arguments. First one is the text/string and the second is destination language.

The function, its use and the output are shown here :
from googletrans import Translator
def MyFunction(value,dest):
   translator = Translator()
   return translator.translate(value,dest=dest).text
value="work is completed"

output : काम पूरा हो गया है

Here is a list of languages with there symbols . It will definitely helps you , Just look into the list for your destination language and use it directly in your code.
ro : Romanian,
is : Icelandic,
pl : Polish,
ta : Tamil,
yi : Yiddish,
be : Belarusian,
fr : French,
bg : Bulgarian,
uk : Ukrainian,
hr : Croatian,
bn : Bengali,
sl : Slovenian,
ht : Haitian Creole,
da : Danish,
fa : Persian,
hi : Hindi,
fi : Finnish,
hu : Hungarian,
ja : Japanese,
ka : Georgian,
te : Telugu,
zh-TW : Chinese Traditional,
sq : Albanian,
no : Norwegian,
ko : Korean,
kn : Kannada,
mk : Macedonian,
zh-CN : Chinese Simplified,
sk : Slovak,
mt : Maltese,
de : German,
ms : Malay,
sr : Serbian
el : Greek,
eo : Esperanto,
en : English,
af : Afrikaans,
sw : Swahili,
ca : Catalan,
it : Italian,
iw : Hebrew,
sv : Swedish,
cs : Czech,
cy : Welsh,
ar : Arabic,
ur : Urdu,
ga : Irish,
eu : Basque,
et : Estonian,
az : Azerbaijani,
id : Indonesian,
es : Spanish,
ru : Russian,
gl : Galician,
nl : Dutch,
pt : Portuguese,
la : Latin,
tr : Turkish,
tl : Filipino,
lv : Latvian,
lt : Lithuanian,
th : Thai,
vi : Vietnamese,
gu : Gujarati,

Some recent posts

How to deploy a django website on pythonanywhere | django Web app deploying...

Complete process of changing database from SQLite to MySQL - How to migrat...

" How to download a file in django | Making a large file downloadable from ...

How to use proxy in python requests | http & https proxies - Scraping...

Top Django Querysets, How to filter records | Fetching data from database e...

How to change base url or domain in sitemap - Django ...

How to Make a Website - Everything you should know about web development...

What is Javascript - Features and usages ...

Top 5 Interview Questions : Tips for HR round ...

How to get job in IT : Perfect resume guide for IT job ...

View all...