ZetCode

Python Google translator

last modified October 30, 2026 Python 3.15

In this article we show how to translate text and detect languages in Python with the googletrans module.

googletrans

googletrans is an unofficial Python client for Google Translate. It uses the same web API as the Google Translate website to detect languages and translate text. It needs no API key, but it is not supported by Google; the API may change, and Google may limit the number of requests. For production use, the official Cloud Translation API is a safer choice.

Since version 4.0, the library is asynchronous. It is built on top of the httpx library, and its methods are coroutines that we run with asyncio.

$ pip install googletrans

Python googletrans list languages

In the first example, we list the supported languages.

languages.py
import googletrans

print(len(googletrans.LANGUAGES))

for code, name in list(googletrans.LANGUAGES.items())[:8]:
    print(f'{code:>5}  {name}')

print(googletrans.LANGCODES['slovak'])
print(googletrans.LANGUAGES['hu'])

The googletrans.LANGUAGES dictionary maps language codes to language names; the LANGCODES dictionary maps names to codes. We print the number of supported languages, the first eight entries, and look up a code and a name.

$ python languages.py
245
  abk  abkhaz
  ace  acehnese
  ach  acholi
  aar  afar
   af  afrikaans
   sq  albanian
  alz  alur
   am  amharic
sk
hungarian

Python googletrans detect languages

The detect method determines the language of a text.

detecting.py
import asyncio

from googletrans import Translator

text1 = '''A Római Birodalom (latinul Imperium Romanum) az ókori Róma által
létrehozott államalakulat volt a Földközi-tenger medencéjében.'''

text2 = '''Vysoké Tatry sú najvyššie pohorie na Slovensku a v Poľsku a sú zároveň
jediným horstvom v týchto štátoch s alpským charakterom.'''


async def main():

    async with Translator() as translator:

        dt1 = await translator.detect(text1)
        print(dt1)

        dt2 = await translator.detect(text2)
        print(dt2.lang, dt2.confidence)


asyncio.run(main())

We have two texts; the first is Hungarian, the second Slovak. The Translator is used as an asynchronous context manager, which closes its HTTP connections at the end. The detect coroutine returns a Detected object with the language code and the confidence of the guess.

$ python detecting.py
Detected(lang=hu, confidence=1)
sk 1

Python googletrans simple translation

The translation is done with the translate method of the Translator.

simple.py
import asyncio

from googletrans import Translator


async def main():

    async with Translator() as translator:
        translated = await translator.translate('Бороди́нское сраже́ние')

    print(translated.text)
    print(translated.src, '->', translated.dest)


asyncio.run(main())

If we do not specify the source and the destination languages, googletrans detects the source language and translates the text into English. The translate coroutine returns a Translated object; the translation is in its text attribute, the detected source language in src, and the destination language in dest.

$ python simple.py
Battle of Borodino
ru -> en

The source and destination languages

In the following example, we specify the source and the destination languages.

source_dest.py
import asyncio

from googletrans import Translator

text = 'Praha je hlavní město České republiky.'


async def main():

    async with Translator() as translator:
        translated = await translator.translate(text, src='cs', dest='hu')

    print(translated.origin)
    print(translated.text)


asyncio.run(main())

The example translates a Czech sentence into Hungarian. The source language is specified with the src option and the destination with the dest option. The origin attribute contains the original text.

$ python source_dest.py
Praha je hlavní město České republiky.
Prága a Cseh Köztársaság fővárosa.

Python googletrans translate list

We can translate a list of texts.

translate_list.py
import asyncio

from googletrans import Translator

data = ['Dobrý deň', 'majestátny orol', 'krehká dohoda']


async def main():

    async with Translator() as translator:
        translated = await translator.translate(data, src='sk', dest='en')

    for trans in translated:
        print(f'{trans.origin} -> {trans.text}')


asyncio.run(main())

When we pass a list, the translate coroutine returns a list of Translated objects. The texts are translated with up to two concurrent requests by default; the limit is set with the list_operation_max_concurrency option of the Translator.

$ python translate_list.py
Dobrý deň -> Hello
majestátny orol -> majestic eagle
krehká dohoda -> a fragile deal

Pronunciation

For languages that do not use the Latin alphabet, the pronunciation attribute contains a romanized transcription.

pronunciation.py
import asyncio

from googletrans import Translator


async def main():

    async with Translator() as translator:

        for dest in ('ru', 'ja', 'el'):
            res = await translator.translate('Good morning', src='en', dest=dest)
            print(f'{dest}: {res.text} ({res.pronunciation})')


asyncio.run(main())

We translate a greeting into Russian, Japanese, and Greek and print the translations with their transcriptions. For languages written in the Latin alphabet, the attribute is None.

$ python pronunciation.py
ru: Доброе утро (Dobroye utro)
ja: おはよう (Ohayō)
el: Καλημέρα (Kaliméra)

Translating into many languages concurrently

Since the methods are coroutines, we can run several translations concurrently with asyncio.gather.

many_languages.py
import asyncio

from googletrans import LANGUAGES, Translator

text = 'The weather is nice today.'
langs = ['de', 'fr', 'es', 'it', 'pl', 'sk']


async def main():

    async with Translator() as translator:
        results = await asyncio.gather(
            *(translator.translate(text, src='en', dest=lang) for lang in langs)
        )

    for lang, res in zip(langs, results):
        print(f'{LANGUAGES[lang]:<8} {res.text}')


asyncio.run(main())

We translate one sentence into six languages. The generator expression creates a coroutine for each language and gather runs them concurrently; the results come back in the order of the languages. We take the language names from the LANGUAGES dictionary.

$ python many_languages.py
german   Das Wetter ist heute schön.
french   Il fait beau aujourd'hui.
spanish  Hace buen tiempo hoy.
italian  Il tempo è bello oggi.
polish   Pogoda jest dziś ładna.
slovak   Dnes je pekné počasie.

Invalid language

An unknown language raises a ValueError.

invalid_lang.py
import asyncio

from googletrans import Translator


async def main():

    async with Translator() as translator:

        try:
            await translator.translate('falcon', dest='xx')
        except ValueError as e:
            print(e)

        res = await translator.translate('falcon', dest='slovak')
        print(res.text)


asyncio.run(main())

The xx code is not a valid language, so the library raises an exception before sending the request. Besides codes, the src and dest options also accept language names.

$ python invalid_lang.py
invalid destination language
sokol

Source

py-googletrans on GitHub

In this article we have worked with the Python Google translator.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.