How to use google-trans-new ?

Duygu Aran
3 min readJan 7, 2021

Machine translation is the automatic translation of a text into a different language using computer software. One of the modules offered by python for this is google-trans-new.

In this article we will look at how to use the Google translate API in Python. The Google translate API uses the same servers that translate.google.com uses. So it gives the same results. google-trans-new released in November 2020.The latest version of Google Translate was updated in December 2020. You can find more detailed information here.

How to install ?

pip install google_trans_new

After importing the libraries, we can use as follows :

from google_trans_new import google_translatortranslator=google_translator()translated_text=translator.translate("merhaba dünya", lang_tgt='en')

Output :

We can define the code of the language we want to translate at ‘lang_tgt

Supported languages and codes :

'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'

Detect

This package includes the ability to detect the language of the texts. Let’s try this time by taking text from the user :

text = input(“Enter your text “)
# to get text from user
detector = google_translator()
detected_language= detector.detect(text)print(detected_language)

After entering the text we get this output :

Translate the text by using google-trans-new

We can see how we integrate it in a very simple application. We can create an application that translates the user uploaded .txt file into English and creates a new .txt file. Let’s see how we can create this project on Colab.

We use the pip install command again to install the package.

pip install google_trans_new

Import the necessary libraries :

from google_trans_new import google_translator
from google.colab import files
#to upload file on Colab

Upload the text.txt named file

After uploading, translate text to english :

upload_file = open(‘text.txt’, ‘r’)
#to open the uploaded file
text= upload_file.read()
translator = google_translator()

translated_text = translator.translate(text, lang_tgt=’en’)
#to translate text
upload_file.close()
#to close the file

Create new .txt file

translated_file = open (‘translated_text.txt’,’w’)
#to create new file and write to the file
translated_file.write (translated_text)
translated_file.close()

We can see the created file from here :

Here you can find all the codes.

--

--