Text translation is becoming increasingly important in a growing digital space like the internet. With users from multiple nationalities accessing online websites, it’s essential to create globally readable content. This involves the task of language translation.
Using Python, we can build a language translator to accomplish this goal. In this tutorial, we will understand 2 methods of building an English to Spanish language translator in Python:
- Using the Python package ‘translate’
- Using the Google Translate API ‘googletrans’
Create an English to Spanish language translator using Python
Using the Python package ‘translate’
‘Translate’ is a powerful python package that can be used to translate sentences from one language to another. The first step is to install the package by typing the following command in the terminal and pressing enter:
pip install translate
After the package is successfully installed, we can begin writing code. First, we have to import ‘Translator’ class from the translate module. By importing the ‘Translator’ class, we can create an instance of it to perform translations.
from translate import Translator
We can now define an instance of the ‘Translator’ class. The ‘Translator’ class passes 2 argument: ‘from_lang’ and ‘to_lang’. As the names suggests, these arguments are used to define the languages to be translated from and into respectively.
from translate import Translator lang_converter = Translator(from_lang = "English", to_lang = "Spanish")
After this, we are ready to take the user’s input in English and convert it to Spanish.
The user input string is assigned to a variable ‘initial_info’. In the next line, ‘the lang_converter’ instance translates ‘initial_info’ from English to Spanish and assigns it to the variable ‘lang_translated’. In this line, the ‘translate()’ method takes a string as input and returns the translated string.
Finally, the translated string is printed in Spanish on the screen. The full code is below for reference:
from translate import Translator lang_converter = Translator(from_lang = "English", to_lang = "Spanish") initial_info = input("Enter text in English to be converted to Spanish: ") lang_translated = lang_converter.translate(initial_info) print(lang_translated)
Enter text in English to be converted to Spanish: Hi, How are you? ¿Hola, cómo estás?
Using the Google Translate API ‘googletrans’
An API, or Application Programming Interface, enables communication between different software services. Through this, data can be exchanged. In this tutorial, we will use the Google Translate API in Python, the ‘googletrans’ library.
The first step is to install the package by typing the following updated command in the terminal and pressing enter:
pip install googletrans==4.0.0-rc1
After this is successfully installed, we can observe the languages supported by the library using the following code:
import googletrans print(googletrans.LANGUAGES)
Output:
{'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'}
from googletrans import Translator
We can now define an instance of the ‘Translator’ class without any arguments. Here, it is assigned to the variable ‘lang_converter’
from googletrans import Translator lang_converter = Translator()
from googletrans import Translator lang_converter = Translator() initial_info = input("Enter text in English to be converted to Spanish: ") lang_translated = lang_converter.translate(initial_info, src = 'en', dest = 'es') print(lang_translated.text)
Output:
Enter text in English to be converted to Spanish: Hi, how are you doing? ¿Hola cómo estás?
The above methods provide a simple way of translating user input text from English to Spanish using Python.
Thank you for reading this tutorial.