Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
/ pyYandexTranslateAPI Public archive

[DEPRECATED] Python library for Yandex Translate API

License

Notifications You must be signed in to change notification settings

rtix/pyYandexTranslateAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyYandexTranslateAPI

Python library for Yandex Translate API. Available for both Python 2 and Python 3

Get pyYandexTranslateAPI

pip install pyyandextranslateapi

Usage

It's very simple to use. There is only one class: Translate

Import

from Yandex import Translate

Initialize

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)

Use it

Get list of supported languages

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': 'Арабский', ...}

Detect the language of text

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

Translate text

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')
# Здравствуйте!

Exceptions

If something went wrong, you will face exceptions:

  • InvalidAPIKeyError : Invalid API key
  • BlockedAPIKeyError : Blocked API key
  • DailyLimitExceededError : Exceeded the daily limit on the amount of translated text
  • TextSizeExceededError : Exceeded the maximum text size
  • UntranslatableTextError : The text cannot be translated
  • DirectionNotSupportedError : The specified translation direction is not supported
  • YandexTranslateError : Other kind of error

For more information

About API

https://tech.yandex.com/translate/doc/dg/concepts/about-docpage/

About this library

radif.tazetdinov@mail.ru