我使用Spring的MessageSource
和SessionLocaleResolver
来区分区域语言变体。
我遇到的问题是我的本地开发环境和ci环境的行为有所不同:
new Locale("en")
- > messages_en.properties
(本地和ci)new Locale("en", "GB")
- > messages_en_GB.properties
(本地)new Locale("en", "GB")
- > messages_en.properties
(ci)中文版本的同一问题:
new Locale("zh")
- > messages_zh.properties
(本地和ci)new Locale("zh", "TW")
- > messages_zh_TW.properties
(本地)new Locale("zh", "TW")
- > messages_zh.properties
(ci)以小写字母书写区域并不能解决问题。
本地开发环境:
$ mvn --version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T21:39:06+02:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_144, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.4", arch: "x86_64", family: "mac"
ci环境:
$ mvn --version
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.16.13", arch: "amd64", family: "unix"
Spring的i18n配置方式如下:
@Configuration
public class I18nConfiguration {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasenames("i18n/messages"); // name of the resource bundle
source.setDefaultEncoding("UTF-8");
source.setUseCodeAsDefaultMessage(true);
return source;
}
}