我按照本指南How to internationalize a Java web application?做了JSTL JSP i18N但是,我无法设置默认语言,我用过< c:设置>每次正文加载(onload)时设置默认语言,但是当我刷新页面时它会随机显示语言。
你能否告诉我默认语言的想法?
<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>
答案 0 :(得分:1)
JavaScript在客户端执行。 JSP标记在服务器端执行,可用于生成JavaScript。所以这是执行以下操作时会发生的事情:
<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"/>
}
在服务器端,语言会话属性设置为fr
,然后设置为en
,然后设置为es
。生成以下JavaScript并将其发送到浏览器,因为<c:set>
不生成任何内容。它只更改服务器端会话属性的值:
<script>
function setFR()
{
}
function setEN()
{
}
function setES()
{
}
这没有任何意义。
答案 1 :(得分:0)
您可以通过以下方式设置首选默认语言环境
<fmt:setLocale value=”en_US” />
而不是
<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
请确保您拥有相应的资源捆绑