国际化文件的编写
messages.properties
init project
messages_en_US.properties
init project
messages_zh_CN.properties
页面非连接配置国际化只需要:
spring.messages.basename=i18n.login
1: 1.5X版本配置的方式
链接配置
编写类 实现LocaleResover
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class LocaleSetting implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
/**
* l=('en_US) 取得连接字符串
*/
String lstr = request.getParameter( "l" );
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(lstr)){
String[] split = lstr.split( "_" );
locale = new Locale(split[ 0 ],split[ 1 ]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
|
一个Spring Boot 只需要存在一个继承 WebMvcConfigurationSupport,所以都在这个类 配置
在下面这里配置
1
2
3
4
5
6
7
8
|
public class AppConfig extends WebMvcConfigurationSupport
/**
* 配置国际化
*/
@Bean
public LocaleResolver initLocale(){
return new LocaleSetting();
}
|
2: 2.x版本的配置方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* 拦截器映射
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
super .addInterceptors(registry);
}
/**
* 配置国际化
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
/**
* <a class="btn btn-sm" th:href="@{index.html(l='en-US')}" rel="external nofollow" >中文</a>
* <a class="btn btn-sm" th:href="@{index.html(l='zh-CN')}" rel="external nofollow" >English</a>
*/
lci.setParamName( "l" );
return lci;
}
|
|