Free Google Translate API for Python. Translates totally free of charge.
Project description
ANNOUNCEMENT: v4.0 is planned.
Googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate.
Compatible with Python 3.6+.
For details refer to the API Documentation.
Features
Fast and reliable - it uses the same servers that translate.google.com uses
Auto language detection
Bulk translations
Customizable service URL
HTTP/2 support
TODO
more features are coming soon.
Proxy support
Internal session management (for better bulk translations)
HTTP/2 support
This library uses httpx for HTTP requests so HTTP/2 is supported by default.
You can check if http2 is enabled and working by the ._response.http_version of Translated or Detected object:
>>> translator.translate('테스트')._response.http_version
# 'HTTP/2'
How does this library work
You may wonder why this library works properly, whereas other approaches such like goslate won’t work since Google has updated its translation service recently with a ticket mechanism to prevent a lot of crawler programs.
I eventually figure out a way to generate a ticket by reverse engineering on the obfuscated and minified code used by Google to generate such token, and implemented on the top of Python. However, this could be blocked at any time.
Installation
To install, either use things like pip with the package “googletrans” or download the package and put the “googletrans” directory into your python path.
$ pip install googletrans
Basic Usage
If source language is not given, google translate attempts to detect the source language.
>>> from googletrans import Translator
>>> translator = Translator()
>>> translator.translate('안녕하세요.')
# <Translated src=ko dest=en text=Good evening. pronunciation=Good evening.>
>>> translator.translate('안녕하세요.', dest='ja')
# <Translated src=ko dest=ja text=こんにちは。 pronunciation=Kon'nichiwa.>
>>> translator.translate('veritas lux mea', src='la')
# <Translated src=la dest=en text=The truth is my light pronunciation=The truth is my light>
Customize service URL
You can use another google translate domain for translation. If multiple URLs are provided, it then randomly chooses a domain.
>>> from googletrans import Translator
>>> translator = Translator(service_urls=[
'translate.google.com',
'translate.google.co.kr',
])
Advanced Usage (Bulk)
Array can be used to translate a batch of strings in a single method call and a single HTTP session. The exact same method shown above works for arrays as well.
>>> translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
>>> for translation in translations:
... print(translation.origin, ' -> ', translation.text)
# The quick brown fox -> 빠른 갈색 여우
# jumps over -> 이상 점프
# the lazy dog -> 게으른 개
Language detection
The detect method, as its name implies, identifies the language used in a given sentence.
>>> from googletrans import Translator
>>> translator = Translator()
>>> translator.detect('이 문장은 한글로 쓰여졌습니다.')
# <Detected lang=ko confidence=0.27041003>
>>> translator.detect('この文章は日本語で書かれました。')
# <Detected lang=ja confidence=0.64889508>
>>> translator.detect('This sentence is written in English.')
# <Detected lang=en confidence=0.22348526>
>>> translator.detect('Tiu frazo estas skribita en Esperanto.')
# <Detected lang=eo confidence=0.10538048>
GoogleTrans as a command line application
$ translate -h
usage: translate [-h] [-d DEST] [-s SRC] [-c] text
Python Google Translator as a command-line tool
positional arguments:
text The text you want to translate.
optional arguments:
-h, --help show this help message and exit
-d DEST, --dest DEST The destination language you want to translate.
(Default: en)
-s SRC, --src SRC The source language you want to translate. (Default:
auto)
-c, --detect
$ translate "veritas lux mea" -s la -d en
[veritas] veritas lux mea
->
[en] The truth is my light
[pron.] The truth is my light
$ translate -c "안녕하세요."
[ko, 1] 안녕하세요.
Note on library usage
DISCLAIMER: this is an unofficial library using the web API of translate.google.com and also is not associated with Google.
The maximum character limit on a single text is 15k.
Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times (so please use this library if you don’t care about stability).
Important: If you want to use a stable API, I highly recommend you to use Google’s official translate API.
If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.
Versioning
This library follows Semantic Versioning from v2.0.0. Any release versioned 0.x.y is subject to backwards incompatible changes at any time.
Contributing
Contributions are more than welcomed. See CONTRIBUTING.md
License
Googletrans is licensed under the MIT License. The terms are as follows:
The MIT License (MIT) Copyright (c) 2015 SuHun Han Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file googletransfork-1.0.0.tar.gz
.
File metadata
- Download URL: googletransfork-1.0.0.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6745fee38965873e0321966a5e98322eeba52c9259c2ff1941c1dd686340b16 |
|
MD5 | b0dcd8af565e6f2504d9dd7ad3824efb |
|
BLAKE2b-256 | bd37313d81e0fbc8d38132396e8a2549e31d385b56d62cbc8ca2ad98a7f1b72e |
File details
Details for the file googletransfork-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: googletransfork-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 345a08326c9db6250a5c9b258430d22d5a6eec97e1dbf195dc25cffc92f48b43 |
|
MD5 | 9930cedfbe65e1ce1645d8ea2c763caf |
|
BLAKE2b-256 | e48a1cc0ddc4beadb08d6e9a70dd05c3f1e215aad93b64156fda9c6312bcf6bd |