Basic usage

Let’s use the Googletrans library to translate simple sentences into specific languages and to use the ability to auto-detect languages.


1. Translate

from googletrans import Translator

translator = Translator()
print(translator.translate('안녕하세요'))
Translated(src=ko, dest=en, text=Hi, pronunciation=None, extra_data="{'examples...")

Import the Translator from googletrans.

Enter a sentence to translate() . The result is as above.


print(translator.translate('안녕하세요').text)

When you print translator.translate('안녕하세요').text , the translated sentence is printed.



2. Set language

from googletrans import Translator

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

googletrans.LANGUAGES contains code information of 106 languages in the form of ‘dictionary’.

The above results will be printed.



3. Detect Language

from googletrans import Translator

translator = Translator()
print(translator.detect('이 언어는 한국어입니다.'))
print(translator.detect('veritas lux mea'))
Detected(lang=ko, confidence=1)
Detected(lang=la, confidence=0.66786897)

When you print out a sentence using detect() , the results are as shown above.

lang represents the code of the detected language, and confidence indicates the reliability of the results (0-1).

Prev