views:

120

answers:

4

Hey ppl, i have the following scenary:

<div class="maindiv">
     <div class="msg">some text</div>
                  ...
     <div class="msg">some other text</div>
</div>

I'd like to take the text of each 'msg' class div and translate it using Google API.

I've made the following JS function:

function translateTimeline(){

var lang = $('#timelineLenguage').val();
var translation;
    $(".msg").each(
 function(i){
                 translation=google.language.translate($(this).text(), "", lang,
                 function(result) {
                    if (!result.error) {

                       $(this).text(result.translation);
                    }else{
                       alert('Cannot translate tweet. Try again later');
                    }
                 });

 });
return false;
};

which is not working..

This was all i could make with my limited JS/jQuery skills..

Thx in advance for your help and time :)

A: 

myabe try

$(this).html()

instead of

$(this).text()
MattC
text() is the appropriate method here. The google translater isn't interested in any html-tags.
Magnar
Already tried that one, same problem :( thanks for your input :)
mcabral
A: 

This might be because you have spelled the language wrong in the line:

lang = $('#timelineLenguage').val();

If this is not it, you need to provide more details on what is happening.

Technowise
Thanks for the answer. Yes, there was a typo there, but that wasnt it :(
mcabral
A: 

Are you sure that the "this" in the following line points to the right object? Doesn't it point to window?

$(this).text(result.translation);

Try this:

function translateTimeline(){

    var lang = $('#timelineLenguage').val();
    var translation;
    $(".msg").each(
    function(i){
        var me = this; /*CHANGED*/
        translation=google.language.translate($(this).text(), "", lang,
            function(result) {
                if (!result.error) {
                    $(me).text(result.translation); /*CHANGED*/
                }else{
                    alert('Cannot translate tweet. Try again later');
                }
            }
        );
    });
    return false;
};
Kaze no Koe
mcabral
Sorry it didn't work
Kaze no Koe
A: 

I've never played with the google.language APIs, and this was a fun exercise. Basically it's a tool to translate the .msg items into another language. I'm not sure how brittle this will be if the APIs change, but it seems to work pretty nice. Note that this does not preserve the original text, and so if you do multiple translates the translation quality "degrades." Have a look at how I did the translateWithGoogle() function and I think that will inform your own code. I streamlined it down as much as possible.

<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script type="text/javascript">

google.load("jquery", "1.3.2");
google.load("language", "1");

function translateWithGoogle(context, fromLang, toLang) {
    google.language.translate($(context).text(), fromLang, toLang,
      function(result) {
        if (!result.error) {
      $(context).text(result.translation);
        }
    });
}
function translateTimeline() {
    var fromLang = $('#currentLang').text();
    var toLang = $('#targetLang').val()
    $('.msg').each(function() {
     translateWithGoogle(this, fromLang, toLang);
    });
    $('#currentLang').text(toLang);
}

$(document).ready(function(){
    $('#targetLang').bind('change', function(){
     translateTimeline();
    });
});
</script>
</head>
<body>
<div class="maindiv">
    <div class="msg">some text</div>
    <div class="msg">some other text</div>
    <div class="msg">hello world</div>
    <div class="msg">my stack has overflowed!</div>
</div>
<hr />
<span id="currentLang">en</span>
<select id="targetLang">
    <option value="af">Afrikaans</option>
    <option value="sq">Albanian</option>
    <option value="ar">Arabic</option>
    <option value="be">Belarusian</option>
    <option value="bg">Bulgarian</option>
    <option value="ca">Catalan</option>
    <option value="zh-CN">Chinese</option>
    <option value="hr">Croatian</option>
    <option value="cs">Czech</option>
    <option value="da">Danish</option>
    <option value="nl">Dutch</option>
    <option value="en">English</option>
    <option value="et">Estonian</option>
    <option value="tl">Filipino</option>
    <option value="fi">Finnish</option>
    <option value="fr">French</option>
    <option value="gl">Galician</option>
    <option value="de">German</option>
    <option value="el">Greek</option>
    <option value="iw">Hebrew</option>
    <option value="hi">Hindi</option>
    <option value="hu">Hungarian</option>
    <option value="is">Icelandic</option>
    <option value="id">Indonesian</option>
    <option value="ga">Irish</option>
    <option value="it">Italian</option>
    <option value="ja">Japanese</option>
    <option value="ko">Korean</option>
    <option value="lv">Latvian</option>
    <option value="lt">Lithuanian</option>
    <option value="mk">Macedonian</option>
    <option value="ms">Malay</option>
    <option value="mt">Maltese</option>
    <option value="no">Norwegian</option>
    <option value="fa">Persian</option>
    <option value="pl">Polish</option>
    <option value="pt">Portuguese</option>
    <option value="ro">Romanian</option>
    <option value="ru">Russian</option>
    <option value="sr">Serbian</option>
    <option value="sk">Slovak</option>
    <option value="sl">Slovenian</option>
    <option value="es">Spanish</option>
    <option value="sw">Swahili</option>
    <option value="sv">Swedish</option>
    <option value="th">Thai</option>
    <option value="tr">Turkish</option>
    <option value="uk">Ukrainian</option>
    <option value="vi">Vietnamese</option>
    <option value="cy">Welsh</option>
    <option value="yi">Yiddish</option>
</select>

</body>
</html>
artlung
worked wonders! thx a lot! :)
mcabral
Like I said, it was a great exercise -- and I'm glad to help!
artlung