My favorites | Sign in
Project Logo
                
Search
for
Updated Oct 24, 2009 by balazs.endresz
Labels: Featured
Extensions  
Extensions

jquery.translate.ui

Simple extension to generate an element based on the translatable languages. You can try this here: http://docs.jquery.com/Main_Page by running it with Firebug ( http://getfirebug.com/ ).

$.getScript('http://jquery-translate.googlecode.com/files/jquery.translate-1.3.9.min.js', function(){ //when the script is loaded
  $.translate(function(){ //when the Google Language API is loaded

    $.translate.ui('select', 'option') //generate dropdown
      .change(function(){ //when selecting another language
        $('body').translate( 'english', $(this).val(), { //translate from english to the selected language
          not: '.option, #demo, #source, pre, .jq-translate-ui', //exclude these elements
          fromOriginal:true //always translate from english (even after the page has been translated)
        })
      })
      .val('English') //select English as default
      .appendTo('#jq-primaryNavigation'); //insert the dropdown to the page
    
    //insert Google's logo after the dropdown:
    $.translate.getBranding().appendTo('#jq-primaryNavigation');

  });
});

On other sites this will only work if jQuery was included before. Also, you have to append it to an existing element: .appendTo('h1') or .prependTo('body')

IMPORTANT!

The html structure of the dropdown changed in v1.4: now $(this).val() in the above code returns the exact language code, not the displayed text. You might have to change you're code accordingly!

Since v1.4, you can also use a config object as the only argument, for example these are the defaults:

$.translate.ui({
  tags: ["select", "option"],
  //a function that filters the languages:
  filter: $.translate.isTranslatable, //this can be an array of languages/language codes too
  //a function that returns the text to display based on the language code:
  label: $.translate.toNativeLanguage ||
         function(langCode, lang){
           return $.translate.capitalize(lang);
         },
  //whether to include the UNKNOWN:"" along with the languages:
  includeUnknown: false
}).prependTo('body');

jquery.translate.progress

This small extensions helps you to integrate jQuery UI's progressbar component.

$(function(){ //DOM ready
  $('body').translate('de',{
    each: function(){
      this.progress("#progressbar" /* , options */ )
    }
  })
});

Available options to initialize the progressbar here: http://docs.jquery.com/UI/Progressbar/progressbar#options The progressbar will be automatically updated as each element gets translated.

Without arguments it will return a number between 0 and 100, with which you can manually update the progressbar component or just update some field on the page.

Official jQuery UI demo: http://ui.jquery.com/demos/progressbar/


Extensions added in v1.4.0

The translateLanguages function is currently included in the native extension was removed in v1.4.1, you can copy it from here.

Native language names

paralell

example usage:

//you can generate the first argument with $.translate.getLanguages
translateLanguages: function(languages, callback){
  delete languages.UNKNOWN;
  var deferred = []; //instances to be executed later are collected here
  $.each(languages, function(l, lc){
    if( $.translate.nativeLanguages[lc] ) return;
      var deferredInstance = 
               $.translate.defer( $.translate.capitalize(l), "en", lc, 
                         function(tr){ $.translate.nativeLanguages[lc] = tr; });
      deferred.push( deferredInstance );
  });

  if(!deferred.length)
    callback();
  else
    $.translate.run(deferred, callback);
},

Comment by balazs.endresz, Nov 24, 2008

If you try the first example on http://docs.jquery.com you may notice that the last part of the page won't be translated, because there's an h2 element that contains this html:

<a href="/UI" title="UI">jQuery UI</a> Reference

and after the translation it becomes

<a href="/UI" title="UI">jquery UI-Referenz</a>.

I have no idea why this happens, but it will break the plugin as it stores the translation on the h2 element via $.data and after one translation it will search the inner element (the anchor tag) not the h2 and finds nothing as the translation was stored on it's parent.

Comment by balazs.endresz, Nov 27, 2008

Here's the bug report on the above issue, please give it a star: http://code.google.com/p/google-ajax-apis/issues/detail?id=149

Comment by balazs.endresz, May 13, 2009

I've just realized that Google has fixed this a while ago but oddly enough it has been replaced with a jQuery bug that caused almost the same error at exactly the same place on the page.

Now it's been fixed in the plugin: http://code.google.com/p/jquery-translate/issues/detail?id=10

Comment by fido68, May 21, 2009

Hi there,

Can i have a working sample on using progressbar extension?

