Commit 5ecfe004 authored by Quxl's avatar Quxl

修改国际化设置

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