Spring MVC + Velocity实现国际化配置

本示例已升级到Spring Boot 2.x

源码地址:https://github.com/smltq/spring-boot-demo/tree/master/template-thymeleaf

国际化介绍

web开发中,国际化是需要考虑的一个问题,而且这个问题一般是越早敲定越好(不然等到系统大了,翻译是个问题).下面是结合实际项目(Spring MVC+Velocity)对实现国际化的一些总结.github地址

Spring国际化

I18N:作为"国际化"的简称,其来源是英文单词internationalization的首末字符i和n,18为中间的字符数.

Spring做国际化的配置主要有3个关键点:

  1. ResourceBundleMessageSource:实现国际化资源的定义.
  2. LocaleResolver:实现本地化信息的解析.
  3. LocaleChangeInterceptor:实现本地化信息的监听(来实现url参数动态指定locale).

LocaleResolver

LocaleResolver是指用什么策略来检测请求是哪一种locale,Spring MVC提供了一下几种策略:

AcceptHeaderLocaleResolver

根据浏览器Http Header中的accept-language域判定浏览器的语言环境,可以通过HttpServletRequest.getLocale获得域的内容,但是无法调用LocaleResolver接口的setLocale设置locale.基于这个策略,在后面的demo中可以实现基于浏览器的国际化案例.

SessionLocaleResolver

根据用户本次会话过程中的语言设定决定语言种类,session级别的,在此session周期内可以修改语言种类,但是session失效后,语言设定失效.基于这个策略,在后面的demo中可以实现基于session的国际化案例.

CookiedLocaleResolver

根据Cookie判定用于的语言设定(Cookie中保存着用户前一次的语言设定参数).

FixedLocaleResolver

一直使用固定的Locale,改变locale是不支持的.
如果需要使用哪一种策略,只需要在DispatcherServlet制定的Spring配置文件中配置就行,DispatchServlet将在初始化的时候调用initLocaleResolver(context)方法去配置文件中找名字为localeResolver的bean,如果有就使用配置文件的,没有就使用默认的AcceptHeaderLocaleResovler
通过上面,了解了Spring实现国际化的相关概念,下面结合demo实例,看看Spring MVC是如何实现国际化的

  • 配置文件
    
    
        
        
    

    
        
    

    
    
        
    
    
  • demo里准备两份语言文件分别是messages_cn.properties和messages_en.properties,内容分别如下:
Hello=Hello
HelloWorld=Hello World
SpringMvcBootstrap=Spring MVC Bootstrap
Greetings=I deeply greet you!
Hello=你好,现在是中文版
HelloWorld=你好,现在是中文版
SpringMvcBootstrap=中文版头部
Greetings=中文版欢迎你
  • 前端界面通过使用spring针对不同view视图提供的标记处理国际化信息.velocity标记,比如demo中的hello.vm文件
#define($content)
    #springMessage("Hello")
#end
  • 最后运行结果会根据defaultLocale的配置显示英语版本或中文版本,显示效果如下:
    Spring MVC + Velocity实现国际化配置_第1张图片
    Spring MVC + Velocity实现国际化配置_第2张图片

  • 以上配置示例是基于Cookie的国际化实现,国际化根据实际需求,实现方式有很多比如:

    • 基于浏览器请求的国际化
    • 基于Session的国际化实现
    • 基于ULR请求的国际化实现

Velocity简单使用

  • pom.xml增加Velocity 依赖
        
        
            org.apache.velocity
            velocity
            1.7
        
        
            org.apache.velocity
            velocity-tools
            2.0
        
  • xml增加模板引擎配置
    
    
        
            /WEB-INF/velocity/velocity.properties
        
    

    
        
        
        
        
        
        
        
    
    
  • controller代码,hello方法会显示hello.vm内容,helloWorld方法显示hello-world.vm内容,入口是main.vm
@Controller
public class HelloWorldController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

    @RequestMapping(value = "/hello-world", method = RequestMethod.GET)
    public String helloWorld() {
        return "hello-world";
    }

    @RequestMapping(value = "/hello-redirect", method = RequestMethod.GET)
    public String helloRedirect() {
        return "redirect:/hello-world";
    }

}
  • 我们看看main.vm代码



    $!page_title
    
    


#parse('/layout/header.vm')
$!content
#parse('/layout/footer.vm')

SpringMVC和REST服务API的基本用法示例

  • pom.xml增加json和xml依赖
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.5.3
        
        
        
            javax.xml.bind
            jaxb-api
            2.0
        
  • controller代码
@Controller
public class UserServiceController {

    @RequestMapping(value = "/user/{name}/{surname}.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
    User getUserJson(@PathVariable String name, @PathVariable String surname) {
        User user = new User(name, surname);
        return user;
    }

    @RequestMapping(value = "/user/{name}/{surname}.xml", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
    public @ResponseBody
    User getUserXml(@PathVariable String name, @PathVariable String surname) {
        User user = new User(name, surname);
        return user;
    }
}

@XmlRootElement(name = "user")
public class User {

    private String name;

    private String surname;

    public User() {

    }

    public User(String name, String surname) {
        super();
        this.name = name;
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}
  • json数据结果

浏览器输入http://localhost:9080/testweb/user/tq/lin.json,结果显示如下:

{
    name: "tq",
    surname: "lin"
}
  • xml数据结果

浏览器输入http://localhost:9080/testweb/user/tq/lin.xml,结果显示如下:


    tq
    lin

你可能感兴趣的:(Spring MVC + Velocity实现国际化配置)