SpringMVC---国际化

一:国际化

        1.在页面上根据浏览器语言设置情况对文本(不是内容),时间,数值进行本地化处理
        2.可以在Bean中获取国际资源化文件Locale信息
        3.可以通过超链接切换Locale,而不必依赖于浏览器的语言设置情况

二: 解决办法
        1.使用jstl的fmt标签
        2.在Bean中注入ResourceBundleMessageSource的实例,使用其对应的getMessage方法即可
        3.配合LocalReslover和LocaleChangeInterceptor

三:使用jstl的fmt标签实现

1.,创建资源化文件i18n_en_US.properties,i18n_zh_CN.properties,i18n.properties

i18n.user=User
i18n.password=Password
i18n.user=用户名
i18n.password=密码

 2.配置ResourceBundleMessageSource

<!--配置国际化资源文件  -->
	<bean id="messageSource"
	class="org.springframework.context.support.ResourceBundleMessageSource">
			<property name="basename" value="i18n"></property>
	</bean>
	
	<mvc:view-controller path="i18n" view-name="i18n"/>
	<mvc:view-controller path="i18n2" view-name="i18n2"/>

3.创建i18n.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	
	<fmt:message key="i18n.password"></fmt:message>
    <fmt:message key="i18n.user"></fmt:message>
	<br><br>
	
	<a href="i18n2">i18n2</a>

</body>
</html>

四:用ResourceBundleMessageSource实现

@Autowired
	private ResourceBundleMessageSource messageSource;
	
	@RequestMapping("/i18n")
	public String testI18n(Locale locale) {
		String val=messageSource.getMessage("i18n.user", null, locale);
		System.out.println(val);
		return "i18n";
		
	}

五:超链接切换Locale实现

1.配置

<!-- 配置 SessionLocaleResolver-->
	<bean id="localeResolver"
	class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
	
	<!-- 配置 LocaleChangeInterceptor-->
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
	</mvc:interceptors>
<a href="i18n?locale=zh_CN">i18n2</a>
	
	<br>
	<a href="i18n?locale=en_US">i18n2</a>

2.

 

发布了64 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39093474/article/details/103856998