0

I did a JSTL JSP i18N following this guide How to internationalize a Java web application? however, I couldn't set the default language, I used < c : set> to set the default language each time the body load (onload) but it displayed randomly the languages when I refresh the page.

Could you please give me the idea for the default language?

<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
<fmtlang:setLocale value="${language}" />
<fmtlang:setBundle basename="com.example.i18n.text" var="lang" />
<script>
function setFR()
{
    <c:set var="language" scope="session" value="fr"/>
}
function setEN()
{
    <c:set var="language" scope="session" value="en"/>
}
function setES()
{
    <c:set var="language" scope="session" value="es"/>
}
</script>

<html lang="${language}">
<body onload="setFR()">
<div id="all">

    <img src="img/contentlogin.jpg"/>
    <div id="language">
        <form id="formLang" name="formlang">
        <button type="submit" style="border: 0; background: transparent" id="language"  name="language" value="fr" ${language == 'fr' ? 'selected' : ''}>
            <img src="icon/fr.png" width="32" height="32" alt="submit" />
        </button>
        <button type="submit" style="border: 0; background: transparent" id="language" name="language" value="en" ${language == 'en' ? 'selected' : ''}>
            <img src="icon/us.png" width="32" height="32" alt="submit" />
        </button>
        <button type="submit" style="border: 0; background: transparent" id="language"  name="language" value="es" ${language == 'es' ? 'selected' : ''}>
            <img src="icon/es.png" width="32" height="32" alt="submit" />
        </button>
    </form>
    </div>
    <form id="form1" name="form1" method="post" action="<%=appPath%>/loginAction">
    <div id="info">
        <span><fmtlang:message key="login.span.pleaseenterinfo" bundle="${lang}"/></span>
    </div>
    <div id="username_label">
        <span><fmtlang:message key="login.label.username" bundle="${lang}"/></span>
    </div>
    <div id="username">
            <input type="text" maxlength="15" STYLE="color: #3c7033; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #ebf7ca;" name="username" id="username" size="22">
    </div>

    <div id="password_label">
        <span><fmtlang:message key="login.label.password" bundle="${lang}"/></span>
    </div>
    <div id="password">
            <input type="password" STYLE="color: #3c7033; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #ebf7ca;" id ="password" name="password" size="22">
            <input type="hidden" id ="language" name="language" value=<c:out value="${language}"/> size="22">
    </div>
    <div id="forgetpassword">
        <span><fmtlang:message key="login.span.forget" bundle="${lang}"/></span>
    </div>

    <div id="forcea">
        <img src="img/forceacopyright.png"/>
    </div>

     <div id="buttonlogin">
        <button type="submit" style="border: 0; background: transparent" >
            <img src="img/login.png" alt="12" width="101" height="94" border="0" />
        </button>
    </div>

    <div id="wronguandp">
        <%if(message!=null){ %>
            <b><span style="color: red;"> <fmtlang:message key="login.wrong.display" bundle="${lang}"/></span></b>
        <%}%>

    </div>
    </form>
    </div>
</body>
Community
  • 1
  • 1
bluewonder
  • 767
  • 2
  • 10
  • 18
  • 1
    Can you show your code . – AllTooSir Jul 30 '13 at 14:32
  • Show us your code. Tell us what you expect it to do, and what it does instead. Your question doesn't make sense: you're confusing the JSTL, executed at server-side, with JavaScript, executed at client-side. – JB Nizet Jul 30 '13 at 14:33
  • You're completely misunderstanding the role of JSP and JavaScript. JSP is a HTML code generator and JavaScript is part of that HTML code. Rightclick page in browser and do *View Source*. What do you see? Right, no single line of JSP code! Just get rid of all that JS code. They don't do anything useful. – BalusC Jul 30 '13 at 14:40
  • I believe your problems come from the fact, that you want to implement common anti-pattern better known as language switcher. I don't think you really need this in the first place, setting the page Locale to request Locale makes much more sense. – Paweł Dyda Jul 30 '13 at 15:48

2 Answers2

1

JavaScript is executed at client side. JSP tags are executed at server-side, and can be used to generate JavaScript. So here's what happens when the following is executed:

<script>
function setFR()
{
    <c:set var="language" scope="session" value="fr"/>
}
function setEN()
{
    <c:set var="language" scope="session" value="en"/>
}
function setES()
{
    <c:set var="language" scope="session" value="es"/>
}

At server-side, the language session attribute is set to fr, then to en, then to es. The following JavaScript is generated and sent to the browser, because <c:set> doesn't generate anything. It only changes the value of a session attribute at server-side:

<script>
function setFR()
{

}
function setEN()
{

}
function setES()
{

}

This doesn't make any sense.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

You can set the preferred default locale by this way as follows

<fmt:setLocale value=”en_US” />

instead of

<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />

Please ensure that you have the corresponding resourcebundle

  • The reason I used the < c:set > and is to set new language each time I choose the specific language, so if I set following ur example I cannot change language anymore – bluewonder Jul 30 '13 at 15:08
  • I couldn't understand what are you exactly looking for? what do you mean by default language? Can you please explain! – junkiecoder Jul 31 '13 at 05:36
  • I meant the first language when the page is loaded – bluewonder Jul 31 '13 at 07:34