Python library for Yandex Translate API. Available for both Python 2 and Python 3
pip install pyyandextranslateapi
It's very simple to use. There is only one class: Translate
from Yandex import Translate
Initialize your Translate with your API key for Yandex.Translate. You can get it for free on https://translate.yandex.com/developers/keys.
t = Translate(api_key=API_KEY)
Just in case, you can change your key any moment:
t.set_key(OTHER_API_KEY)
t.get_langs()
# {'af': 'Afrikaans', 'am': 'Amharic', 'ar': 'Arabic', ...}
There is an optional parameter ui
here which is for language of definitions of language codes:
t.get_langs(ui='ru')
# {'af': 'Африкаанс', 'am': 'Амхарский', 'ar': 'Арабский', ...}
t.detect(text='Здравствуйте!')
# ru
There is an optional parameter hint
where you can pass list of hints, if you have suggestions about text language:
t.detect('Здравствуйте!', hint=['ru', 'tt'])
# ru
But it's not the list from where API will choose language! API can ingore your hints if he thinks that you are wrong:
t.detect('Hello!', ['ru', 'tt'])
# en
t.translate(text='Hello!', lang_to='ru')
# Здравствуйте!
There is an optional parameter lang_from
where you can pass language of original text:
t.translate('Hello!', 'ru', lang_from='en')
# Здравствуйте!
If something went wrong, you will face exceptions:
InvalidAPIKeyError
: Invalid API keyBlockedAPIKeyError
: Blocked API keyDailyLimitExceededError
: Exceeded the daily limit on the amount of translated textTextSizeExceededError
: Exceeded the maximum text sizeUntranslatableTextError
: The text cannot be translatedDirectionNotSupportedError
: The specified translation direction is not supportedYandexTranslateError
: Other kind of error
https://tech.yandex.com/translate/doc/dg/concepts/about-docpage/