googletrans

repository·main·Indexed 26 days ago

https://github.com/ssut/py-googletrans

An unofficial Google Translate API for Python (version 4.0.2) that leverages the Google Translate Ajax API. It supports asynchronous operations, HTTP/2 for faster performance via the hyper package, bulk translations, and automatic language detection. The library provides a Translator class for translating text and detecting languages, along with constants for ISO 639-1 language codes.

Tokens
3.3K
Snippets
5
Records
33
Agent score
88%

What's inside googletrans

  1. Install the Solar theme for Sphinx

    main

    To use the Solar theme in a Sphinx project, follow these steps:

    1. Download and extract the theme archive.
    2. Locate your Sphinx project's conf.py file.
    3. Set the html_theme parameter to 'solar'.
    4. Set the html_theme_path parameter to the absolute or relative path of the directory containing the extracted Solar theme files.
  2. Customize service URLs

    main

    You can provide a list of service_urls to the Translator constructor. If multiple URLs are provided, the library randomly chooses one. You can also point to translate.googleapis.com to use the direct API instead of the webapp-based service, which may help avoid unstable token issues.

    from googletrans import Translator
    
    # Use multiple domains (randomly selected)
    translator = Translator(service_urls=[
        'translate.google.com',
        'translate.google.co.kr',
    ])
    
    # Use direct Google API domain to avoid token issues
    translator = Translator(service_urls=[
        'translate.googleapis.com'
    ])
  3. Important usage notes and limitations

    main

    Limitations

    • Character Limit: The maximum character limit for a single text is 15,000.
    • Stability: This library uses the Google Translate Ajax API (web version), which does not guarantee 100% stability. For production-critical applications, use the official Google Cloud Translation API.
    • IP Bans: If you encounter HTTP 5xx errors or specific error codes like #6, your client IP address may have been temporarily banned by Google.
  4. Important usage limitations and troubleshooting

    main

    Limitations

    • Character Limit: The maximum character limit for a single text request is 15,000 characters.
    • Stability: This is an unofficial library using the Google Translate web API. It does not guarantee stability and is not associated with Google.

    Troubleshooting

    • IP Bans: If you encounter HTTP 5xx errors or specific error codes (like #6), your client IP address may have been banned by Google. For stable production use, it is highly recommended to use the official Google Cloud Translation API.
  5. Detect languages with detect()

    main

    The detect method identifies the language used in a given string and returns a Detected object containing the language code and confidence level.

    import asyncio
    from googletrans import Translator
    
    async def detect_languages():
        async with Translator() as translator:
            result = await translator.detect('이 문장은 한글로 쓰여졌습니다.')
            print(result)  # <Detected lang=ko confidence=0.27041003>
    
    asyncio.run(detect_languages())
  6. Translate text using Translator.translate()

    main
    Use the Translator.translate() method to translate strings. If the src (source language) is not provided, the library attempts to auto-detect it. You can specify the target language using the dest parameter.
  7. Translate text asynchronously with Translator

    main
    Use the Translator class within an async with context manager to perform translations. If the src language is not provided, the library attempts to auto-detect it. You can specify the destination language using the dest parameter.
  8. Perform bulk translations

    main

    You can pass a list of strings to the translate method to translate a batch of strings in a single method call and a single HTTP session.

    import asyncio
    from googletrans import Translator
    
    async def translate_bulk():
        async with Translator() as translator:
            translations = await translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
            for translation in translations:
                print(translation.origin, ' -> ', translation.text)
    
    asyncio.run(translate_bulk())
  9. Verify HTTP/2 support

    main

    The library uses httpx and supports HTTP/2 by default. You can verify the HTTP version of a response by accessing the ._response.http_version attribute on the Translated or Detected object.

    >>> translator.translate('테스트')._response.http_version
    # 'HTTP/2'