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.
30 lines
921 B
30 lines
921 B
2 years ago
|
import argostranslate.package
|
||
|
import argostranslate.translate
|
||
2 years ago
|
import logging
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
2 years ago
|
|
||
|
#from_code = "de"
|
||
|
#to_code = "en"
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
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)
|
||
2 years ago
|
logger.info('Translated: \"{}\" -> \"{}\"'.format(text, translatedText))
|
||
2 years ago
|
return translatedText
|
||
|
# '¡Hola Mundo!'
|