Animenosekai / translate

A module grouping multiple translation APIs
GNU Affero General Public License v3.0
515 stars 60 forks source link

What translation is used by default #44

Closed mbassiouny33 closed 2 years ago

mbassiouny33 commented 2 years ago

Hi there,

Thanks a lot for this great tool.

It is not really an issue with the code itself, but more with the documentation...

This sentence is a bit ambiguous to me: "The translator lets you group and use multiple translators at the same time, to increase your chance on getting an answer"

so when you run something like

from translatepy import Translator
translator = Translator()
text = "bonjour"
print(translator.translate(text, "en"))

For example when you use from translatepy.translators.google import GoogleTranslate it is obvious here we're using google translate. But in the generic case with Translator What service is actually used in the end? does it simply try a sequence of services until the first one that responds? if so, in what order?

Thanks in advance for a clarification :)

ZhymabekRoman commented 2 years ago

does it simply try a sequence of services until the first one that responds? if so, in what order?

Yes, the Translator class tries to translate text by services list and order is specified in the services_list attribute of the Translator class. If an error occurs during translation by one of the services, the Translator class tries to translate the text by the other service. Default services_list attribute value:

services_list: List[BaseTranslator] = [
            GoogleTranslate,
            BingTranslate,
            YandexTranslate,
            ReversoTranslate,
            DeeplTranslate,
            LibreTranslate,
            TranslateComTranslate,
            MyMemoryTranslate
        ]

You can change this order by specifying services_list attribute during the initialisation of the Translator class:

from translatepy.translators import GoogleTranslate, DeeplTranslate
from translatepy import Translator

translator = Translator(services_list=[DeeplTranslate, GoogleTranslate]) # First of all it will be try translate using the service DeepL, but if there is an error it will try Google Translate. This is very useful in Production
text = "bonjour"
translate_result = translator.translate(text, "en")
print(f"Translate result: '{translate_result.result}', original text: '{text}', translated using service '{translate_result.service}'")
Animenosekai commented 2 years ago

I'd like to point out that if fast is set to True rather than trying the translators in the list order, it will run every translator simultaneously to get the fastest answer

mbassiouny33 commented 2 years ago

Thanks both for the clear explanation! I appreciate it!