所以我一直在学习以下教程的组合....
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 片段
<%@ 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” , 运气不好如果您需要更多文件或代码,请询问!
最佳答案
试试这个..
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" p:basename="messages" />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
关于java - Spring Framework 仅读取英文 (en) 语言环境文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17844125/