Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Glosbe and Wikimedia translation APIs with tests #265

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added "
Empty file.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions deep_translator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from deep_translator.tencent import TencentTranslator
from deep_translator.yandex import YandexTranslator

from .glosbe import GlosbeTranslator
from .wikimedia import WikimediaTranslator

__author__ = """Nidhal Baccouri"""
__email__ = "[email protected]"
__version__ = "1.9.1"
Expand Down
20 changes: 20 additions & 0 deletions deep_translator/glosbe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# glosbe.py

import requests

class GlosbeTranslator:
def __init__(self, source='auto', target='en'):
self.source = source
self.target = target
self.base_url = 'https://api.glosbe.com/translate'

def translate(self, text):
params = {
'from': self.source,
'dest': self.target,
'phrase': text,
'format': 'json'
}
response = requests.get(self.base_url, params=params)
response.raise_for_status()
return response.json()['tuc'][0]['phrase']['text']
Empty file added deep_translator/test_glosbe.py
Empty file.
13 changes: 13 additions & 0 deletions deep_translator/test_wikimedia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# test_wikimedia.py

import unittest
from deep_translator import WikimediaTranslator

class TestWikimediaTranslator(unittest.TestCase):
def test_translate(self):
translator = WikimediaTranslator(source='en', target='fr')
result = translator.translate('hello')
self.assertEqual(result, 'Je vous salue .') # Adjust according to actual API response

if __name__ == '__main__':
unittest.main()
26 changes: 26 additions & 0 deletions deep_translator/wikimedia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# wikimedia.py

import requests

class WikimediaTranslator:
def __init__(self, source='en', target='fr'):
self.source = source
self.target = target
self.base_url = 'https://translate.wmcloud.org/api/translate'

def translate(self, text):
data = {
'format': 'text', # Assuming the format is 'text'
'content': text,
'source_language': self.source,
'target_language': self.target
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(self.base_url, json=data, headers=headers)
print("Request Data:", data) # Debugging line
print("Response Status Code:", response.status_code) # Debugging line
print("Response Text:", response.text) # Debugging line
response.raise_for_status()
return response.json()['translation']
22 changes: 22 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,28 @@ Here are some screenshots:
:height: 300
:alt: screenshot3

Glosbe Translator
=================

.. code-block:: python

from deep_translator import GlosbeTranslator

translated = GlosbeTranslator(source='en', target='es').translate("hello")
print(translated) # Should print the translation of "hello" in Spanish


Wikimedia Translator
=====================

.. code-block:: python

from deep_translator import WikimediaTranslator

translated = WikimediaTranslator(source='en', target='fr').translate("hello")
print(translated) # Should print the translation of "hello" in French


===========================
Website & Desktop app
===========================
Expand Down