Skip to content

Commit

Permalink
added new translation provider - LibreTranslate (#55)
Browse files Browse the repository at this point in the history
* added new translation provider - LibreTranslate

* fix format
  • Loading branch information
ilyachch authored Nov 10, 2023
1 parent 6db74f7 commit 728ab1b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Currently supported services are:
- `Google`
- `Bing`
- `Deepl`
- 'LibreTranslate'

### Configuring with a Configuration File

Expand Down
2 changes: 2 additions & 0 deletions md_translate/translators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .bing import BingTranslateProvider
from .deepl import DeeplTranslateProvider
from .google import GoogleTranslateProvider
from .libretranslate import LibreTranslateTranslateProvider
from .yandex import YandexTranslateProvider


Expand All @@ -12,3 +13,4 @@ class Translator(enum.Enum):
google = GoogleTranslateProvider
bing = BingTranslateProvider
deepl = DeeplTranslateProvider
libretranslate = LibreTranslateTranslateProvider
52 changes: 52 additions & 0 deletions md_translate/translators/libretranslate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Any

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.remote.webelement import WebElement

from md_translate.exceptions import safe_run

from ._selenium_base import SeleniumBaseTranslator


class LibreTranslateTranslateProvider(SeleniumBaseTranslator):
HOST = 'https://libretranslate.com/'

def get_url(self) -> str:
params = {
'source': self.from_language,
'target': self.to_language,
}
return f'{self.HOST}?{self.build_params(params)}'

def get_input_element(self) -> WebElement:
return self._driver.find_element(by=self.WEBDRIVER_BY.ID, value='textarea1')

def get_output_element(self) -> WebElement:
return self._driver.find_element(by=self.WEBDRIVER_BY.ID, value='textarea2')

@safe_run(NoSuchElementException, default_return_value=False)
def check_for_translation(self) -> bool:
element = self.get_output_element()
return element.get_attribute('value') != ''

def accept_cookies(self) -> None:
return

@safe_run(NoSuchElementException, default_return_value=False)
def check_for_antispam(self) -> bool:
container = self._driver.find_element(by=self.WEBDRIVER_BY.ID, value='t_enter_captcha')
return container.is_displayed()

@staticmethod
def get_translated_data(output_element: WebElement) -> str:
return output_element.get_attribute('value')

def wait_for_page_load(self) -> None:
def wait_for(driver: Any) -> bool:
document_ready = driver.execute_script('return document.readyState') == 'complete'
controls_loaded = (
len(driver.find_elements(by=self.WEBDRIVER_BY.ID, value='textarea1')) > 0
)
return document_ready and controls_loaded

self.WEBDRIVER_WAIT(self._driver, self.PAGE_LOAD_TIMEOUT).until(wait_for)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "md_translate"
version = "3.0.5"
version = "3.1.0"
description = "CLI tool to translate markdown files"
authors = ["Ilya Chichak <[email protected]>"]
license = "MIT License"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
DeeplTranslateProvider,
GoogleTranslateProvider,
YandexTranslateProvider,
LibreTranslateTranslateProvider,
)


Expand All @@ -22,6 +23,7 @@ class TestTranslator:
(GoogleTranslateProvider, 'Hello world', 'Привет, мир'),
(BingTranslateProvider, 'Hello world', 'Всем привет'),
(DeeplTranslateProvider, 'Hello world', 'Здравствуй мир'),
(LibreTranslateTranslateProvider, 'Hello world', 'Привет мир'),
],
)
def test_translate(self, translator, source_text, expected):
Expand Down

0 comments on commit 728ab1b

Please sign in to comment.