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