【发布时间】:2013-07-24 20:22:58
【问题描述】:
所以我一直在学习以下教程的混合......
http://viralpatel.net/blogs/spring-3-mvc-internationalization-i18n-localization-tutorial-example/
http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/
想法!
通过单击特定语言并相应地更新网页,使我们的网页可以显示多种语言
计划!
使用spring的LocaleChangeInterceptor、LocaleResolver、HandlerMapping和MessageSource,以及一些messages_xx.properties文件,我们根据指定的“lang”参数动态调用不同语言的消息。我们使用 例如,如果“lang”=en,我们将转到 messages_en.properties 文件并获取 label.message 的消息。
设置有问题!
我们已经在 applicationContext.xml 文件和 web.xml 文件中设置了所有的 spring “东西”(注意我是这个东西的业余爱好者)。我们还在我们的 .jsp 文件之一中添加了 。
问题!
当我们运行应用程序(使用 TomCat 服务器)时,它会在 messages_en.properties 文件中成功显示消息(在本例中为“hello”),但是,当我们尝试更改语言时,会出现相同的消息。
参考代码
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
示例:messages_en.properties
label.message = Hello
jsp sn-ps 用于显示消息/更改语言
<%@ include file="/include.jsp"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
..............
<a href="?lang=en">English</a>|<a href="?lang=ar">Arabic</a>
<h3>
<spring:message code="label.message" text="default text" />
</h3>
..............
注意事项
我在代码中输入了一行
${pageContext.response.locale},网页上的输出是“en_US”。即使更改了参数(例如,在 url 中会显示.....lang=ar的位置)它确实找到了 messages_en.properties 文件并从该文件中获取消息并将其打印到网页上,但仅访问该文件并仅显示相应的消息。
-
我们使用的是 spring v3.0.3(编辑:基于库文件,但在 xml 文件中显示为 2.5,所以我不知道),我们应该使用不同的版本或库吗?
我认为这是什么?
- 我的经验非常少,但我感觉语言环境变量实际上并没有被改变,那么我该如何有效地改变它呢?
谢谢大家!我非常感谢任何和所有的帮助!
编辑
我已经用
<mvc:interceptors>包围了“localeChangeInterceptor”,没有运气如果您需要更多文件或代码,请询问!
【问题讨论】:
-
我已经能够通过进入 Firefox 并使用它的语言环境选择器来完成这项工作。在浏览器中更改它,Spring 将从 HTTP 请求标头中获取它。
-
我试过了,还是不行:/
标签: java xml spring jsp locale