Skip to content Skip to sidebar Skip to footer

Translation In JS?

Basically need to generate custom(some different then yes no) messeges(alert) in JS , how to deal with translation in it?

Solution 1:

Plese take a look at jQuery-i18n translation plugin

var my_dictionary = { 
    "some text"  : "a translation",
    "some more text"  : "another translation"
}
$.i18n.load(my_dictionary);


$('div#example').text($.i18n._('some text'));

And jQuery localisation

$.localise('js/greeting');
$('#greeting').val(greeting);
$('#languages').val($.localise.defaultLanguage);
$('#changeLocale').change(function() {
    var newLang = $(this).val();
    $.localise('js/greeting', {language: newLang, loadBase: true});
    $('#greeting').val(greeting);
    $('#languages').val(newLang);
});

Or, If you really want more translation than looking up data, try google translate api

google.language.translate("Hello world", "en", "es", function(result) {
  if (!result.error) {
    var container = document.getElementById("translation");
    container.innerHTML = result.translation;
  }
});

Solution 2:

Use this JQuery plugin http://www.openxrest.com/translatejs

1 - Include the "trn" class to the text you want to translate:

<span class="trn">text to translate</span>

2 - Define a dictionary:

var dict = {
  "text to translate": {
    pt: "texto para traduzir"
  },
  "Download plugin": {
    pt: "Descarregar plugin",
    en: "Download plugin"
  }
}

3 - Translate the entire page body:

var translator = $('body').translate({lang: "en", t: dict}); //use English

4 - Change to another language:

translator.lang("pt"); //change to Portuguese

Post a Comment for "Translation In JS?"