多语言-nacos的配置分组保存
This commit is contained in:
@@ -32,50 +32,45 @@ public class I18NMessageSourceUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 将需要国际化的数据进行反序列化并进行修改
|
* 将需要国际化的数据进行反序列化并进行修改
|
||||||
|
*
|
||||||
* @param id 主键
|
* @param id 主键
|
||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static void deserialization(Object obj, String id, List<String> fieldNames){
|
public static void deserialization(Object obj, String id, List<String> fieldNames) {
|
||||||
MessageI18nNacosService messageI18nNacosService = SpringContextHolder.getBean(MessageI18nNacosService.class);
|
MessageI18nNacosService messageI18nNacosService = SpringContextHolder.getBean(MessageI18nNacosService.class);
|
||||||
ThreadPoolExecutor bizExecutor = SpringContextHolder.getBean(ThreadPoolExecutor.class);
|
ThreadPoolExecutor bizExecutor = SpringContextHolder.getBean(ThreadPoolExecutor.class);
|
||||||
Class<?> clazz = obj.getClass();
|
Class<?> clazz = obj.getClass();
|
||||||
for (Field field : clazz.getDeclaredFields()) {
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
String fieldName = field.getName();
|
String fieldName = field.getName();
|
||||||
if (fieldNames.contains(fieldName)){
|
if (fieldNames.contains(fieldName)) {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
String value = field.get(obj).toString();
|
String value = field.get(obj).toString();
|
||||||
JSONObject jsonObject = JSONObject.parseObject(value);
|
JSONObject jsonObject = JSONObject.parseObject(value);
|
||||||
|
|
||||||
String zhValue = jsonObject.getString(WebLocaleConfig.Chinese.getLanguage());
|
String zhValue = jsonObject.getString(WebLocaleConfig.Chinese.getLanguage());
|
||||||
if (StringUtils.isEmpty(zhValue)){
|
if (StringUtils.isEmpty(zhValue)) {
|
||||||
throw new ServiceException(BusiStatus.PARAMERROR);
|
throw new ServiceException(BusiStatus.PARAMERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
String i18nKey = clazz.getSimpleName() + "." + zhValue;
|
String i18nKey = clazz.getSimpleName() + "." + zhValue;
|
||||||
|
|
||||||
Set<String> keySet = jsonObject.keySet();
|
Set<String> keySet = jsonObject.keySet();
|
||||||
|
|
||||||
CountDownLatch cdl = new CountDownLatch(keySet.size());
|
CountDownLatch cdl = new CountDownLatch(keySet.size());
|
||||||
|
|
||||||
// 国际化数据
|
// 国际化数据
|
||||||
for (String key : keySet) {
|
for (String key : keySet) {
|
||||||
bizExecutor.execute(()->{
|
bizExecutor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
// 需要保存的值
|
// 需要保存的值
|
||||||
String i18nValue = jsonObject.getString(key);
|
String i18nValue = jsonObject.getString(key);
|
||||||
if (StringUtils.isNoneBlank(i18nValue)){
|
if (StringUtils.isNoneBlank(i18nValue)) {
|
||||||
messageI18nNacosService.saveOrUpdate(new Locale(key), i18nKey, i18nValue);
|
messageI18nNacosService.saveOrUpdate(new Locale(key), i18nKey, i18nValue);
|
||||||
}
|
}
|
||||||
} catch (Exception e){
|
} catch (Exception e) {
|
||||||
log.error("{}",e.getMessage(), e);
|
log.error("{}", e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
cdl.countDown();
|
cdl.countDown();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
boolean isComplete = cdl.await(5, TimeUnit.SECONDS);
|
||||||
cdl.await(5, TimeUnit.SECONDS);
|
log.info("I18NMessageSourceUtil deserialization isComplete : {}", isComplete);
|
||||||
|
|
||||||
field.set(obj, zhValue);
|
field.set(obj, zhValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package com.accompany.core.service;
|
package com.accompany.core.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.accompany.common.utils.StringUtils;
|
import com.accompany.common.utils.StringUtils;
|
||||||
import com.accompany.core.base.SpringContextHolder;
|
import com.accompany.core.base.SpringContextHolder;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
@@ -15,10 +16,7 @@ import java.io.StringReader;
|
|||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -63,11 +61,11 @@ public class MessageI18nNacosService {
|
|||||||
/**
|
/**
|
||||||
* 根据id获取sys_conf对象
|
* 根据id获取sys_conf对象
|
||||||
*
|
*
|
||||||
* @param key key值
|
* @param key key值
|
||||||
* @param messageId 消息id
|
* @param messageId 消息id
|
||||||
* @return ignore
|
* @return ignore
|
||||||
*/
|
*/
|
||||||
public String getMessageByKey(String key,String messageId) {
|
public String getMessageByKey(String key, String messageId) {
|
||||||
Properties props = null;
|
Properties props = null;
|
||||||
try {
|
try {
|
||||||
// 创建Nacos配置服务实例
|
// 创建Nacos配置服务实例
|
||||||
@@ -82,7 +80,7 @@ public class MessageI18nNacosService {
|
|||||||
props = new Properties();
|
props = new Properties();
|
||||||
props.load(new StringReader(content));
|
props.load(new StringReader(content));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("nacos读取异常【{}】",e.getMessage(),e);
|
log.error("nacos读取异常【{}】", e.getMessage(), e);
|
||||||
}
|
}
|
||||||
if (props != null) {
|
if (props != null) {
|
||||||
String result = props.getProperty(key);
|
String result = props.getProperty(key);
|
||||||
@@ -93,80 +91,101 @@ public class MessageI18nNacosService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取阿拉伯语国际化数据
|
* 获取阿拉伯语国际化数据
|
||||||
|
*
|
||||||
* @param key key
|
* @param key key
|
||||||
* @return ignore
|
* @return ignore
|
||||||
*/
|
*/
|
||||||
public String getMessageArByKey(String key) {
|
public String getMessageArByKey(String key) {
|
||||||
return getMessageByKey(key,MESSAGES_AR_CONF_DATA_ID);
|
return getMessageByKey(key, MESSAGES_AR_CONF_DATA_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取中文国际化数据
|
* 获取中文国际化数据
|
||||||
|
*
|
||||||
* @param key key
|
* @param key key
|
||||||
* @return ignore
|
* @return ignore
|
||||||
*/
|
*/
|
||||||
public String getMessageZhByKey(String key) {
|
public String getMessageZhByKey(String key) {
|
||||||
return getMessageByKey(key,MESSAGES_ZH_CONF_DATA_ID);
|
return getMessageByKey(key, MESSAGES_ZH_CONF_DATA_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改或者更新国际化数据
|
* 修改或者更新国际化数据
|
||||||
* @param key key值
|
*
|
||||||
* @param value 内容
|
* @param key key值
|
||||||
|
* @param value 内容
|
||||||
* @param messageId 消息id
|
* @param messageId 消息id
|
||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public boolean saveOrUpdateProperties(String key,String value,String messageId) {
|
public boolean saveOrUpdateProperties(String key, String value, String messageId) {
|
||||||
// 创建Nacos配置服务实例
|
// 创建Nacos配置服务实例
|
||||||
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
||||||
String content = configService.getConfig(messageId, GROUP, 5000);
|
String content = configService.getConfig(messageId, GROUP, 5000);
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.load(new StringReader(content));
|
properties.load(new StringReader(content));
|
||||||
|
|
||||||
// 修改属性值
|
// 修改属性值
|
||||||
properties.setProperty(key, value);
|
properties.setProperty(key, value);
|
||||||
|
|
||||||
String newContent = propertiesToString(properties);
|
String newContent = propertiesToString(properties);
|
||||||
|
|
||||||
// 更新配置
|
// 更新配置
|
||||||
return configService.publishConfig(
|
return configService.publishConfig(
|
||||||
messageId,
|
messageId,
|
||||||
GROUP,
|
GROUP,
|
||||||
newContent);
|
newContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将properties属性转换为字符串内容
|
* 将properties属性转换为字符串内容
|
||||||
|
*
|
||||||
* @param properties 属性文件
|
* @param properties 属性文件
|
||||||
* @return ignore
|
* @return ignore
|
||||||
* @throws IOException 异常
|
* @throws IOException 异常
|
||||||
*/
|
*/
|
||||||
private String propertiesToString(Properties properties) throws IOException {
|
private String propertiesToString(Properties properties) throws IOException {
|
||||||
|
Map<String, Properties> groupMap = new HashMap<>();
|
||||||
|
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
|
||||||
|
String key = entry.getKey().toString();
|
||||||
|
String value = entry.getValue().toString();
|
||||||
|
String className = key;
|
||||||
|
if (key.contains(StrUtil.DOT)) {
|
||||||
|
String[] keyArray = key.split("\\.");
|
||||||
|
className = keyArray[0];
|
||||||
|
}
|
||||||
|
Properties p = groupMap.get(className);
|
||||||
|
if (p == null) {
|
||||||
|
p = new Properties();
|
||||||
|
}
|
||||||
|
p.put(key, value);
|
||||||
|
groupMap.put(className, p);
|
||||||
|
}
|
||||||
try (Writer writer = new StringWriter()) {
|
try (Writer writer = new StringWriter()) {
|
||||||
properties.store(writer, "");
|
for (Properties p : groupMap.values()) {
|
||||||
|
p.store(writer, StrUtil.EMPTY);
|
||||||
|
}
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMessageFileName(String language){
|
public String getMessageFileName(String language) {
|
||||||
return String.format("messages_%s.properties", language);
|
return String.format("messages_%s.properties", language);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean saveOrUpdate(Locale locale, String key, String value){
|
public Boolean saveOrUpdate(Locale locale, String key, String value) {
|
||||||
return saveOrUpdateProperties(key,value,getMessageFileName(locale.getLanguage()));
|
return saveOrUpdateProperties(key, value, getMessageFileName(locale.getLanguage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将需要国际化的数据进行反序列化并进行修改
|
* 将需要国际化的数据进行反序列化并进行修改
|
||||||
|
*
|
||||||
* @param id 主键
|
* @param id 主键
|
||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static void deserialization(Object obj, String id, List<String> fieldNames){
|
public static void deserialization(Object obj, String id, List<String> fieldNames) {
|
||||||
MessageI18nNacosService messageI18NNacosService = SpringContextHolder.getBean(MessageI18nNacosService.class);
|
MessageI18nNacosService messageI18NNacosService = SpringContextHolder.getBean(MessageI18nNacosService.class);
|
||||||
ThreadPoolExecutor bizExecutor = SpringContextHolder.getBean(ThreadPoolExecutor.class);
|
ThreadPoolExecutor bizExecutor = SpringContextHolder.getBean(ThreadPoolExecutor.class);
|
||||||
Class<?> clazz = obj.getClass();
|
Class<?> clazz = obj.getClass();
|
||||||
for (Field field : clazz.getDeclaredFields()) {
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
String fieldName = field.getName();
|
String fieldName = field.getName();
|
||||||
if (fieldNames.contains(fieldName)){
|
if (fieldNames.contains(fieldName)) {
|
||||||
String i18nKey = clazz.getName() + "." + fieldName + "." + id;
|
String i18nKey = clazz.getName() + "." + fieldName + "." + id;
|
||||||
|
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
@@ -179,13 +198,13 @@ public class MessageI18nNacosService {
|
|||||||
|
|
||||||
// 国际化数据
|
// 国际化数据
|
||||||
for (String key : keySet) {
|
for (String key : keySet) {
|
||||||
bizExecutor.execute(()->{
|
bizExecutor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
// 需要保存的值
|
// 需要保存的值
|
||||||
String i18nValue = jsonObject.getString(key);
|
String i18nValue = jsonObject.getString(key);
|
||||||
messageI18NNacosService.saveOrUpdate(new Locale(key), i18nKey, i18nValue);
|
messageI18NNacosService.saveOrUpdate(new Locale(key), i18nKey, i18nValue);
|
||||||
} catch (Exception e){
|
} catch (Exception e) {
|
||||||
log.error("{}",e.getMessage(), e);
|
log.error("{}", e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
cdl.countDown();
|
cdl.countDown();
|
||||||
}
|
}
|
||||||
|
@@ -22,7 +22,7 @@ def baidu_translate(word):
|
|||||||
api_data = {
|
api_data = {
|
||||||
'q': word,
|
'q': word,
|
||||||
'from': 'cht',
|
'from': 'cht',
|
||||||
'to': 'en',
|
'to': 'ara',
|
||||||
'appid': appid,
|
'appid': appid,
|
||||||
'salt': salt,
|
'salt': salt,
|
||||||
'sign': sign
|
'sign': sign
|
||||||
@@ -30,7 +30,7 @@ def baidu_translate(word):
|
|||||||
req_get = requests.get(api_url, api_data)
|
req_get = requests.get(api_url, api_data)
|
||||||
# 结果的位置可能不同
|
# 结果的位置可能不同
|
||||||
result_json = req_get.json()
|
result_json = req_get.json()
|
||||||
print(result_json)
|
# print(result_json)
|
||||||
result: str = ''
|
result: str = ''
|
||||||
if 'trans_result' in result_json:
|
if 'trans_result' in result_json:
|
||||||
result = result_json['trans_result'][0]['dst']
|
result = result_json['trans_result'][0]['dst']
|
||||||
@@ -85,28 +85,28 @@ if __name__ == '__main__':
|
|||||||
# print(e)
|
# print(e)
|
||||||
# continue
|
# continue
|
||||||
for m in msgs:
|
for m in msgs:
|
||||||
while True:
|
try:
|
||||||
try:
|
if len(m) == 0:
|
||||||
if len(m) == 0:
|
|
||||||
continue
|
|
||||||
arr = m.split(', ')
|
|
||||||
str1: str = ''
|
|
||||||
if len(arr) > 0:
|
|
||||||
str1 = arr[0]
|
|
||||||
str2: str = ''
|
|
||||||
if len(arr) > 1:
|
|
||||||
str2 = arr[1]
|
|
||||||
if len(str2) == 0:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
str2 = str2.split(')')[0]
|
|
||||||
str2 = str2.replace('"', '')
|
|
||||||
content = baidu_translate(str2)
|
|
||||||
if len(str1) > 0 and '(' in str1:
|
|
||||||
str1 = str1.split('(')[0]
|
|
||||||
print('BusiStatus.', str1, '=', content)
|
|
||||||
time.sleep(1)
|
|
||||||
break
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
continue
|
continue
|
||||||
|
arr = m.split(', ')
|
||||||
|
str1: str = ''
|
||||||
|
if len(arr) > 0:
|
||||||
|
str1 = arr[0]
|
||||||
|
str2: str = ''
|
||||||
|
if len(arr) > 1:
|
||||||
|
str2 = arr[1]
|
||||||
|
if len(str2) == 0:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
str2 = str2.split(')')[0]
|
||||||
|
str2 = str2.replace('"', '')
|
||||||
|
content = str2
|
||||||
|
# content = baidu_translate(str2)
|
||||||
|
if len(str1) > 0 and '(' in str1:
|
||||||
|
str1 = str1.split('(')[0]
|
||||||
|
print('BusiStatus.', str1, '="', content, '"')
|
||||||
|
# time.sleep(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
continue
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user