Skip to main content

A python module which lets you use Google Translate (translation, transliteration, defintion, language detection, etc.) by parsing the website.

Project description

pyGoogleTranslate

A python module for Google Translate (without using the API)

It lets you use Google Translate (translation, transliteration, defintion, language detection, etc.) by parsing the website.

This module lets you get four things:

  • translate(text, destination_language, source_language)

Translates the given text into the chosen language.

text: The text to translate
destination_language: The language you want to get.
source_language (default: auto): The language the text is in. ('auto' detects automatically the language the text is in)
cache (default: False): Boolean value which determines if you want to cache the result in the translation cache file (to make the same translation or reverse translation faster the next time).
debug (default: False): Boolean value which determines if you want to make a log file with what's happening when you run this function (useful to troubleshoot).

Returns a string.

  • detect_language(text, result_language)

Gives the language of the given text.

text: The text to check.
result_language: The language of the output language.

Returns a string.

  • transliterate(text, source_language)

Returns the transliteration provided by Google Translate (if available).

text: The text to check.
source_language (default: auto): The language the text is in.

Returns a string.

  • definition(text, source_language)

Returns the word type (i.e Interjection, Noun), defintion (if available) and sentence example where the word could be used (if available)

text: The text to check.
source_language (default: auto): The language the text is in.

Returns a dictionnary.

Other functions

  • browser(browser_name, executable_path)

To choose the headless browser used by pyGoogleTranslate.

browser_name: the name of the browser you want to use.
executable_path (default: PATH): sets the executable path for your browser. 
If executable_path is not changed, pyGoogleTranslate will consider that the browser driver/executable is in your PATH (for example if you downloaded the driver with Homebrew).
  • browser_kill()

Kills the browser process in use.

no argument to pass.

This needs to be call at the end of the execution of your program or when you plan to stop python because the browser will still be opened even if you shut down Python if you don't (which can results in multiple browsers opened even if you don't use them until you manually kills them in your activity monitor).

Installation

You can install pyGoogleTranslate via PIP the Python Package Index Manager.

pip install pyGoogleTranslate

But you also need to install a compatible browser:

Browser browser() command Installation Notes
PhantomJS pyGoogleTranslate.browser('phantomjs') On macOS with Homebrew brew cask install phantomjs PhantomJS is not maintained anymore but is the most lightweight of all three. ⚠️It seems to not handle Japanese Characters well when rendering.
Firefox pyGoogleTranslate.browser('firefox') Install Firefox (with the name Firefox) and install the driver (on macOS with Homebrew brew install geckodriver) I tested pyGoogleTranslate on Firefox and it seems to be quite long to start but works well with Japanese Characters which seemed to not work with PhantomJS. Seems to use lots of CPU when used multiple times in a row.
Chrome pyGoogleTranslate.browser('chrome') Install Google Chrome and install the driver (on macOS with Homebrew brew cask install chromedriver) Same as Firefox but seems to use less CPU.

Usage

import pyGoogleTranslate
>>> pyGoogleTranslate.browser('<browser you want to use>')


>>> pyGoogleTranslate.translate('Hello', 'ja')
'こんにちは'

>>> pyGoogleTranslate.translate('Bonjour', source_language='french', destination_language='英語')
'Hello'

>>> pyGoogleTranslate.detect_language('Hola', 'es')
'español'

>>> pyGoogleTranslate.detect_language('Nihao', 'english')
'Chinese'

>>> pyGoogleTranslate.transliterate('Ohayou')
'おはよう'

>>> pyGoogleTranslate.transliterate('おはよう')
'Ohayou'

>>> pyGoogleTranslate.definition('おやすみ')
{'word_type': 'Noun', 'definition': '仕事などを休むこと。休暇。', 'example': '「一週間―をいただく」'}

>>> pyGoogleTranslate.detect_language('このPythonモジュールをダウンロード頂き誠にありがとうございました。')
'Japanese'

>>> pyGoogleTranslate.browser_kill()

Notes

Data in google_translate_data.py are made by me except for the list of google translate domain names which I need to give credit to ssut in his project py-googletrans.

You can change the google translate domain with:

>>> from pyGoogleTranslate.internal.domain import google_translate_domain
>>> google_translate_domain('<the new google translate domain>')
'<the new domain is returned>'

You can export your caches file with:

>>> from pyGoogleTranslate.internal.caching import export_cache
>>> export_cache()

# The file is exported in the current directory

You can then import it with (for example after an update):

>>> from pyGoogleTranslate.internal.caching import import_cache
>>> import_cache('<the path to the cache file>')

# The file is imported and merged with the current cache file

You can try using pyppeteer instead of Selenium with:

>>> from pyGoogleTranslate.internal import pyGoogleTranslate_pyppeteer
>>> pyGoogleTranslate_pyppeteer.translate('Hello', 'japanese')
'こんにちは'

>>> pyGoogleTranslate_pyppeteer.detect_language('Hola', 'es')
'español'

>>> pyGoogleTranslate_pyppeteer.transliterate('Ohayou')
'おはよう'

>>> pyGoogleTranslate_pyppeteer.definition('おやすみ')
{'word_type': 'Noun', 'definition': '仕事などを休むこと。休暇。', 'example': '「一週間―をいただく」'}

You can try the new TTS Download function with:

>>> from pyGoogleTranslate.internal.project.alpha.text_to_speech import text_to_speech
>>> text_to_speech('Hello', 'en', '1')

# The tts file is saved in your current directory
# Note that you need to install googletrans for this.

Dependencies

This module has three python module dependency: Selenium, psutil and lifeeasy used to automate browsers which are downloaded when installing pyGoogleTranslate with PIP.

Google Translate belongs to Google LLC, fully owned by Alphabet Inc.

© Anime no Sekai - 2020

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyGoogleTranslate-3.6.5.tar.gz (246.4 kB view details)

Uploaded Source

Built Distribution

pyGoogleTranslate-3.6.5-py3-none-any.whl (250.8 kB view details)

Uploaded Python 3

File details

Details for the file pyGoogleTranslate-3.6.5.tar.gz.

File metadata

  • Download URL: pyGoogleTranslate-3.6.5.tar.gz
  • Upload date:
  • Size: 246.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1

File hashes

Hashes for pyGoogleTranslate-3.6.5.tar.gz
Algorithm Hash digest
SHA256 e74ee61a733afa30f236b034b216a11ae9823eca38d9ee2bb8d065de3985494b
MD5 878900d3fad11fdd7927026309f3eaa6
BLAKE2b-256 ef7ffe20ef724f908ffcc00d36551891033eb75bbb0f22c6ab0a2b0c1269b043

See more details on using hashes here.

File details

Details for the file pyGoogleTranslate-3.6.5-py3-none-any.whl.

File metadata

  • Download URL: pyGoogleTranslate-3.6.5-py3-none-any.whl
  • Upload date:
  • Size: 250.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.1

File hashes

Hashes for pyGoogleTranslate-3.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3ca5680b13ad47ef79760ce501d048b26966a8fe1fa71de5de403a0c6fc53201
MD5 fbd156f5aea55061b72acc776e6777ae
BLAKE2b-256 276e044e5329603daa16cf14638260902ccf239fae3cf3e79b82a4f67859e588

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page