parent
f6d56d9b3f
commit
2e1b9f97b6
@ -0,0 +1,49 @@
|
||||
package com.xjs.config;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 自定义国际化解析器
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-02-09
|
||||
*/
|
||||
@Configuration
|
||||
public class LocaleResolverConfig implements LocaleResolver {
|
||||
@Override
|
||||
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
|
||||
// 获取页面手动切换传递的语言参数l
|
||||
String l = httpServletRequest.getParameter("l");
|
||||
// 获取请求头自动传递的语言参数Accept-Language
|
||||
String header = httpServletRequest.getHeader("Accept-Language");
|
||||
Locale locale = null;
|
||||
// 如果手动切换参数不为空,就根据手动参数进行语言切换,否则默认根据请求头信息切换
|
||||
if (!StringUtils.isEmpty(l)) {
|
||||
String[] split = l.split("_");
|
||||
locale = new Locale(split[0], split[1]);
|
||||
} else {
|
||||
// Accept-Language: en-US,en;q=0.9
|
||||
String[] splits = header.split(",");
|
||||
String[] split = splits[0].split("-");
|
||||
locale = new Locale(split[0], split[1]);
|
||||
}
|
||||
return locale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver(){
|
||||
return new LocaleResolverConfig();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.xjs.config;
|
||||
package com.xjs.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
@ -1,4 +1,4 @@
|
||||
package com.xjs.config;
|
||||
package com.xjs.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
@ -1,4 +1,4 @@
|
||||
package com.xjs.config;
|
||||
package com.xjs.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
@ -1,4 +1,4 @@
|
||||
package com.xjs.config;
|
||||
package com.xjs.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
Loading…
Reference in new issue