0

I have an app where I am having a button on the top for page translation(english/spanish) & I hide them based on the page loaded. By default english loads, so the spanish button is on the top so the user can click on them, so the spanish version loads(we are hiding the spanish button and showing the english now).

The issue is when the user loads the page first time, then selects spanish, then goes away, then reloads the page, the spanish version comes off, but the button is spanish too. Technically it should be english. This is because the first time the page loads the value of the variable is undefined and it is defined as english and we show the spanish button. Is there anyway we can tweak this to display the buttons correctly? I also do not store anything in the cookie explicitly.

The code

function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
}

function pageLoadConditions() {

    var lang = getUrlParameter('lang'); 

    alert ("lang: " +lang);
    if(typeof lang=="undefined"){
        lang='en';
        alert ("lang 2: " +lang);
    }
    alert ("lang 3: " +lang);
    if(lang){
        if(lang==="es"){
            $('#lang_pref').val(lang);      
            $("#lang_ess").hide();
            $("#lang_enn").show();
        }else{
            $('#lang_pref').val(lang);      
            $("#lang_enn").hide();
            $("#lang_ess").show();          
        }
    }else{
        $('#lang_pref').val("en");      
        $("#lang_enn").hide();
        $("#lang_ess").show();
    }

}
    window.onload = pageLoadConditions; 

Thanks.

user115391
  • 185
  • 1
  • 8
  • 22
  • Have you considered just changing the text value of the button rather than show / hide? Have it listen to which page is loaded rather than the click itself. – M.Bush Dec 22 '15 at 16:50
  • Thanks, it is technically the same page. I am using Spring for the automatic translation. – user115391 Dec 22 '15 at 16:55
  • I think they key then is creating a variable or Boolean to determine if you're looking at English or Spanish, and have your button text listen to that Boolean. – M.Bush Dec 22 '15 at 17:01
  • Thanks again. The issue will be how to determine if the page load is english or spanish. I then need to rely on the same kind of logic(the one I posted above) and the first time of page load, it will be undefined and hence it will be english & so on. Is there any other way to determine that? Any pointers would be helpful. – user115391 Dec 23 '15 at 14:00

2 Answers2

0

Thanks Maulzal, I was able to get this working.

All I had to do was add if condition in the jsp as below

<c:if test="${pageContext.response.locale != &quot;es&quot;}">
    Spanish button
</c:if>
<c:if test="${pageContext.response.locale != &quot;en&quot;}">
    Eng button
</c:if>

I also commented out all the javascript functions related to this. Thanks.

user115391
  • 185
  • 1
  • 8
  • 22
0

What you're doing will certainly work, but it's not the "right way" to do internationalization. There is native support in JSPs for defining resource bundles which contain id->language specific string mapping. Once you get the string ids setup correctly in your JSP adding another language does not mean you need to go through your entire application and find all the strings and add another rung to your if-else ladder, but instead you just add another id->language mapping file.

However if you're only ever supporting two languages, then what you're doing will work OKay.

How to internationalize a Java web application?

Community
  • 1
  • 1
Jazzepi
  • 5,259
  • 11
  • 55
  • 81
  • 1
    I am actually using Spring Locale for all others, except this particular scenario where I need to display/hide the alternate lang buttons so the user can click. I tried in the javascript as I mentioned in the question, but it did not work in couple of instances, hence this solution. Thanks. – user115391 Dec 28 '15 at 02:30
  • np, just wanted to make sure you didn't go down a blind alley for no reason – Jazzepi Dec 28 '15 at 02:36