Language detection & translation using Python | How to translate text from one language to another

There are many ways to translate one language into another one. Some people may suggest machine learning, or 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 with 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 anyone 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 a 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 the 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 an argument and detect the language with the help of the detect_language() function.

Language Translation:
I am making a function that 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 a 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 let's download the Python package first. Use the pip command to download the package :

pip install googletrans
Language Detection:
Detection of language is the 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"))

The output of the above code is:

output : Detected(lang=en, confidence=1.0)

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)

The 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 that takes 2 arguments. The first one is the text/string and the second is the 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 their symbols. It will definitely help 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,

Recent Articles

Changing Database from SQLite to MySQL - Django

One of the main benefit to use django for web development is that it comes with an inbuilt database/filesystem i.e SQLITE. Managing database and files is super easy in django. But there are some drawbacks of using SQLITE...

  - 2023-07-12
How to download a file in django

Sometimes we have to serve a file which may be a large file from our project directory. Suppose you want to transfer a file from your project directory to your friend or to some other person/server/platform....

  - 2023-07-12
How to change base url or domain in sitemap - Django

A sitemap is the first important SEO step which elaborates a website's content to search engines. It provides information to search engines about the available content on our site, which helps them to crawl webpages for indexing....

  - 2023-07-12
How to Make a Website

After reading this article you will never ever search for web development guide neither on Google nor on YouTube....

  - 2023-07-12
What is Javascript - Features and usages

I'm kind of a person who always likes to get started by installing the things & writing some code. I love that but this time JavaScript requires a little bit of the theoretical basic so that we can understand what things we are learning, how hard and powerful it is and where they can be applied....

  - 2023-07-12
Top 5 Interview Questions : Tips for HR round

In this article we are talking about the most famous HR questions and the mind-set to answer those HR questions....

  - 2023-07-12
How to get job in IT - Perfect resume guide

In today's article I want to talk about how to prepare the best resume to get a job in IT. Yes, everybody wants to have a job and in order to get a job you have to apply for it and when you apply for that, the resume is one good thing that you always submit....

  - 2023-07-12