|
Extensions
Extensions
jquery.translate.uiSimple 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.progressThis 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.0The translateLanguages function is currently included in the native extension was removed in v1.4.1, you can copy it from here. Native language names
$.translate.nativeLanguages = {
"sq":"Shqipe",
"ar":"العربية",
"bg":"Български",
"ca":"Català",
"zh":"中文",
"zh-CN":"简体中文",
"zh-TW":"繁體中文",
"nl":"Nederlands",
"en":"English",
...
...
};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);
},
|
Sign in to add a comment
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
<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.
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
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
Hi there,
Can i have a working sample on using progressbar extension?
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.
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.
Fixed in v1.3.7.
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.
@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+'>'); }Sir, I checked the link with 'ul','li','a'. I just want this to be done with 'select','option'. Please help me.
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
}) //end of DOM ready </script>@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
wow... nice project... but you know? I have some problems,
can I translate the <title> script with this translator?
tell me about this...
thanks
Of course: $("title").translate(...)