SpringMVC学习笔记 | 在SpringMVC中的国际化

目录

 

一、国际化需要解决的问题

1、使用jstl的fmt标签来对文本进行本地化处理

2、在bean中获取国际化资源文件Locale对应的消息

3、通过超链接切换Locale


一、国际化需要解决的问题

  • 在页面上能够根据浏览器语言设置的情况对文本,时间,数值进行本地化处理
    解决:使用JSTL的fmt标签

  • 可以在bean中获取国际化资源文件Locale对应的消息
    解决:在bean中注入ResourceBundleMessageSource的实例,使用其对应的getMessage方法即可

  • 可以通过超链接切换Locale,而不再依赖于浏览器的语言设置情况
    配置LocalResolverLocaleChangeInterceptor

1、使用jstl的fmt标签来对文本进行本地化处理

我们先新建三个国际化资源文件
i18n.properties:

i18n.user=\u7528\u6237\u540d
i18n.password=\u5bc6\u7801

i18n_en_US.properties:

i18n.user=Username
i18n.password=Password

i18n_zh_CN.properties:

i18n.user=\u7528\u6237\u540d
i18n.password=\u5bc6\u7801

然后在配置文件中配置国际化

<bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>

然后在页面中使用fmt标签:
我们定义两个页面,i18n.jsp和i18n2.jsp,一个显示用户名一个显示用户密码,然后在配置文件中配置一个可以直接访问这两个页面的配置:

    <mvc:view-controller path="/i18n" view-name="i18n"/>
    <mvc:view-controller path="/i18n2" view-name="i18n2"/>

i18n.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <fmt:message key="i18n.user"/>
    <br><br>
    <a href="i18n2">i18n2</a>
</body>
</html>

i18n2.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <fmt:message key="i18n.password"/>
    <br><br>
    <a href="i18n">i18n</a>
</body>
</html>

通过浏览器访问:
当浏览器的语言首选项是中文时,显示如下:

 
13424350-7b1d7961cc604060.png
 

设置语言首选项是英语:

 
13424350-9214a02b9d801e18.png
 


然后页面的显示如下:

 
13424350-2ef34d4396e8868d.png
 

2、在bean中获取国际化资源文件Locale对应的消息

上述中我们已经在配置文件中配置了ResourceBundleMessageSource,现在我们编写一个方法:

package com.cerr.springmvc.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

@Controller
public class SpringMVCTest1 {
    @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";
    }
}

显示的效果跟第一种情况一样,并且还会在控制台中打印出来i18n.user的内容。

3、通过超链接切换Locale

SessionLocaleResolver与LocaleChangeInterceptor工作原理

 
13424350-f6ecc02df3d15a9e.png
 

步骤

  • 编写国际化资源文件
  • 在配置文件中配置SessionLocaleResolverLocaleChangeInterceptor
  • 发请求时传一个locale参数

示例:我们想编写一个通过超链接来切换中英文的Demo
我们先在配置文件里面配置如下:

    <!-- 配置SessionLocaleResolver -->
    <bean id="localeResolver"
          class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    </bean>

    <!-- 配置LocaleChangeInterceptor拦截器 -->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    </mvc:interceptors>

添加两个超链接

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <a href="i18n?locale=zh_CN">中文</a>
    <br><br>
    <a href="i18n?locale=en_US">英文</a>
</body>
</html>

这样就可以通过访问超链接来切换中英文了。

猜你喜欢

转载自blog.csdn.net/qq_14810195/article/details/103159529