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