.
├── README.md
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── quick
│ │ └── source
│ │ ├── Application.java
│ │ ├── config
│ │ │ └── ResourceBundleConfig.java
│ │ └── controller
│ │ └── ApiController.java
│ └── resources
│ ├── application.properties
│ └── i18n
│ ├── messages.properties
│ ├── messages_en.properties
│ └── messages_zh.properties
└── test
└── java
留意下资源文件的路径,实在i18n下
localhost:8080/hello?lang=zh_CN
返回
操作成功
localhost:8080/hello?lang=en_US
返回
action success
- 两种配置方式,直接在properties中配置
spring.messages.basename=i18n/messages
spring.messages.encoding=UTF-8
- 或者自己创建Bean,在Bean中直接指定
@Bean
public ResourceBundleMessageSource messageSource() {
Locale.setDefault(Locale.CHINESE);
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasenames("i18n/messages");// name of the resource bundle
source.setUseCodeAsDefaultMessage(true);
source.setDefaultEncoding("UTF-8");
return source;
}
@Bean
public LocaleResolver localeResolver(){
final SessionLocaleResolver localeResolver = new SessionLocaleResolver();
//final CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(new Locale("zh", "CN"));
return localeResolver;
}
一定要注意这个bean的名字,是localeResolver
,也可以这样
@Bean(name="localeResolver")