0

Found this script on JSFiddle that would switch between languages. The problem is. When I type in the text with <br> it displays the breaks.

How can I get it to actually break the text?

Also, how can I get it to choose a default language? So every time you reload the page it will be that language that is selected. Otherwise, nothing is displayed until you select the language manually.

Also, I would highly appreciate it, if you can recommend other such scripts that are better then this one.

function getLanguageResources(){
    var fr = new Array(); var en = new Array();

    fr['settings'] = "paramètres"; en['settings'] = "settings";
    fr['default_feed'] = "Flux par défaut"; en['default_feed'] = "Default feed";
    fr['hidden'] = "Masquer"; en['hidden'] = " Hidden";
    fr['save_settings'] = "Enregistrer les paramètres"; en['save_settings'] = "Save settings";

    var resources = new Array();
    resources['fr'] = fr;
    resources['en'] = en;

    return resources;
}

function changeLanguage(lang){
    var langResources = getLanguageResources()[lang];

    $("span[name='lbl']").each(function(i, elt){
        $(elt).text(langResources[$(elt).attr("caption")]);
    });    
}

$(document).ready(function() {
    $("input[name='radio-language']").click(function(){
        changeLanguage($(this).val());
    });
});


Thanks.

9
  • 1
    To break the text, you need to apply the string as HTML instead of text, so in changeLanguage() you need to do .html() instead of .text(). To set a default language, set the checked attribute on the appropriate input element. Commented Jun 13, 2014 at 0:50
  • 1
    Why are you using new Array() when your variables contain objects? Arrays have numeric index, objects have string keys. Commented Jun 13, 2014 at 0:52
  • Also, your code would be easier to read if you used object literals instead of setting each property separately. Commented Jun 13, 2014 at 0:52
  • @Barmar I didn't write the code. Please write your suggestion in the answers section. Commented Jun 13, 2014 at 0:54
  • @univerio I added checked. The radio appears checked. But doesn't actually apply the value. How can I solve this? Commented Jun 13, 2014 at 0:57

1 Answer 1

1

To break the text, apply your string as HTML instead of text:

$(elt).html(langResources[$(elt).attr("caption")]);

To set a default language, set the checked attribute on the appropriate radio button and simulate a click event in your $(document).ready callback:

$("#radioEnglish").attr("checked", true).trigger("click");
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you that worked... One more thing. I tried adding span with class en['sat'] = "<span class="">from 8:00am — to 7:00pm</span>"; and got an error. How can I fix that?
@Boris You need to escape your quotes inside your string. Otherwise JavaScript will parse it as two strings in a row: <span class= and >from 8:00am — to 7:00pm</span>. To escape a quote: "this is \"quoted\" inside the string".
So like this? <span class=\"bold\">
But. It doesn't work. This is what I have: en['fri'] = "Today, Friday open <br> <span class=\"bold\">from 8:00am — to 7:00pm</span>";
@Boris Doesn't work? What did you want it to do? What did it do instead?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.