Internationalization in Spring @MVC

Spring中i18n的组件

  1. org.springframework.context.MessageSource基于message code和Locale转换成相应的message
  2. org.springframework.web.servlet.LocaleResolver从session或者cookie里取的locale
  3. org.springframework.web.servlet.i18n.LocaleChangeInterceptor改变locale

MessageSource

MessageSource的实现

Spring在org.springframework.context.support包下提供了两个MessageSource的实现:
1. ResourceBundleMessageSource:使用JVM提供的ResourceBundle实现,只能在classpath里加载资源
2. ReloadableResourceBundleMessageSource:和ResourceBundleMessageSource非常类似,但是提供了自动重新加载缓存能力,以及可以从文件系统读取资源文件

配置MessageSource

配置MessageSource
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.apress.prospringmvc.bookstore.web.config;
import org.springframework.context.support.ReloadableResourceBundleMessageSource; // Other imports omitted
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.apress.prospringmvc.bookstore.web" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource;
        messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:/messages");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
} }
  • 配置一个name是messageSource的Bean
  • 该配置会从classpath加载所有的messages.properties和messages_[locale].properties文件

LocaleResolver

LocaleResolver用来判断使用什么Locale,Spring在org.springframework.web.servlet.i18n包里具有如下实现: * FixedLocaleResolver:固定Locale,不支持改变locale * SessionLocaleResolver * AcceptHeaderLocaleResolver:从Http头的accept里取的locale,一般和用户的操作系统一样,因此也不支持改变locale。是默认LocaleResolver * CookieLocaleResolver

LocaleChangeInterceptor

LocaleChangeInterceptor检查当前的request中是否有locale的参数,如果有,interceptor会使用配置的LocaleResolver去改变当前用户的locale。参数的名子(locale)是可配置的。

配置Spring的i18n

Spring的i18n配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.apress.prospringmvc.bookstore.web.config;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
// Other imports omitted
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.apress.prospringmvc.bookstore.web" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
    @Bean
    public HandlerInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor;
        localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
}
    @Bean
    public LocaleResolver localeResolver() {
        return new CookieLocaleResolver();
    }
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource;
        messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:/messages");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
} }

注意:
* 23行的代码localeChangeInterceptor.setParamName(“lang”)用来设置request中的参数名
* 一般把LocaleChangeInterceptor配置为第一个interceptor,这样即使出现什么错误,任然能够改变用户的language


本文是基于Pro Spring MVC - With Web Flow的笔记,非原创

Comments

Comments