Comment by balazs.endresz, May 21, 2009

Try it here by running the code below in Firebug:

$.getScript('http://jquery-translate.googlecode.com/files/jquery.translate-   *LATEST VERSION*   .min.js', function(){ 
  $.translate(function(){ 
    $.translate.ui('select', 'option') 
      .change(function(){ 
        $('body').translate( 'english', $(this).val(), {
          not: '.option, #demo, #source, pre, .jq-translate-ui', 
          fromOriginal:true,
          each: function(){
            this.progress("#progressbar");
          }
        })
      })
      .val('English')
      .appendTo('#dock');
    
    $.translate.getBranding().appendTo('#jq-primaryNavigation');

  });
});

If you switch back to English there's an error message, because the html structure changes during the translation (as described a few comments before). It's not so common at all but maybe I should make it fail silently, as there's nothing I can do with that. Anyway, it's unrelated to the progressbar.

Comment by balazs.endresz, May 21, 2009

Actually the problem here is slightly different: in the translation two spans with some text between them get merged into one span element. And there's another issue when the progressbar doesn't move if the translation is restored from $.data. I'm fixing these now.

Comment by balazs.endresz, May 21, 2009

Fixed in v1.3.7.

Comment by tataijal, May 26, 2009

I need to set a cookie to mantain the language through different pages and need to have different languages in a .ui('select','option') Live link: http://angelsinaction.tv/aia_tv.php Can you please figure this out? Thanx in advance.

Comment by balazs.endresz, May 26, 2009

@tataijal: Regarding the cookies check the example on the main page under the "links" section!

Using the UI extension with different languages is possible, but you have to override the $.translate.getLanguages function to do that, but it's not really a clean solution, and it will be addressed in the next major version. Now, I'd say you're better off with creating a new function by modifying the $.translate.ui function a little:

$.translate.myUI = function(a, b, c){
  var languages = $.translate.getLanguages(true, ["en", "de", "fr"]) //here you can filter the languages
  var str='', cs='', cl='';
  if(c){ cs='<'+c+'>'; cl='</'+c+'>'; }
  $.each( languages , function(l, lc){
    str+=('<'+b+'>'+cs+l.charAt(0)+l.substring(1).toLowerCase()+cl+'</'+b+'>');
  });
  return $('<'+a+' class="jq-translate-ui">'+str+'</'+a+'>');
}
Comment by tataijal, May 26, 2009

Sir, I checked the link with 'ul','li','a'. I just want this to be done with 'select','option'. Please help me.

Comment by tataijal, May 26, 2009

Sir, I'm also sending the code I tried with 'select','option': <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> <script src="http://jquery-translate.googlecode.com/files/jquery.translate-1.2.6.min.js"></script> <script src="http://stilbuero.de/jquery/cookie/jquery.cookie.pack.js"></script> <script> jQuery(function($){ //when DOM is ready

$('body').css('height','900px');
$.translate(function(){ //when the Google Language API is loaded
function translateTo( destLang ){ //this can be declared in the global scope too if you need it somewhere else
$('body').translate( 'english', destLang, { //translate from english to the selected language
not: '.jq-translate-ui', //by default the generated element has this className fromOriginal:true //always translate from english (even after the page has been translated)
});
}
//you can generate other controls as well, not just a dropdown: $.translate().ui('select','option')
.appendTo('body') //insert the element to the page .css('background-color','white') .find('a') .attr('href', '#') //to change the mouse pointer .change(function(){ //when selecting another language
translateTo( $(this).text() );
$.cookie('destLang', $(this).text() ); // set a cookie to remember the selected language
return false; //prevent default browser action
})
var destLang = $.cookie('destLang'); //get previously translated language
if( destLang ) //if it was set then
translateTo( destLang );
}); //end of Google Language API loaded
}) //end of DOM ready </script>

Comment by balazs.endresz, May 27, 2009

@tataijal: If you are using a dropdown then you have to use .val() instead of .text() and the .find('a').attr('href', '#') part is unnecessary too, here's a corrected version: http://jsbin.com/exewe/edit

Comment by Shidie.X, Jun 15, 2009

wow... nice project... but you know? I have some problems,

can I translate the <title> script with this translator?

tell me about this...

thanks

Comment by balazs.endresz, Jun 15, 2009

Of course: $("title").translate(...)


Sign in to add a comment
Hosted by Google Code