107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
|
#!/usr/bin/env python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
from googletrans import Translator
|
||
|
from googletrans import LANGUAGES
|
||
|
from re import compile as re_compile
|
||
|
|
||
|
if "LibreTranslatePlus" not in globals():
|
||
|
class LibreTranslatePlus:pass
|
||
|
|
||
|
class Anonymous(LibreTranslatePlus.Abstracts.Drivers): # LibreTranslatePlus.Drivers.Googletrans
|
||
|
|
||
|
re_language_name_clean = re_compile(r'\s*\([^\(\)]+\)$')
|
||
|
|
||
|
def __init__(self, ltp, _input = None):
|
||
|
super().__init__(ltp, _input)
|
||
|
|
||
|
self.__languages = None
|
||
|
self.__error = 0
|
||
|
self.__translator = None
|
||
|
|
||
|
self.languages()
|
||
|
|
||
|
def languages(self, show_errors = None):
|
||
|
|
||
|
if self.__languages == None:
|
||
|
|
||
|
self.__languages = []
|
||
|
self.__error = 0
|
||
|
done = []
|
||
|
|
||
|
try:
|
||
|
for code, name in LANGUAGES.items():
|
||
|
(key, error) = self.ltp.i18n.get_key(code[0:2], False)
|
||
|
if not error and key != code and key not in done:
|
||
|
done.append(key)
|
||
|
self.__languages.append({
|
||
|
"code" : code[0:2],
|
||
|
"name" : LibreTranslatePlus.Drivers.Googletrans.re_language_name_clean.sub(r'', name),
|
||
|
"key" : key
|
||
|
})
|
||
|
except Exception as exception:
|
||
|
self.__error |= 1 << 0
|
||
|
self.exception(exception)
|
||
|
|
||
|
if not len(self.__languages):
|
||
|
self.__error |= 1 << 1
|
||
|
|
||
|
return (self.__languages, self.__error)
|
||
|
|
||
|
def get_code(self, language, show_errors = None):
|
||
|
|
||
|
error = (
|
||
|
1 << 0 if language == None else
|
||
|
1 << 1 if not isinstance(language, str) else
|
||
|
1 << 2 if not language else
|
||
|
0)
|
||
|
code = None
|
||
|
|
||
|
if not error:
|
||
|
|
||
|
found = False
|
||
|
|
||
|
for language_data in self.__languages:
|
||
|
for key in ("code", "name", "key"):
|
||
|
if language_data[key] == language:
|
||
|
found = True
|
||
|
code = language_data["code"]
|
||
|
|
||
|
if not found:
|
||
|
error |= 1 << 3
|
||
|
|
||
|
return (code, error)
|
||
|
|
||
|
def translate(self, text, _from, to, show_errors = None):
|
||
|
|
||
|
(_from, from_error) = self.get_code(_from)
|
||
|
(to, to_error) = self.get_code(to)
|
||
|
error = (
|
||
|
((
|
||
|
1 << 0 if text == None else
|
||
|
1 << 1 if not isinstance(text, str) else
|
||
|
# 1 << 2 if not text else
|
||
|
0) << 0) |
|
||
|
(from_error << 3) |
|
||
|
(to_error << 7) |
|
||
|
0) << 1
|
||
|
translated = None
|
||
|
|
||
|
if not error:
|
||
|
try:
|
||
|
if self.__translator == None:
|
||
|
self.__translator = Translator()
|
||
|
results = self.__translator.translate(text, to, _from)
|
||
|
translated = results.text
|
||
|
except Exception as exception:
|
||
|
error |= 1 << 0
|
||
|
self.exception(exception)
|
||
|
|
||
|
return (translated, error)
|
||
|
|
||
|
if not hasattr(LibreTranslatePlus, "Drivers"):
|
||
|
class Subanonymous:pass
|
||
|
LibreTranslatePlus.Drivers = Subanonymous
|
||
|
del globals()["Subanonymous"]
|
||
|
LibreTranslatePlus.Drivers.Googletrans = Anonymous
|
||
|
del globals()["Anonymous"]
|