Quick move from Google Translate API v1 to Bing Translate API

    So, the day before yesterday, Good Corporation still closed its free Translate API. The author, until the last moment, hoped that they would come to their senses and would not become like the others to do so. Therefore, it was not scratched and did nothing. But then the thunder struck. And in the CMS of one small 3-language project, a translator fell off. Not scary, but unpleasant - before the admin on the form for editing content 1 click translated all fields into 2 more languages. I wrote the translator by my grace, spent 20 minutes on it and did not take a dime for it. Nevertheless, the client calls, threatens with physical violence over his beloved hamster, asks, again threatens. I had to close the bash and look for alternatives.

    Under the cat is a list (4 options, no revelations / discoveries) of which the author found, as well as a recipe for a Javascript translation from Bing.


    Option 1, paid

    It seems like the simplest and quickest solution is to take the Translate API v2 from the same Corporation. But what confuses - there is no simple JS example. Again - a security issue - I will need to add an API key to js. Those. Anyone who has guessed to open the source code of the page can find the key there. And translate on my site at the expense of my client. Not too interesting. (I confess, I didn’t understand, everything is possible there. But I’ll remind you of the original message - Saturday evening, I’m too lazy to understand, I want to go read bash | beer | neighbor)

    Option 2, criminal hemorrhoids

    As suggested here , for some reason the translation works. And even gives answers in JSON format. And Google itself offers to use Translate Widget, which you can probably dissect and see where it sends requests and how to emulate it.
    Here the questions arise - how long does it take to process these options? Is there any certainty that tomorrow (in a week) the bench will not be closed? Will they be able to get JSONP from them, or will they have to bypass Same Origin Policy again?
    Are there many questions?

    Option 3, mythical

    He recalled the love of one Russian search engine to copy the services of D. Corporation. He tried to google the Yandex Translate API. I found only a mention of the existence of such a service and that it is poorly documented. Probably it is not documented in any way, since at the time of disconnecting the translator from D. Inc. all runet would have to be full of links to this service, examples of use and an excerpt from the documentation. Well, I won’t believe in life that I was the only one who didn’t give notice on time, there are probably more victims.
    This is just an assumption, so don’t kick much if you have information - welcome, links only appear in the comments.

    Option 4, treacherous

    In the search process, the API from Yandex was incredibly surprised! http://msdn.microsoft.com/en-us/library/dd877846.aspx They give us a freebie! working! JSONP-enabled translator! And who is so generous? Microsoft! Looking ahead, I’ll say that the translator really works. Truth to leave from Google to Microsoft is somehow ... Unnatural or what? But nevertheless ...
    I gave a link to an example of use. An example, to put it mildly, is cumbersome, but slightly documented. Yes, by the way, after a quick look at the code, it is clear that some kind of API key is required. But it’s easy enough to get it - go here , register and add the site. The interface is slightly buggy, but in general it turned out to add a site.
    Yes, by the way, the translator for me with the same key worked on another (local) domain, unlike, for example, Dobrokart. The code was quickly processed, using jQuery it turned out to be a little shorter and more readable. If you do not like jQuery, then either you yourself can solve a similar problem better and faster than me, or you still have to connect it to make this function work.
    /**
     * function for translating using Bing's Translator API
     * @param String text - text to be translated
     * @param String sl - source language code (f.e. 'ru')
     * @param String tl - target language code (f.e. 'ru')
     * @param Function success - function to be called when translate is complete 
     *        sucessfully (will be given to a jQuery's ajax success parameter)
     * @param Function complete - function to be called anyway (will be given to a 
     *        jQuery's ajax complete parameter)
     */
    function translate_bing(text, sl, tl, success, complete){
        if(typeof(complete) != 'function')
            complete = function(){};
        jQuery.ajax({
            url: 'http://api.bing.net/json.aspx?JsonCallback=?',
            dataType: 'jsonp',
            data: { 
                'AppId' : 'YOUR_API_KEY',
                'Query': text.substr(0, 5000),
                'Sources': 'Translation',
                'Version': '2.2',
                'Translation.SourceLanguage': sl,
                'Translation.TargetLanguage': tl,
                'JsonType':'callback'
            },
            success: function(response){
                success(response.SearchResponse.Translation.Results[0].TranslatedTerm || '');
            },
            complete: complete
        });
    } 

    I didn’t insert comments into the code, not so much as a judge. Yes, and the function is documented with certain violations of something there. But, in my opinion, something can be understood. And most importantly - it works! (If it doesn’t work for you - you are welcome to comment, preferably with a browser)

    Yes, and also - the quality of the translation compared to Google - sucks. At least the direction RU -> UA.
    If anyone has anything better, welcome to the comments.

    And in the end a little pink snotregrets - it seems time to stop loving Google. I understand that the guys have to start making money, so they can no longer remain kind and fluffy. It's a pity. It was somehow ... magical or what? Do you want a card - on. If you want a translator - on. How much do I owe? - Well, thank you for using our service. Damn, it's just a fairy tale. Over.

    That's all, I went to drink beer to my neighbor.
    UPD: oh yes, we use JSONP, i.e. we will only send GET, and of course, when translating a more or less long text (I didn’t understand it, but with 520 Cyrillic characters, it’s true) we say HTTP 414 (which in this particular case is called 404 for some reason)

    Also popular now: