Translate Text with YANDEX

For the translation of a user interface I needed a quick way to translate smaller strings into several languages I do not speak.
So the obvious choice would be to call Google to the rescue.
Sadly. the Google Translate API is not free, so I looked for alternatives and found on, of all places, in Russia!
YANDEX, the Russian version of Google, offers comparable services, only they are for free.
As I do not have any ideological issues here, I wrote a quick component that can make use of the YANDEX API for translations.
Interested?

The component is available compiled for V14, V15 and V16.

Installation

First, download the component here:

for V14 : AC_YandexTrans.4dbase

for V15 : AC_YandexTrans.4dbase

for V16 : AC_YayndexTrans.4dbase

The Demo in V14 : YANDEX Translate Demo (V14)

Just pop it into the components folder of your structure, add the excellent cUrl plugin by Keisuke (although we only need it to URL-escape the query string) and you are ready to go.
Initialise the component with your personal YANDEX API Key, don’t have one? Go get it here.

Before flight

Before calling the actual translation command for the first time, you need to call the initialization routine:

$l_Error:=YANDEX_Init (YourApiKey{;sourceLangauge})

The init routine will verify your API key and then download the available translation modes as well as a list of all languages referenced.
If you do not pass a source language, the component will use your current debase localization.

If all went well the return value will be 0, otherwise one of the following errors may occur:

401 Invalid API key
402 Blocked API key

As to the languages you can pass, please refer to the list passed back by this little utility command:

C_OBJECT($o_Languages)
$o_Languages:=YANDEX_GetLanguages

It will return an object with this structure:

{
	"dirs": [
		"az-ru",
		"be-bg",
		....
		"uk-ro",
		"uk-ru",
		"uk-sr",
		"uk-tr"
	],
	"langs": {
		"af": "Afrikaans",
		"am": "Amharic",
		"ar": "Arabic",
		.....
		"uz": "Uzbek",
		"vi": "Vietnamese",
		"xh": "Xhosa",
		"yi": "Yiddish",
		"zh": "Chinese"
	}
}

Calling the Component

To quickly check if all works out, just call

YANDEX_Show

to display a dialogue in wich you can test if all works

_YANDEX_Show_1

In order to peek at the object with all information on the call, just click the Show Parameters button. If errors occurred during the call you will see them here in the status item.

_YANDEX_Show_2

Possible error codes are:

200 Operation completed successfully
401 Invalid API key
402 Blocked API key
404 Exceeded the daily limit on the amount of translated text
413 Exceeded the maximum text size
422 The text cannot be translated
501 The specified translation direction is not supported
901 API Key was not initialized
910 Source language not specified
911 Target language not specified
912 No text to translate
950 XANDEX component failed to initialize
998-999 YANDEX constants object not initialized or empty

In code you can call the component as follows:


C_OBJECT($o_Params)
OB SET($o_Params;"target";targetLanguageCode) //i.e. "en" or "fr"
OB SET($o_Params;"text";theTextToTranslate)
theTranslatedText:=YANDEX_Translate ($o_Params)

The parameter object will contain a bunch of useful information, amongst others error codes:

{
	"source": "en",
	"target": "fr",
	"text": "This is a translation test",
	"trans": "en-fr",
	"lng_source": "English",
	"lng_target": "French",
	"text_esc": "This%20is%20a%20translation%20test",
	"status": 200,
	"response": {
		"code": 200,
		"lang": "en-fr",
		"text": [
			"C'est un test de traduction"
		]
	}
}

I expect most of this to be self explanatory.

Well folks,
that’s it for today. Hope it helps someone and I would love feedback.

PS: If you do need the source, make me an offer I cannot resist 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.