mirror of
https://github.com/dataease/dataease.git
synced 2026-05-20 19:48:18 +08:00
feat: 国际化
This commit is contained in:
@@ -2,6 +2,7 @@ package io.dataease.auth.api;
|
||||
|
||||
|
||||
import io.dataease.auth.api.dto.DynamicMenuDto;
|
||||
import io.dataease.controller.handler.annotation.I18n;
|
||||
import io.swagger.annotations.Api;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -16,6 +17,7 @@ public interface DynamicMenuApi {
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/menus")
|
||||
@I18n
|
||||
List<DynamicMenuDto> menus();
|
||||
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ package io.dataease.commons.constants;
|
||||
|
||||
public class I18nConstants {
|
||||
|
||||
public static final String LANG_COOKIE_NAME = "MS_USER_LANG";
|
||||
public static final String LANG_COOKIE_NAME = "DE_USER_LANG";
|
||||
}
|
||||
|
||||
@@ -27,8 +27,6 @@ public class I18nController {
|
||||
@Value("${run.mode:release}")
|
||||
private String runMode;
|
||||
|
||||
// @Resource
|
||||
// private UserService userService;
|
||||
|
||||
@GetMapping("lang/change/{lang}")
|
||||
public void changeLang(@PathVariable String lang, HttpServletRequest request, HttpServletResponse response) {
|
||||
@@ -38,14 +36,13 @@ public class I18nController {
|
||||
LogUtil.error("Invalid parameter: " + lang);
|
||||
DEException.throwException(Translator.get("error_lang_invalid"));
|
||||
}
|
||||
// userService.setLanguage(targetLang.getDesc());
|
||||
Cookie cookie = new Cookie(I18nConstants.LANG_COOKIE_NAME, targetLang.getDesc());
|
||||
cookie.setPath("/");
|
||||
cookie.setMaxAge(FOR_EVER);
|
||||
response.addCookie(cookie);
|
||||
//重新登录
|
||||
if ("release".equals(runMode)) {
|
||||
Cookie f2cCookie = new Cookie("MS_SESSION_ID", "deleteMe");
|
||||
Cookie f2cCookie = new Cookie("DE_SESSION_ID", "deleteMe");
|
||||
f2cCookie.setPath("/");
|
||||
f2cCookie.setMaxAge(0);
|
||||
response.addCookie(f2cCookie);
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
package io.dataease.controller.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.JavaBeanSerializer;
|
||||
import com.alibaba.fastjson.serializer.ObjectSerializer;
|
||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
||||
import com.google.gson.Gson;
|
||||
import io.dataease.commons.constants.I18nConstants;
|
||||
import io.dataease.controller.ResultHolder;
|
||||
import io.dataease.controller.handler.annotation.I18n;
|
||||
import io.dataease.controller.handler.annotation.NoResultHolder;
|
||||
import io.dataease.i18n.Translator;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -13,6 +22,10 @@ import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 统一处理返回结果集
|
||||
*/
|
||||
@@ -35,6 +48,12 @@ public class ResultResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||
return o;
|
||||
}
|
||||
|
||||
//if true, need to translate
|
||||
if (methodParameter.hasMethodAnnotation(I18n.class)) {
|
||||
I18n i18n = methodParameter.getMethodAnnotation(I18n.class);
|
||||
o = translate(o, i18n.value());
|
||||
}
|
||||
|
||||
if (!(o instanceof ResultHolder)) {
|
||||
if (o instanceof String) {
|
||||
return new Gson().toJson(ResultHolder.success(o));
|
||||
@@ -44,4 +63,11 @@ public class ResultResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
// i18n
|
||||
private Object translate(Object obj, String type) {
|
||||
return Translator.translateObject(obj);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,31 @@
|
||||
package io.dataease.i18n;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.JavaBeanSerializer;
|
||||
import com.alibaba.fastjson.serializer.ObjectSerializer;
|
||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
||||
import io.dataease.commons.utils.BeanUtils;
|
||||
import io.dataease.commons.utils.LogUtil;
|
||||
import io.dataease.exception.DataEaseException;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
public class Translator {
|
||||
private static MessageSource messageSource;
|
||||
private static final String JSON_SYMBOL = "\":";
|
||||
private static final HashSet<String> IGNORE_KEYS = new HashSet<>(Arrays.asList("id", "password", "passwd"));
|
||||
|
||||
|
||||
@Resource
|
||||
public void setMessageSource(MessageSource messageSource) {
|
||||
@@ -17,6 +36,117 @@ public class Translator {
|
||||
* 单Key翻译
|
||||
*/
|
||||
public static String get(String key) {
|
||||
return messageSource.getMessage(key, null, "Not Support Key: " + key, LocaleContextHolder.getLocale());
|
||||
System.out.println(LocaleContextHolder.getLocale());
|
||||
return messageSource.getMessage(key, null, key, LocaleContextHolder.getLocale());
|
||||
}
|
||||
|
||||
private static Object translateRawString(String key, String rawString) {
|
||||
if (StringUtils.isBlank(rawString)) {
|
||||
return rawString;
|
||||
}
|
||||
for (String ignoreKey : IGNORE_KEYS) {
|
||||
if (StringUtils.containsIgnoreCase(key, ignoreKey)) {
|
||||
return rawString;
|
||||
}
|
||||
}
|
||||
|
||||
if (key != null) {
|
||||
String desc = get(rawString);
|
||||
if (StringUtils.isNotBlank(desc)) {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
return rawString;
|
||||
}
|
||||
|
||||
public static Object translateObject(Object javaObject) {
|
||||
if (javaObject == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
if (javaObject instanceof String) {
|
||||
String rawString = javaObject.toString();
|
||||
if (StringUtils.contains(rawString, JSON_SYMBOL)) {
|
||||
try {
|
||||
Object jsonObject = JSON.parse(rawString);
|
||||
Object a = translateObject(jsonObject);
|
||||
return JSON.toJSONString(a);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error("Failed to translate object: " + rawString, e);
|
||||
e.printStackTrace();
|
||||
LogUtil.warn("Failed to translate object " + rawString + ". Error: " + ExceptionUtils.getStackTrace(e));
|
||||
return translateRawString(null, rawString);
|
||||
}
|
||||
|
||||
} else {
|
||||
return translateRawString(null, rawString);
|
||||
}
|
||||
}
|
||||
if (javaObject instanceof Map) {
|
||||
Map<Object, Object> map = (Map<Object, Object>) javaObject;
|
||||
for (Map.Entry<Object, Object> entry : map.entrySet()) {
|
||||
if (entry.getValue() != null) {
|
||||
if (entry.getValue() instanceof String) {
|
||||
if (StringUtils.contains(entry.getValue().toString(), JSON_SYMBOL)) {
|
||||
map.put(entry.getKey(), translateObject(entry.getValue()));
|
||||
} else {
|
||||
map.put(entry.getKey(), translateRawString(entry.getKey().toString(), entry.getValue().toString()));
|
||||
}
|
||||
} else {
|
||||
translateObject(entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (javaObject instanceof Collection) {
|
||||
Collection<Object> collection = (Collection<Object>) javaObject;
|
||||
for (Object item : collection) {
|
||||
translateObject(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (javaObject.getClass().isArray()) {
|
||||
for (int i = 0; i < Array.getLength(javaObject); ++i) {
|
||||
Object item = Array.get(javaObject, i);
|
||||
Array.set(javaObject, i, translateObject(item));
|
||||
}
|
||||
}
|
||||
|
||||
ObjectSerializer serializer = SerializeConfig.globalInstance.getObjectWriter(javaObject.getClass());
|
||||
if (serializer instanceof JavaBeanSerializer) {
|
||||
JavaBeanSerializer javaBeanSerializer = (JavaBeanSerializer) serializer;
|
||||
|
||||
try {
|
||||
Map<String, Object> values = javaBeanSerializer.getFieldValuesMap(javaObject);
|
||||
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
||||
if (entry.getValue() != null) {
|
||||
if (entry.getValue() instanceof String) {
|
||||
if (StringUtils.contains(entry.getValue().toString(), JSON_SYMBOL)) {
|
||||
BeanUtils.setFieldValueByName(javaObject, entry.getKey(), translateObject(entry.getValue()), String.class);
|
||||
} else {
|
||||
BeanUtils.setFieldValueByName(javaObject, entry.getKey(), translateRawString(entry.getKey(), entry.getValue().toString()), String.class);
|
||||
}
|
||||
} else {
|
||||
translateObject(entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
DataEaseException.throwException(e);
|
||||
}
|
||||
}
|
||||
|
||||
return javaObject;
|
||||
} catch (StackOverflowError stackOverflowError) {
|
||||
try {
|
||||
return JSON.parseObject(translateRawString(null, JSON.toJSONString(javaObject)).toString(), javaObject.getClass());
|
||||
} catch (Exception e) {
|
||||
LogUtil.error("Failed to translate object " + javaObject.toString(), e);
|
||||
return javaObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user