实战
- 通过application.properties改变国际化文件存储位置
spring.messages.basename=static/i18n/messages
-
新建国际化文件
比如messages_en_US.properties
,messages_zh_CN.properties
等文件,并且在static/i18n
文件夹中 -
通过URL传递的参数
lang
更改国际化
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String lang = request.getParameter("lang");
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(lang)) {
String[] split = lang.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
- 加入到Spring 容器中
@Bean
public LocaleResolver localeResolver() {
MyLocaleResolver myLocaleResolver = new MyLocaleResolver();
return myLocaleResolver;
}
网友评论