Commit 5ecfe004 authored by Quxl's avatar Quxl

修改国际化设置

parent 33bf10ca
...@@ -5,7 +5,6 @@ import java.util.Locale; ...@@ -5,7 +5,6 @@ import java.util.Locale;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
...@@ -23,7 +22,7 @@ import com.egolm.film.api.common.service.Messages; ...@@ -23,7 +22,7 @@ import com.egolm.film.api.common.service.Messages;
import com.egolm.film.api.member.service.MemberService; import com.egolm.film.api.member.service.MemberService;
import com.egolm.film.bean.Fc_member; import com.egolm.film.bean.Fc_member;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
import com.egolm.film.config.interceptor.LocaleSessionInterceptor; import com.egolm.film.config.interceptor.LocaleCookieInterceptor;
import com.egolm.film.util.Common; import com.egolm.film.util.Common;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -122,12 +121,10 @@ public class MemberOpenApiController { ...@@ -122,12 +121,10 @@ public class MemberOpenApiController {
@PostMapping("setLocale") @PostMapping("setLocale")
@ApiOperation("设置语言环境") @ApiOperation("设置语言环境")
@ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", required = true, name = "i18n_language", defaultValue="zh_CN")}) @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", required = true, name = "i18n_language", defaultValue="zh_CN")})
public Object setLocale(HttpSession session, HttpServletResponse response, String i18n_language) { public Object setLocale(HttpServletResponse response, String i18n_language) {
Locale locale = new Locale(i18n_language); Cookie cookie = new Cookie(LocaleCookieInterceptor.language, i18n_language);
session.setAttribute(LocaleSessionInterceptor.language, locale);
Cookie cookie = new Cookie(LocaleSessionInterceptor.language, i18n_language);
cookie.setHttpOnly(true); cookie.setHttpOnly(true);
cookie.setMaxAge(3600); cookie.setMaxAge(3600000);
cookie.setPath("/"); cookie.setPath("/");
response.addCookie(cookie); response.addCookie(cookie);
return Rjx.jsonOk(); return Rjx.jsonOk();
......
...@@ -19,7 +19,7 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; ...@@ -19,7 +19,7 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.egolm.film.config.interceptor.AdminLoginInterceptor; import com.egolm.film.config.interceptor.AdminLoginInterceptor;
import com.egolm.film.config.interceptor.LocaleSessionInterceptor; import com.egolm.film.config.interceptor.LocaleCookieInterceptor;
import com.egolm.film.config.interceptor.MemberLoginInterceptor; import com.egolm.film.config.interceptor.MemberLoginInterceptor;
import com.egolm.film.config.interceptor.UserLoginInterceptor; import com.egolm.film.config.interceptor.UserLoginInterceptor;
...@@ -36,7 +36,7 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter { ...@@ -36,7 +36,7 @@ public class WebMvcConfig extends WebMvcConfigurerAdapter {
private MemberLoginInterceptor memberLoginInterceptor; private MemberLoginInterceptor memberLoginInterceptor;
@Autowired @Autowired
private LocaleSessionInterceptor localeSessionInterceptor; private LocaleCookieInterceptor localeSessionInterceptor;
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
......
...@@ -2,27 +2,27 @@ package com.egolm.film.config.interceptor; ...@@ -2,27 +2,27 @@ package com.egolm.film.config.interceptor;
import java.util.Locale; import java.util.Locale;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Component @Component
public class LocaleSessionInterceptor extends HandlerInterceptorAdapter { public class LocaleCookieInterceptor extends HandlerInterceptorAdapter {
public static final String language = "i18n_language"; public static final String language = "i18n_language";
@Override @Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception { public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
HttpSession session = req.getSession(); Cookie[] cookies = req.getCookies();
Locale locale = (Locale)session.getAttribute(language); for(Cookie cookie : cookies) {
if(locale == null) { if(cookie.getName().equals(language)) {
locale = req.getLocale(); LocaleContextHolder.setLocale(new Locale(cookie.getValue()));
}
} }
LocaleContextHolder.setLocale(locale);
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment