Skip to content

Commit f3bd689

Browse files
authored
Merge pull request #115 from justiamtgm/master
QoL fixes & Updated Wiki & Tried to implement new Yandex Provider
2 parents 085fb23 + 7350aab commit f3bd689

12 files changed

Lines changed: 232 additions & 12 deletions

File tree

.readthedocs.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Read the Docs configuration file
2+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
3+
4+
# Required
5+
version: 2
6+
7+
# Set the OS, Python version, and other tools you might need
8+
build:
9+
os: ubuntu-24.04
10+
tools:
11+
python: "3.13"
12+
13+
# Build documentation in the "docs/" directory with Sphinx
14+
sphinx:
15+
configuration: docs/source/conf.py
16+
17+
# Optionally, but recommended,
18+
# declare the Python requirements required to build your documentation
19+
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
20+
# python:
21+
# install:
22+
# - requirements: docs/requirements.txt
23+

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Translated MyMemory API, LibreTranslate, and DeepL's free and pro APIs
1313
Why Should I Use This?
1414
----------------------
1515

16-
The biggest reason to use translate is to make translations in a simple way without the need of bigger
16+
The biggest reason to use translate is to make translations in a simple way without the need of bigger
1717
effort and can be used as a translation tool like command line
1818

1919

docs/source/overview.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
Overview
22
========
33

4-
Translate is a simple but powerful translation tool written in python with with support for multiple translation providers. It can be used as python module or as command line tool
4+
Translate is a simple but powerful translation tool written in python with with support for multiple translation providers. By now we offer integration with Microsoft Translation API, Translated MyMemory API, LibreTranslate, and DeepL's free and pro APIs.
5+
6+
The biggest reason to use translate is to make translations in a simple way without the need of bigger effort and can be used as a translation tool like command line.
7+
8+
Features:
9+
-----
10+
11+
* Translate your output in real time
12+
* Do translation in your terminal using the command line
13+
* Support of many APIs
14+
* Open-source code on `GitHub <https://github.com/terryyin/translate-python>`_
15+
16+
17+
518

6-
TODO: Add more content

docs/source/tutorial.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,26 @@ or run the command line and follow the steps:
8282
Secret Access Key []:
8383
8484
85-
The country code, as far as I know, is following https://en.wikipedia.org/wiki/ISO_639-1.
85+
The country code is following https://en.wikipedia.org/wiki/ISO_639-1.
8686

8787
Use As A Python Module
8888
----------------------
89+
You can use translate as a Python module, imported in the code. Function could use following parameters:
8990

91+
::
92+
93+
Translator(from_lang="fr", to_lang="it", provider="deepl", secret_access_key="TOKEN", region="westeurope")
94+
95+
Options:
96+
from_lang Language, you are going to translate from. Could be any ISO 639-1 country code, the default value is "autodetect". (optional)
97+
to_lang Language, you are going to translate into. Could be any ISO 639-1 country code.
98+
provider API translator used to make translation, could be any of listed in providers.rst (optional)
99+
secret_access_key Secret token, used by some APIs. (optional)
100+
region API region, used by some APIs. (optional)
101+
102+
Any parameters could be not only strings, but any other variables with string Type.
103+
104+
This is an example of using Translate():
90105
::
91106

92107
In [1]: from translate import Translator

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ tox
1313
twine
1414
vcrpy
1515
wheel
16+
setuptools
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- '*/*'
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
User-Agent:
12+
- Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebit/535.19(KHTML, like
13+
Gecko) Chrome/18.0.1025.168 Safari/535.19
14+
method: GET
15+
uri: http://api.mymemory.translated.net/get?q=why&langpair=autodetect%7Cen
16+
response:
17+
body:
18+
string: "<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body>\r\n<center><h1>301
19+
Moved Permanently</h1></center>\r\n</body>\r\n</html>\r\n"
20+
headers:
21+
Connection:
22+
- keep-alive
23+
Content-Length:
24+
- '134'
25+
Content-Type:
26+
- text/html
27+
Date:
28+
- Fri, 31 Oct 2025 16:26:42 GMT
29+
Location:
30+
- https://api.mymemory.translated.net:443/get?q=why&langpair=autodetect%7Cen
31+
Server:
32+
- awselb/2.0
33+
status:
34+
code: 301
35+
message: Moved Permanently
36+
- request:
37+
body: null
38+
headers:
39+
Accept:
40+
- '*/*'
41+
Accept-Encoding:
42+
- gzip, deflate
43+
Connection:
44+
- keep-alive
45+
User-Agent:
46+
- Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebit/535.19(KHTML, like
47+
Gecko) Chrome/18.0.1025.168 Safari/535.19
48+
method: GET
49+
uri: https://api.mymemory.translated.net/get?q=why&langpair=autodetect%7Cen
50+
response:
51+
body:
52+
string: '{"responseData":{"translatedText":"PLEASE SELECT TWO DISTINCT LANGUAGES"},"quotaFinished":null,"mtLangSupported":null,"responseDetails":"PLEASE
53+
SELECT TWO DISTINCT LANGUAGES","responseStatus":"403","responderId":null,"exception_code":null,"matches":""}'
54+
headers:
55+
Access-Control-Allow-Origin:
56+
- '*'
57+
Cache-Control:
58+
- no-cache, no-store, max-age=0, must-revalidate
59+
Connection:
60+
- keep-alive
61+
Content-Length:
62+
- '252'
63+
Content-Type:
64+
- application/json
65+
Date:
66+
- Fri, 31 Oct 2025 16:26:43 GMT
67+
Expires:
68+
- Fri, 01 Jan 1990 00:00:00 GMT
69+
Pragma:
70+
- no-cache
71+
Server:
72+
- Apache/2.4.41 (Ubuntu)
73+
X-Backend-Content-Length:
74+
- '10'
75+
X-Embedded-Status:
76+
- '200'
77+
X-Frame-Options:
78+
- SAMEORIGIN
79+
X-XSS-Protection:
80+
- '0'
81+
status:
82+
code: 200
83+
message: OK
84+
version: 1

