diff --git a/backend/src/main/java/io/metersphere/commons/constants/I18nConstants.java b/backend/src/main/java/io/metersphere/commons/constants/I18nConstants.java index 01a1bf13e8..8e4d5679be 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/I18nConstants.java +++ b/backend/src/main/java/io/metersphere/commons/constants/I18nConstants.java @@ -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"; - - } diff --git a/backend/src/main/java/io/metersphere/config/I18nConfig.java b/backend/src/main/java/io/metersphere/config/I18nConfig.java index be2fd6ab3f..16e27858d4 100644 --- a/backend/src/main/java/io/metersphere/config/I18nConfig.java +++ b/backend/src/main/java/io/metersphere/config/I18nConfig.java @@ -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(); + } } diff --git a/backend/src/main/java/io/metersphere/controller/I18nController.java b/backend/src/main/java/io/metersphere/controller/I18nController.java index 71c78dc3be..b154053bbb 100644 --- a/backend/src/main/java/io/metersphere/controller/I18nController.java +++ b/backend/src/main/java/io/metersphere/controller/I18nController.java @@ -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); + } + } } } diff --git a/backend/src/main/java/io/metersphere/controller/LoginController.java b/backend/src/main/java/io/metersphere/controller/LoginController.java index 5099706ff5..11da340770 100644 --- a/backend/src/main/java/io/metersphere/controller/LoginController.java +++ b/backend/src/main/java/io/metersphere/controller/LoginController.java @@ -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(""); } diff --git a/backend/src/main/java/io/metersphere/i18n/Translator.java b/backend/src/main/java/io/metersphere/i18n/Translator.java index 0e87901eca..307877886d 100644 --- a/backend/src/main/java/io/metersphere/i18n/Translator.java +++ b/backend/src/main/java/io/metersphere/i18n/Translator.java @@ -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); } diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index 9c68f46189..67eb018ce5 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -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; diff --git a/backend/src/main/java/io/metersphere/websocket/WebSocketServer.java b/backend/src/main/java/io/metersphere/websocket/WebSocketServer.java index d70e31815f..dcf92cadfa 100644 --- a/backend/src/main/java/io/metersphere/websocket/WebSocketServer.java +++ b/backend/src/main/java/io/metersphere/websocket/WebSocketServer.java @@ -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 diff --git a/backend/src/main/resources/i18n/en-US.json b/backend/src/main/resources/i18n/en-US.json new file mode 100644 index 0000000000..10d54c8101 --- /dev/null +++ b/backend/src/main/resources/i18n/en-US.json @@ -0,0 +1,3 @@ +{ + "error_lang_invalid": "Invalid language parameter" +} \ No newline at end of file diff --git a/backend/src/main/resources/i18n/zh-CN.json b/backend/src/main/resources/i18n/zh-CN.json index 4b7a0a4de8..cfa517a25c 100644 --- a/backend/src/main/resources/i18n/zh-CN.json +++ b/backend/src/main/resources/i18n/zh-CN.json @@ -1,3 +1,3 @@ { - "i18n_test": "测试" + "error_lang_invalid": "语言参数错误" } \ No newline at end of file diff --git a/frontend/src/performance/App.vue b/frontend/src/performance/App.vue index a228076247..6e7c912178 100644 --- a/frontend/src/performance/App.vue +++ b/frontend/src/performance/App.vue @@ -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"