一文搞定Spring Boot、SpringMVC使用MessageSource进行i18n国际化支持

版权声明:本文为博主原创文章,未经博主允许不得转载。更多请关注:http://blog.csdn.net/nthack5730 https://blog.csdn.net/nthack5730/article/details/82870368

一、国际化

1. 简述

国际化是什么,简单地说就是,在不修改内部代码的情况下,根据不同语言及地区显示相应的语言界面。

2. Spring官方解释

Spring中对国际化文件支持的基础接口是MessageSource
参照Spring对于MessageSource解释的官方文档:

Strategy interface for resolving messages, with support for the parameterization and internationalization of such messages.
Spring provides two out-of-the-box implementations for production:
ResourceBundleMessageSource, built on top of the standard ResourceBundle
ReloadableResourceBundleMessageSource, being able to reload message definitions without restarting the VM

意思为:

Spring提供了两种开箱即用的实现,一种是标准实现,一种是运行时可重新加载。



本文允许转载,转载本文时请加上本文链接:
https://blog.csdn.net/nthack5730/article/details/82870368
对于爬虫网站随意爬取以及转载不加原文链接的,本人保留追究法律责任的权力!



二、SpringBoot中使用MessageSource国际化

1. SpringBoot自动化配置国际化支持

Spring Boot已经对i18n国际化做了自动配置,自动配置类为:

org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration

使用MessageSource时只要@Autowired就行:

@Autowired
private MessageSource messageSource;

而Spring在启动的时候装备的实现类是:

org.springframework.context.support.ResourceBundleMessageSource

但是很多人会说:我用了Autowired为什么调用messageSource.getMessage(...)却返回空内容?
这是因为SpringBoot对国际化properties文件路径默认设定是在message文件夹下,找不到文件夹,所以就没有信息返回呗。
下面会进行如何自定义i18n国际化配置讲述。

2. 常见国际化支持配置参数解释

MessageSource国际化配置中有几个参数是常见的,在这里必须要说明下,因为知道遮几个参数才能理解下面的配置:

basename:默认的扫描的国际化文件名为messages,即在resources建立messages_xx.properties文件,可以通过逗号指定多个,如果不指定包名默认从classpath下寻找。
encoding:默认的编码为UTF-8,也可以改为GBK等等
cacheSeconds:加载国际化文件的缓存时间,单位为秒,默认为永久缓存。
fallbackToSystemLocale:当找不到当前语言的资源文件时,如果为true默认找当前系统的语言对应的资源文件如messages_zh_CN.properties,如果为false即加载系统默认的如messages.properties文件。

3. 自定义i18n国际化配置

很多时候我们要修改自动配置中的某些配置参数,例如message.properties文件所在的文件夹路径、properties文件的编码格式等等,可以用以下两种方式进行配置修改,选一个就好:

  1. 修改Bean,直接返回一个配置Bean
  2. 使用配置文件(比较推荐这种做法)

3.1 在application.yml配置文件中

在配置文件中加入下面的配置:(application.properties配置文件麻烦自行转换下)

spring: 
  messages:
    basename: i18n/messages
    encoding: UTF-8

3.2 用Bean进行代码配置

@Configuration下面加入Bean配置就好,下面是完整的类代码:

@Configuration
public class MessageSourceConfig {

    @Bean(name = "messageSource")
    public ResourceBundleMessageSource getMessageSource() throws Exception {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
        resourceBundleMessageSource.setBasenames("i18n/messages");
        return resourceBundleMessageSource;
    }
}

注意:如果返回值不是MessageSource类型就要在@Bean上面指定name参数,否则无法使用@Autowired进行自动装入,这个是SpringIOC的东西,大家可以去查找对应的资料。

4. 使用i18n国际化

4.1 写入国际化文件

根据上面的配置,在resouces文件夹下面加入message_xxx.properties文件:

  1. 【默认】messages.properties
  2. 【英文】messages_en_US.properties
  3. 【中文】messages_zh_CN.properties

在里面写入同样的字段:

hello=xxx

其中xxx在不同文件里面写不同的语句(你能分辨出来就行)

4.2 代码中使用MessageSource

在需要用到的类中@Autowired就可以使用了:

@Autowired
private MessageSource messageSource;

然后使用:

messageSource.getMessage(code, args, defaultMessage, locale);

就可以获得对应的信息内容。

4.3 MessageSource接口中的方法

MessageSource中有三个方法,分别是:

String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);

String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;

String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;

常用的是第一个(主要是其他两个会抛出NoSuchMessageException,不太方便),下面是第一个方法中的参数解释:

code:信息的键,properties中的key
args:系统运行时参数,可为空
defaultMessage:默认信息,可为空
locale:区域信息,我们可以通过java.util.Locale类下面的静态常量找到对应的值。如:简体中文就是zh_CN;英文就是en_US,详见java.util.Locale中的常量值。

测试用例:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.main.web-application-type=reactive")
public class TestMessageSource {
    private static final Logger logger = LoggerFactory.getLogger(TestMessageSource.class);

    @Autowired
    private MessageSource messageSource;

    @Test
    public void testGetMessage() {
        logger.info(messageSource.getMessage("hello", null, "", null));
    }
}

测试用例中使用了nulllocale参数,这里会输出messages.propertieshello的值,这个参数会在下面4.4节中说明原因。

4.4 指定和默认的国际化信息

上面4.3中说明了MessageSource接口的三个方法,我们最常用的是第一个不抛出异常的方法。其中有一个是locale参数,这个参数的作用就是指定语言环境
java.util.Locale类中定义了很多的语言环境,如:

...
static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
static public final Locale CHINA = SIMPLIFIED_CHINESE;
static public final Locale UK = createConstant("en", "GB");
static public final Locale US = createConstant("en", "US");
static public final Locale CANADA = createConstant("en", "CA");
...

MessageSource.getMessage(...)所有方法中,都有Locale参数,指定区域信息。
当调用时,要如果指定位置为:Locale.CHINA,这时ResourceBundelMessageSource会从messages_zh_CN.properties中寻找对应的键值。
当给出Locale参数值为null,空的区域信息;或者对应的properties文件中没有找到对应的键值对,那么ResourceBundelMessageSource默认会从messages.properties中寻找键,当都找不到的时候,会返回空字符串。

三、SpringMVC使用国际化

未完待续,会补上



最后:版权声明

本文允许转载,转载本文时请加上本文链接:
https://blog.csdn.net/nthack5730/article/details/82870368
对于爬虫网站随意爬取以及转载不加原文链接的,本人保留追究法律责任的权力!

猜你喜欢

转载自blog.csdn.net/nthack5730/article/details/82870368