提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.framework.config; |
X |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import org.springframework.context.annotation.Bean; |
|
5 |
import org.springframework.context.annotation.Configuration; |
|
6 |
import org.springframework.web.servlet.LocaleResolver; |
|
7 |
|
|
8 |
import javax.servlet.http.HttpServletRequest; |
|
9 |
import javax.servlet.http.HttpServletResponse; |
|
10 |
import java.util.Locale; |
|
11 |
|
|
12 |
/** |
|
13 |
* 国际化配置 |
|
14 |
* |
|
15 |
* @author Lion Li |
|
16 |
*/ |
|
17 |
@Configuration |
|
18 |
public class I18nConfig { |
|
19 |
|
|
20 |
@Bean |
|
21 |
public LocaleResolver localeResolver() { |
|
22 |
return new I18nLocaleResolver(); |
|
23 |
} |
|
24 |
|
|
25 |
/** |
|
26 |
* 获取请求头国际化信息 |
|
27 |
*/ |
|
28 |
static class I18nLocaleResolver implements LocaleResolver { |
|
29 |
|
|
30 |
@Override |
|
31 |
public Locale resolveLocale(HttpServletRequest httpServletRequest) { |
|
32 |
String language = httpServletRequest.getHeader("content-language"); |
|
33 |
Locale locale = Locale.getDefault(); |
|
34 |
if (StrUtil.isNotBlank(language)) { |
|
35 |
String[] split = language.split("_"); |
|
36 |
locale = new Locale(split[0], split[1]); |
|
37 |
} |
|
38 |
return locale; |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { |
|
43 |
|
|
44 |
} |
|
45 |
} |
|
46 |
} |