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 NewTranslator class and tests #267

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
1 change: 1 addition & 0 deletions deep_translator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from deep_translator.qcri import QcriTranslator
from deep_translator.tencent import TencentTranslator
from deep_translator.yandex import YandexTranslator
from .new_translator import NewTranslator

__author__ = """Nidhal Baccouri"""
__email__ = "[email protected]"
Expand Down
42 changes: 42 additions & 0 deletions deep_translator/new_translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from deep_translator.base import BaseTranslator

class NewTranslator(BaseTranslator):
"""
Implements a new translator using the example translation service.
"""
def __init__(self, source='auto', target='en'):
base_url = "https://api.example.com/translate" # Example URL
languages = {
'auto': 'auto',
'en': 'English',
# Add more language mappings if needed
}
super().__init__(base_url=base_url, languages=languages, source=source, target=target)

def translate(self, text):
"""
Translate the provided text.
:param text: Text to be translated
:return: Translated text
"""
response = self._make_request(text)
return self._parse_response(response)

def _make_request(self, text):
params = {
'q': text,
'source': self.source,
'target': self.target
}
# Mocking an API response for demonstration
response = {
'data': {
'translations': [{
'translatedText': f"Translated {text}"
}]
}
}
return response

def _parse_response(self, response):
return response['data']['translations'][0]['translatedText']
13 changes: 13 additions & 0 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import unittest
from deep_translator.new_translator import NewTranslator

class TestNewTranslator(unittest.TestCase):
def setUp(self):
self.translator = NewTranslator()

def test_translation(self):
result = self.translator.translate("Hello")
self.assertEqual(result, "Translated Hello")

if __name__ == '__main__':
unittest.main()