tests/test_provider.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
except Exception:
66
import mock
77

8-
from translate.providers import MyMemoryProvider, MicrosoftProvider
8+
from translate.providers import MyMemoryProvider, MicrosoftProvider, YandexProvider
99

1010

1111
def test_provider_mymemory_languages_attribute():
@@ -41,3 +41,21 @@ def raise_for_status(self):
4141
provider = MicrosoftProvider(to_lang='en', headers={}, secret_access_key='secret')
4242
provider._make_request('test')
4343
assert mock_requests_post.called
44+
45+
@mock.patch('requests.Session.post')
46+
def test_provider_yandex_make_request(mock_requests_post):
47+
class MockResponse:
48+
text = '"dummyjson"'
49+
def raise_for_status(self):
50+
return False
51+
52+
mock_requests_post.return_value = MockResponse()
53+
provider = YandexProvider(to_lang='en', secret_access_key='secret', folder_id='some_id')
54+
provider.get_translation('test')
55+
assert mock_requests_post.called
56+
57+
args, kwargs = mock_requests_post.call_args
58+
assert 'Authorization' in kwargs['headers']
59+
assert kwargs['json']['folderId'] == 'some_id'
60+
assert kwargs['json']['targetLanguageCode'] == 'en'
61+
assert kwargs['json']['texts'] == 'test'

translate/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
import locale
1010
import sys
1111

12-
from .translate import Translator
13-
from .version import __version__
12+
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
13+
from translate import Translator
14+
from translate.version import __version__
1415
from .constants import CONFIG_FILE_PATH, DEFAULT_PROVIDER, TRANSLATION_FROM_DEFAULT
1516

1617

@@ -149,7 +150,7 @@ def config_file(ctx, from_lang, to_lang, provider, secret_access_key):
149150
@click.argument('text', nargs=-1, type=click.STRING, required=True)
150151
def main(from_lang, to_lang, provider, secret_access_key, output_only, pro, text, region):
151152
"""
152-
Python command line tool to make on line translations
153+
Python command line tool to make online translations
153154
154155
\b
155156
Example:
@@ -170,6 +171,7 @@ def main(from_lang, to_lang, provider, secret_access_key, output_only, pro, text
170171
kwargs['secret_access_key'] = secret_access_key
171172
kwargs['pro'] = pro
172173
kwargs['region'] = region
174+
kwargs['folder_id'] = folder_id
173175

174176
translator = Translator(**kwargs)
175177
translation = translator.translate(text)

translate/providers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
from .microsoft import MicrosoftProvider # noqa
66
from .deepl import DeeplProvider # noqa
77
from .libre import LibreProvider
8+
from .yandex import YandexProvider
89

9-
__all__ = ['MyMemoryProvider', 'MicrosoftProvider', 'DeeplProvider', 'LibreProvider']
10+
__all__ = ['MyMemoryProvider', 'MicrosoftProvider', 'DeeplProvider', 'LibreProvider', 'YandexProvider']

translate/providers/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# encoding: utf-8
33

44
from abc import ABCMeta, abstractmethod
5+
import requests
56

67

78
class BaseProvider:
@@ -10,14 +11,15 @@ class BaseProvider:
1011
name = ''
1112
base_url = ''
1213

13-
def __init__(self, to_lang, from_lang='en', secret_access_key=None, region=None, **kwargs):
14+
def __init__(self, to_lang, from_lang='en', secret_access_key=None, region=None, folder_id=None, **kwargs):
1415
self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebit/535.19'
1516
'(KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19'}
1617
self.from_lang = from_lang
1718
self.to_lang = to_lang
1819
self.secret_access_key = secret_access_key
1920
self.region = region
2021
self.kwargs = kwargs
22+
self.session = requests.Session()
2123

2224
@abstractmethod
2325
def get_translation(self, params):

0 commit comments

Comments
 (0)