Problem using Yandex translater API in Python

1.6k Views Asked by At

I've been asked to translate some words, and I'm using Python to do it. Yandex has an API that is supposed to be used withing Python, documentation here :

https://pypi.org/project/yandex-translater/1.0/

I followed the steps, but Always get the same error that seems to be withing the API, or maybe I'm not setting Something right in my code.

The code goes as follow :

from yandex import Translater

tr = Translater()
tr.set_key('my API key not given here')
tr.set_text("Hello World") 
tr.set_from_lang('en')
tr.set_to_lang('fr')

result = tr.translate()

print(result)

I then get this error :

File "C:\Users\BMQT\Desktop\Scraping\test.py", line 2, in <module>
tr = Translater()
File "C:\Program Files\Python37\lib\site-packages\yandex\Translater.py",     line 23, in __init__
self.default_ui = locale.getlocale()[0].split('_')[0]
AttributeError: 'NoneType' object has no attribute 'split'

A quick look if you need in the translater.py goes as follow for line 23 :

self.default_ui = locale.getlocale()[0].split('_')[0]

Is the API broken or am I wrong in my code? Thanks for the answers!

2

There are 2 best solutions below

0
On

I've used another api module called yandex_translate, and it works fine.

from yandex_translate import YandexTranslate
translate = YandexTranslate('mykey')
traduction =('Translate:', translate.translate('bonjour', 'fr-ar'))
print(traduction)

Don't know what was wrong with the previous one, maybe outdated.

0
On

translater object need to be created like this: tr = Translater.Translater()

from yandex import Translater

tr = Translater.Translater()
tr.set_key('my API key not given here')
tr.set_text("Hello World") 
tr.set_from_lang('en')
tr.set_to_lang('fr')

result = tr.translate()

print(result)