Commit c8b51cec authored by Quxl's avatar Quxl

设置语言环境到Cookie中

parent 10c993fb
......@@ -2,7 +2,9 @@ package com.egolm.film.api;
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;
......@@ -114,9 +116,14 @@ 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, String i18n_language) {
public Object setLocale(HttpSession session, HttpServletResponse response, String i18n_language) {
Locale locale = new Locale(i18n_language);
session.setAttribute(LocaleSessionInterceptor.I18N_LANGUAGE_SESSION, locale);
session.setAttribute(LocaleSessionInterceptor.language, locale);
Cookie cookie = new Cookie(LocaleSessionInterceptor.language, i18n_language);
cookie.setHttpOnly(true);
cookie.setMaxAge(3600);
cookie.setPath("/");
response.addCookie(cookie);
return Rjx.jsonOk();
}
......
......@@ -13,12 +13,12 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Component
public class LocaleSessionInterceptor extends HandlerInterceptorAdapter {
public static final String I18N_LANGUAGE_SESSION = "I18N_LANGUAGE_SESSION";
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(I18N_LANGUAGE_SESSION);
Locale locale = (Locale)session.getAttribute(language);
if(locale == null) {
locale = req.getLocale();
}
......
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