1、使用SpringMVC的步骤:
- 编写国际化的配置文件
- 使用ResourceBundleMessageSource管理国际化资源文件
- 在页面使用fmt:message取出国际化内容
2、使用SpringBoot的步骤:
-
编写国际化配置文件,抽取页面需要显示的国际化消息
创建步骤:
在resource文件夹下创建i18n文件夹,然后创建login_zh_CN.properties文件,创建好之后会自动变成国际化视图,出现“Resource Bundle ‘login’”,这时候可以右键“Resource Bundle ‘login’”进行创建,如下图:
然后点击“+”按钮,直接添加即可,如下图:
文件添加好了,进行添加属性对应的显示方式,如下图:
可以切换到Resource Bundle视图,然后添加在html中显示的内容,再依次添加它再不同语言中显示的值 -
SpringBoot自动配好了管理国际化资源文件的组件
private static final Resource[] NO_RESOURCES = {
};
@Bean
@ConfigurationProperties(prefix = "spring.messages")
public MessageSourceProperties messageSourceProperties() {
return new MessageSourceProperties();
}
@Bean
public MessageSource messageSource(MessageSourceProperties properties) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(properties.getBasename())) {
//设置国际化资源文件的基础名(去掉语言国家代码的)
messageSource.setBasenames(StringUtils
.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename