def traductionChained(utt, languages): tr = Translater() tr.set_key( 'trnsl.1.1.20200203T095917Z.30bf7af5cf091999.513bc5df39c8c45fd3cc2baa0ee4c8af7669202a' ) tr.set_text(utt) originLang = tr.detect_lang() tr.set_from_lang(originLang) tr.set_to_lang(languages[0]) tr.set_text(utt) utt = tr.translate() for i, lan in enumerate(languages): tr.set_from_lang(lan) if i < len(languages) - 1: tr.set_to_lang(languages[i + 1]) else: tr.set_to_lang(originLang) utt = tr.translate() tr.set_text(utt) #print(utt) return utt
class Translate: def __init__(self): self.tin = Translater() self.tin.set_key("#your yandex id here") def translate(self, text, target): try: self.tin.set_to_lang(target) self.tin.set_text(text) self.tin.set_from_lang(self.tin.detect_lang()) self.r = self.tin.translate() return self.r except: print("Translate Failed.")
def detect_language(texte): tr = Translater() tr.set_key(parameters.yandex_api_key()) tr.set_text(texte) tr.set_hint("fr", "en") return tr.detect_lang()