coderifous / jquery-localize

a jQuery plugin that makes it easy to internationalize your web site.
465 stars 142 forks source link

Revert back to original translation #17

Closed edwardmp closed 12 years ago

edwardmp commented 12 years ago

Hi,

I've translated my site in English, however, I made a dropdown where people can select a language. The original language is Dutch in this case. How can I revert back to Dutch without reloading the page? Is the only option to create another translation in Dutch in a JSON file?

Thanks in advance!

coderifous commented 12 years ago

Hi Edward - There's no built-in support for "clearing out" applied translations and reverting to the default language (without a page reload). If many people needed this feature, I wouldn't be opposed to building it in. I think it would be done by storing the pre-localization values in memory and passing them to the normal default callback in order to restore the original state.

You might be able to achieve this without any mods to the plugin just by providing your own callback function and doing some version of what I described above.

edwardmp commented 12 years ago

Hi Jim,

Thanks for the reply. My jQuery/Javascript skills are not sufficient enough to provide my own callback function like that. It would be a good feature; I guess a lot of people want that too..

coderifous commented 12 years ago

Here's a simple approach. I didn't test it out, but the basic idea is that you store what the values were before localization, and then give your users a way to reapply that text to those elements. The example below only handles the simplest case. If you're localizing images srcs, titles, input elements, etc, you'll have to do more in the store/retrieve functions.

// First, store the pre-localization values for later use.
$("[data-localize]").each(function(){
  var preLocalizationText = $(this).text();
  $(this).data("prelocalization", preLocalizationText);
});

// Then do your normal localization
$("[data-localize]").localize("application");

// Then, anytime you want to revert to to the pre-localization values:
function returnToPreLocalization() {
  $("[data-localize]").each(function(){
    var preLocalizationText = $(this).data("prelocalization");
    $(this).text(preLocalizationText);
  });
}

// For example, when a button is clicked.
$("button#return-to-default-lang").click(returnToPreLocalization);
edwardmp commented 12 years ago

Thank you sir, it worked immediately. However, as you state, the few <a href=""> and html elements I have in my localization don't work.

You should definitely add this to your readme!

edwardmp commented 12 years ago

Please help?

elestedt commented 11 years ago

What you want to do is not possible. The original data is destroyed by the browser when it is replaced by the translation (yes, the view source function might show the original source, but that won't help).

If you can live with having to create a language file for the default language, this should work for you:

HTML:

<select id="languages">
  <option value="en">English</option>
  <option value="du">Duch</option>
</select>

JavaScript:

function onLanguageItemChange() {
  var lang = $("select#languages option:selected").val();
  $("[data-localize]").localize("domain", { language: lang });
}
$(document).ready(function() {
  $("select#languages").change(onLanguageItemChange).change();
}

I'm using that to some success. Hope it helps. Note: The language files will be cached in browser after initial load...

edwardmp commented 11 years ago

There must be a better way, but thanks for you suggestion. I find it too much work too make a language file for the default language. @coderifous solution works, but the html elements like are not included. I just need something that makes html elements work too.