IBepyProgrammer

Language Translation using Python

4 min read
Language Translation using Python

How to translate languages using Python

Translating from one language to another can be a daunting task. However, with the help of modern technology, it has become easier than ever. Most of us rely on Google Translate for this purpose, but have you ever wondered how it works behind the scenes? In this article, we will explore how to write a simple language translation tool in Python using the Goslate library. By the end of this article, you will have a basic understanding of how such a tool can be built.

So, what are the libraries we will use?

Python offers several libraries for language translation, the most common being:

  • Goslate is one of the many Python libraries used for language translation. It provides a simple interface to the Google Translate API, allowing developers to translate text between languages in their Python applications easily. The library is a wrapper for the Google Translate API, which is not an official or publicly documented API from Google.

  • Translate. A simple library that can also be used for text translation. This library offers integration with Microsoft Translation API, the Translated MyMemory API, and Libre Translate API among others.

  • Microsoft Translator Text API. This isn't a Python library itself, but Microsoft offers an API for text translation which can be accessed through Python using HTTP requests or SDKs.

  • TextBlob. This is a popular Python library built on top of NLTK (Natural Language Toolkit) and Pattern, providing a simple API for common natural language processing (NLP) tasks. It's designed to be an easy-to-use tool for processing textual data, including sentiment analysis, classification, translation, and more.

Requirements

For this tutorial, you can utilize an editor of your choice. We will be using Jupyter Notebook to demonstrate how we can perform language translation using Goslate, Translate, and TextBlob libraries. You will also need to have installed Python on your local machine but can also utilize Google Colab which is an online Jupyter Notebook editor provided by Google.

Link to google colab https://colab.google/.

1. Language Translation using Goslate Library

We can start by installing the Goslate library using pip.

!pip install goslate

We then import Goslate and proceed to translate a string of text.

import goslate

Steps required.

  • We first store the intended string datatype in a variable.

  • We then call the Goslate class from the imported Goslate library

  • We then translate the text by calling the translate method by passing the variable and the abbreviation of the language you want to translate the language text to.

original_text = "Let's perform language translation using python."
new_text = goslate.Goslate()
new_text.translate(original_text, 'es')

In our case, we want to translate from English to Spanish but also works for any language with a standard ISO language code.

This cell shows how you can translate text from English to Spanish
This cell shows how you can translate text from English to French.
This cell shows how you can translate text from English to German.

2. Language Translation using Translate Library

We can start by installing the Translate library using pip.

!pip install translate

We can then import the translator method from the translate library to translate text from one language to another.

from translate import Translator

Steps required.

  • We first store the intended string datatype in a variable.

  • We then translate the text by calling the translate method by passing the variable and the abbreviation of the language you want to translate the language text to.

original_language = Translator(from_lang = "english", to_lang = "japanese")

new_language = original_language.translate("We can translate text from English to Japanese using the translate library.")

print(new_language)

In our case, we want to translate from English to Japanese.

This cell shows the output as text in English is converted to Japanese

3. Language Translation using TextBlob Library

We can start by installing the TextBlob library using pip.

!pip install textblob

We can then import the TextBlob method from the textblob library to translate text from one language to another.

from textblob import TextBlob

Steps required.

  • We can store the TextBlob method in a variable and insert the intended text to be translated inside parenthesis.

  • We can then use the variable to translate to the desired language by using the abbreviation of the language with a standard ISO language code.

original_language = TextBlob("Let's use the textblob library to translate english to Portugese")

original_language.translate(from_lang='en', to = "pt-br")

In our case, we want to translate from English to Portuguese (Brazil).

This call shows the output as we translated text from English to Portuguese.

Conclusion

In this article, we learn what language is and how we can accomplish the task by writing simple Python scripts. In future articles, we will dive deeper into language translation and learn how to build more sophisticated Python applications that perform language translation, grammar correction, and much more. You can also find the Jupyter notebooks on my GitHub via the link https://github.com/IBepyProgrammer/Language-translation-in-python.

If you found this article helpful consider subscribing and sharing.

Thank you.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!