Skip to content

Commit

Permalink
DeepL: add Pro option, update Readme, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Azin Zahraei committed May 21, 2021
1 parent 055ebc4 commit 4f7b9ea
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ Use a different translation provider
In [1]: from translate import Translator
In [2]: to_lang = 'zh'
In [3]: secret = '<your secret from Microsoft or DeepL>'
In [4]: translator = Translator(provider='microsoft', to_lang=to_lang, secret_access_key=secret)
In [4]: translator = Translator(provider='<the name of the provider, eg. microsoft or deepl>', to_lang=to_lang, secret_access_key=secret)
In [5]: translator.translate('the book is on the table')
Out [5]: '碗是在桌子上。'
The DeepL Provider
~~~~~~~~~~~~~~~~~~
To use DeepL's pro API, pass an additional parameter called pro to the Translator object and set it to True and use your pro authentication key as the secret_access_key

.. code-block:: python
In [4]: translator = Translator(provider='microsoft', to_lang=to_lang, secret_access_key=secret, pro=True)
Documentation
-------------
Expand Down
13 changes: 13 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# encoding: utf-8
from __future__ import unicode_literals
import unittest
try:
from unittest import mock
except Exception:
Expand All @@ -15,6 +16,12 @@
'-------------------------\n'
'Translated by: MyMemory\n'
)
deepl_response_template = (
'\nTranslation: {}\n'
'-------------------------\n'
'Translated by: Deepl\n'
)
deepl_secret = 'secret'


@vcr.use_cassette
Expand Down Expand Up @@ -46,3 +53,9 @@ def test_main_from_language(cli_runner):
def test_main_get_vesion(cli_runner):
result = cli_runner.invoke(main, ['--version'])
assert 'translate, version 0.0.0\n' == result.output


@unittest.skipIf(deepl_secret == 'secret', 'No authentication token provided for DeepL')
def test_main_deepl_provider(cli_runner):
result = cli_runner.invoke(main, ['--provider', 'deepl', '--from', 'de', '--to', 'zh', '--secret_access_key', deepl_secret, 'the book is on the table'])
assert deepl_response_template.format('书在桌上') == result.output
10 changes: 9 additions & 1 deletion translate/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ def config_file(ctx, from_lang, to_lang, provider, secret_access_key):
help="Set to display the translation only.",
required=False,
)
@click.option(
'pro', '--pro',
default=False,
is_flag=True,
help="Set to use DeepL's pro API.",
required=False,
)
@click.argument('text', nargs=-1, type=click.STRING, required=True)
def main(from_lang, to_lang, provider, secret_access_key, output_only, text):
def main(from_lang, to_lang, provider, secret_access_key, output_only, pro, text):
"""
Python command line tool to make on line translations
Expand All @@ -155,6 +162,7 @@ def main(from_lang, to_lang, provider, secret_access_key, output_only, text):
kwargs = dict(from_lang=from_lang, to_lang=to_lang, provider=provider)
if provider != DEFAULT_PROVIDER:
kwargs['secret_access_key'] = secret_access_key
kwargs['pro'] = pro

translator = Translator(**kwargs)
translation = translator.translate(text)
Expand Down
1 change: 1 addition & 0 deletions translate/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, to_lang, from_lang='en', secret_access_key=None, region=None,
self.to_lang = to_lang
self.secret_access_key = secret_access_key
self.region = region
self.kwargs = kwargs

@abstractmethod
def get_translation(self, params):
Expand Down
11 changes: 10 additions & 1 deletion translate/providers/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ class DeeplProvider(BaseProvider):
Documentation: https://www.deepl.com/docs-api
'''
name = 'Deepl'
base_url = 'https://api-free.deepl.com/v2/translate'
base_free_url = 'https://api-free.deepl.com/v2/translate'
base_pro_url = 'https://api.deepl.com/v2/translate'

def __init__(self, **kwargs):
try:
super().__init__(**kwargs)
except TypeError:
super(DeeplProvider, self).__init__(**kwargs)
self.pro = self.kwargs.get('pro', False)
self.base_url = self.base_pro_url if self.pro else self.base_free_url

def _make_request(self, text):
params = {
Expand Down

0 comments on commit 4f7b9ea

Please sign in to comment.