server i18n
This commit is contained in:
parent
cb6291bcb0
commit
bba185a6ae
|
@ -3,9 +3,4 @@ package io.metersphere.commons.constants;
|
|||
public class I18nConstants {
|
||||
|
||||
public static final String LANG_COOKIE_NAME = "MS_USER_LANG";
|
||||
|
||||
public static final String LOCAL = "local";
|
||||
public static final String CLUSTER = "cluster";
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package io.metersphere.config;
|
||||
|
||||
import io.metersphere.i18n.I18nManager;
|
||||
import io.metersphere.service.CommonBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -18,4 +19,10 @@ public class I18nConfig {
|
|||
dirs.add("i18n/");
|
||||
return new I18nManager(dirs);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CommonBeanFactory commonBeanFactory() {
|
||||
return new CommonBeanFactory();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import io.metersphere.commons.constants.I18nConstants;
|
|||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.i18n.Lang;
|
||||
import io.metersphere.i18n.Translator;
|
||||
import io.metersphere.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
|
@ -30,12 +32,12 @@ public class I18nController {
|
|||
private UserService userService;
|
||||
|
||||
@GetMapping("lang/change/{lang}")
|
||||
public void changeLang(@PathVariable String lang, HttpServletResponse response) {
|
||||
public void changeLang(@PathVariable String lang, HttpServletRequest request, HttpServletResponse response) {
|
||||
Lang targetLang = Lang.getLangWithoutDefault(lang);
|
||||
if (targetLang == null) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
|
||||
LogUtil.error("Invalid parameter: " + lang);
|
||||
MSException.throwException("ERROR_LANG_INVALID");
|
||||
MSException.throwException(Translator.get("error_lang_invalid"));
|
||||
}
|
||||
userService.setLanguage(targetLang.getDesc());
|
||||
Cookie cookie = new Cookie(I18nConstants.LANG_COOKIE_NAME, targetLang.getDesc());
|
||||
|
@ -44,10 +46,16 @@ public class I18nController {
|
|||
response.addCookie(cookie);
|
||||
//重新登录
|
||||
if ("release".equals(runMode)) {
|
||||
Cookie f2cCookie = new Cookie("MS_COOKIE_ID", "deleteMe");
|
||||
Cookie f2cCookie = new Cookie("MS_SESSION_ID", "deleteMe");
|
||||
f2cCookie.setPath("/");
|
||||
f2cCookie.setMaxAge(0);
|
||||
response.addCookie(f2cCookie);
|
||||
}
|
||||
//本地测试用
|
||||
if ("local".equals(runMode)) {
|
||||
if (request != null) {
|
||||
request.getSession(true).setAttribute(I18nConstants.LANG_COOKIE_NAME, lang);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package io.metersphere.controller;
|
||||
|
||||
import io.metersphere.controller.request.LoginRequest;
|
||||
import io.metersphere.i18n.Translator;
|
||||
import io.metersphere.user.SessionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.*;
|
||||
|
@ -15,7 +17,7 @@ public class LoginController {
|
|||
@GetMapping(value = "/isLogin")
|
||||
public ResultHolder isLogin() {
|
||||
if (SecurityUtils.getSubject().isAuthenticated()) {
|
||||
return ResultHolder.success("");
|
||||
return ResultHolder.success(Translator.getLangDes());
|
||||
}
|
||||
return ResultHolder.error("");
|
||||
}
|
||||
|
|
|
@ -83,7 +83,6 @@ public class Translator {
|
|||
String preferLang = Lang.zh_CN.getDesc();
|
||||
|
||||
try {
|
||||
|
||||
if (request != null) {
|
||||
Object sessionLang = request.getSession(true).getAttribute(I18nConstants.LANG_COOKIE_NAME);
|
||||
if (sessionLang != null && StringUtils.isNotBlank(sessionLang.toString())) {
|
||||
|
@ -111,7 +110,6 @@ public class Translator {
|
|||
} else {
|
||||
preferLang = getSystemParameterLanguage(preferLang);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LogUtil.error("Fail to getLang.", e);
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import io.metersphere.dto.OrganizationMemberDTO;
|
|||
import io.metersphere.dto.UserDTO;
|
||||
import io.metersphere.dto.UserRoleDTO;
|
||||
import io.metersphere.dto.UserRoleHelpDTO;
|
||||
import io.metersphere.i18n.Translator;
|
||||
import io.metersphere.user.SessionUser;
|
||||
import io.metersphere.user.SessionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
|
|
@ -13,21 +13,21 @@ public class WebSocketServer {
|
|||
@OnOpen
|
||||
public void onOpen(Session session) throws IOException {
|
||||
// Get session and WebSocket connection
|
||||
System.out.println("open: " + session.isOpen());
|
||||
System.out.println("open: " + SessionUtils.getUser());
|
||||
System.out.println("open: " + session.getUserProperties().get("user"));
|
||||
// System.out.println("open: " + session.isOpen());
|
||||
// System.out.println("open: " + SessionUtils.getUser());
|
||||
// System.out.println("open: " + session.getUserProperties().get("user"));
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message) throws IOException {
|
||||
// Handle new messages
|
||||
System.out.println(message);
|
||||
// System.out.println(message);
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session session) throws IOException {
|
||||
// WebSocket connection closes
|
||||
System.out.println("close: " + session.isOpen());
|
||||
// System.out.println("close: " + session.isOpen());
|
||||
}
|
||||
|
||||
@OnError
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"error_lang_invalid": "Invalid language parameter"
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
{
|
||||
"i18n_test": "测试"
|
||||
"error_lang_invalid": "语言参数错误"
|
||||
}
|
|
@ -47,6 +47,8 @@
|
|||
beforeCreate() {
|
||||
this.$get("/isLogin").then(response => {
|
||||
if (response.data.success) {
|
||||
window.console.log(response.data);
|
||||
this.$setLang(response.data.data);
|
||||
this.auth = true;
|
||||
} else {
|
||||
window.location.href = "/login"
|
||||
|
|
Loading…
Reference in New Issue