You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
921 B
29 lines
921 B
import argostranslate.package
|
|
import argostranslate.translate
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
#from_code = "de"
|
|
#to_code = "en"
|
|
|
|
|
|
def init(from_code, to_code):
|
|
# Download and install Argos Translate package
|
|
argostranslate.package.update_package_index()
|
|
available_packages = argostranslate.package.get_available_packages()
|
|
package_to_install = next(
|
|
filter(
|
|
lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
|
|
)
|
|
)
|
|
argostranslate.package.install_from_path(package_to_install.download())
|
|
|
|
def translate(text, from_code, to_code):
|
|
if (from_code is None) or (to_code is None):
|
|
return text
|
|
# Translate
|
|
translatedText = argostranslate.translate.translate(text, from_code, to_code)
|
|
logger.info('Translated: \"{}\" -> \"{}\"'.format(text, translatedText))
|
|
return translatedText
|
|
# '¡Hola Mundo!'
|
|
|