Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DedInc committed Dec 3, 2023
1 parent 8909e0f commit 3f41d4e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
53 changes: 43 additions & 10 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
<h1 align="center">Mintrans is a free API wrapper that utilizes Bing Translator for translation purposes.</h1>
# 🗺️ mintrans - A translation API wrapper 🗺️

<br>
mintrans is a free API wrapper that utilizes Bing, DeepL, and Google Translate for translation purposes. 🤖

<h1 align="center"> -How to use- </h1>
## 💡 How to use 💡

```python
from mintrans import BingTranslator
```python
from mintrans import BingTranslator, DeepLTranslator, GoogleTranslator
from mintrans import RateLimitException

text = 'Hello World!'
from_lang = 'en'
to_lang = 'de'
from_lang = 'en'
to_lang = 'fr'

# Bing Translator
bing_translator = BingTranslator()
bing_translation = bing_translator.translate(text, from_lang, to_lang)
print(bing_translation)

# DeepL Translator
deepl_translator = DeepLTranslator()
try:
deepl_translation = deepl_translator.translate(text, from_lang, to_lang)
print(deepl_translation)
except RateLimitException:
print('Limit of DeepL Translator reached!')

# Google Translator
google_translator = GoogleTranslator()
google_translation = google_translator.translate(text, from_lang, to_lang)
print(google_translation)
```

## 🔑 Features 🔑

- Bing, DeepL and Google translators for more translation options!
- Easy switching between translators.
- Supports 100+ languages across translators. 🌍

## 🏆 Examples 🏆

```python
text = 'This is a longer text to test the translation.'

bing_translation = bing_translator.translate(text, 'en', 'es')

deepl_translation = deepl_translator.translate(text, 'en', 'de')

translator = BingTranslator()
translation = translator.translate(text, from_lang, to_lang)
print(translation)
google_translation = google_translator.translate(text, 'en', 'fr')
```
2 changes: 1 addition & 1 deletion mintrans/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .mintrans import BingTranslator
from .mintrans import *
72 changes: 71 additions & 1 deletion mintrans/mintrans.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import re
import time
import random

class BingTranslator:
Expand Down Expand Up @@ -40,4 +41,73 @@ def translate(self, text, from_lang, to_lang):
response['errorMessage'] = f'1000 characters limit! You send {len(text)} characters.'
else:
return response[0]
return response
return response

class RateLimitException(Exception):
pass

class DeepLTranslator:
def __init__(self):
pass

def translate(self, text, from_lang, to_lang):
json = {
"jsonrpc": "2.0",
"method": "LMT_handle_jobs",
"params": {
"jobs": [{
"kind": "default",
"sentences": [{
"text": text,
"id": 1,
"prefix": ""
}],
"raw_en_context_before": [],
"raw_en_context_after": [],
"preferred_num_beams": 4
}],
"lang": {
"target_lang": to_lang.upper(),
"preference": {
"weight": {},
"default": "default"
},
"source_lang_computed": from_lang.upper()
},
"priority": 1,
"commonJobParams": {
"regionalVariant": "en-US",
"mode": "translate",
"textType": "plaintext",
"browserType": 1
},
"timestamp": round(time.time() * 1.5)
},
"id": random.randint(100000000, 9999999999)
}
r = requests.post('https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs', json=json)
try:
translated_text = r.json()['result']['translations'][-1]['beams'][-1]['sentences'][-1]['text']
return translated_text
except KeyError:
raise RateLimitException('Rate limit error!')

class GoogleTranslator:
def __init__(self):
pass

def translate(self, text, from_lang, to_lang):
url = 'https://translate.googleapis.com/translate_a/single'

params = {
'client': 'gtx',
'sl': 'auto',
'tl': to_lang,
'hl': from_lang,
'dt': ['t', 'bd'],
'dj': '1',
'source': 'popup5',
'q': text
}

return requests.get(url, params=params).json()
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

setup(
name="mintrans",
version="1.0.0",
version="1.0.1",
author="Maehdakvan",
author_email="visitanimation@google.com",
description="Mintrans is a free API wrapper that utilizes Bing Translator for translation purposes.",
description="Mintrans is a free API wrapper that utilizes Bing, DeepL, and Google Translate for translation purposes.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/DedInc/mintrans",
Expand All @@ -22,5 +22,5 @@
],
packages=find_packages(),
install_requires=['requests'],
python_requires='>=3.0'
python_requires='>=3.6'
)

0 comments on commit 3f41d4e

Please sign in to comment.