2013-04-30 58 views
3

尝试使用JSTL有一个国际化的应用程序,我有这样的:JSTL国际化FMT特性设置

<li><a href="admin/insertEmployee.jsp"><fmt:message key="new"/></a></li> 

但在浏览器,它并没有转化通讯员键“新”和显示???new???而不是定义的值属性文件作为HTML锚点(应该是'Novo',在pt_PT中)。

我有一个包下的下列文件:

  • messages.properties
  • messages_en_US.properties
  • messages_pt_Pt.properties。

试图定义的web.xml(PT_PT)内的默认语言环境,但仍然没有工作...

我需要定义一个<fmt:setLocale />
这是正确的URI:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

web.xml中

<context-param> 
    <param-name> 
     javax.servlet.jsp.jstl.fmt.localizationContext 
    </param-name> 
    <param-value>com.arthurportas-i18n.messages</param-value> 
</context-param> 
<context-param> 
    <param-name> 
     javax.servlet.jsp.jstl.fmt.fallbackLocale 
    </param-name> 
    <param-value>pt_PT</param-value> 
</context-param> 
<context-param> 
    <param-name> 
     javax.servlet.jsp.jstl.fmt.locale 
    </param-name> 
    <param-value>pt_PT</param-value> 
</context-param> 

回答

3

@ arthur-portas:唯一奇怪的是,通过点击链接选择英文翻译,需要双击!一次点击被忽略......这是一个浏览器缓存行为?

这是因为您在setBundle之后设置了语言环境。正确的顺序是:

<c:if test="${param['lang'] !=null}"> 
    <fmt:setLocale value="${param['lang']}" scope="session" /> 
</c:if> 
<fmt:setBundle basename="com.arthurportas.i18n.Messages"/> 
1

解决: 我在web.xml中定义的默认语言环境PT_PT

<context-param> 
    <param-name> 
     javax.servlet.jsp.jstl.fmt.locale 
    </param-name> 
    <param-value> 
     pt_PT 
    </param-value> 
</context-param> 

和内index.jsp,声明资源包和覆盖区域设置的代码(如果由url作为参数提供的话) ple-> /?LANG = EN_US)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 

<fmt:setBundle basename="com.arthurportas.i18n.Messages"/> 
<c:if test="${param['lang'] !=null}"> 
    <fmt:setLocale value="${param['lang']}" scope="session" /> 
</c:if> 

使用范围= “会话”,适用于所有JPS的。 呈现链接来更改语言(英语)

<a href="?lang=en_US"><img src="img/UKFlag_32_32.png"></img></a> 

和我包下com.arthurportas.i18n两个文件: Messages_pt_PT.properties和Messages_en_US。例如使用 里面jsp文件文本翻译:

<fmt:message key="employee"/> 

只有奇怪的现象是,通过点击链接选择英语翻译,需要双击!一次点击被忽略......这是一个浏览器缓存行为?