Phonetics Assistant

The fiddle that prints phonetic transcription for a given text. Click on a transcription to listen its pronunciation. Powered by putting Yandex Dictionary API and Google Translate API together. The Yandex API Key can be obtained at http://api.yandex.ru/dictionary/

by Andrey Cheptsov

HTML

Hello, world!
Develop with pleasure!

CSS

.word  {
    cursor: pointer;
}

JavaScript

function translate(text, sl, tl, success) {
    $.ajax({
        url: 'https://dictionary.yandex.net/api/v1/dicservice.json/lookup',
        dataType: 'jsonp',
        data: {
            key: 'dict.1.1.20140529T074913Z.e38dbea81d148538.6f3db42887e3685c11573236fa74642c446ba7af',
            text: text,
            lang: sl + '-' + tl
        },
        success: function (result) {
            success(result);
        },
        error: function (request, error, exception) {
            alert(error);
        }
    });
}

var words = $(document.body).text().trim().split(/([\.,-\/#!$%\^&\*;:{}=\-_`~()\s]+)/gi);
var transcriptions = new Array(words.length);
var count = 0;
$(words).each(function (index, word) {
    translate(word, 'en', 'ru', function (response) {
        transcriptions[index] = response.def ? (response.def[0] ? response.def[0].ts : word) : word;
        if (++count == words.length) {
            $(document.body).html('')
            $(transcriptions).each(function (index, transcription) {
                $('<span></span>').html(transcription.replace(/\n/g, '<br/>')).appendTo(document.body).addClass('word').attr({title: words[index]}).click(function () {
                    $('<iframe></iframe>').attr({
                        src: 'http://translate.google.com/translate_tts?q=' + words[index] + '&tl=en&sfsff=sfsf'
                    }).hide().appendTo(document.body);
                });
            });
        }
    });
});