From 4f5b7be7b888c6ee96462385566bed8d748b2e92 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 28 Jun 2023 16:22:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E5=9B=BD=E9=99=85?= =?UTF-8?q?=E5=8C=96=E4=BF=AE=E6=94=B9=EF=BC=8C=E8=A7=A6=E5=8F=91=E6=9B=B4?= =?UTF-8?q?=E6=96=B0json=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helper/InternationalizationHelper.cs | 31 +++++++++++++++---- .../Common/InternationalizationService.cs | 7 +++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs b/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs index ddf14b32d..e12cb271b 100644 --- a/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs +++ b/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs @@ -17,7 +17,7 @@ namespace IRaCIS.Core.Application.Helper { public static string JsonFileFolder = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources); - public static async Task AddOrUpdateJsonKeyValueAsync(string key, string value) + public static async Task AddOrUpdateJsonKeyValueAsync(string key, string value, string valueCN) { @@ -28,15 +28,32 @@ namespace IRaCIS.Core.Application.Helper throw new BusinessValidationFailedException("国际化Json文件目录有误"); } - //将该目录下的国际化文件全部修改 - foreach (var filePath in Directory.GetFiles(JsonFileFolder).Where(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase))) + var usJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json); + var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json); + + + //更新json 文件 同时更新内存缓存的数据 + foreach (var filePath in new string[] { usJsonPath, cnJsonPath }) { var json = await File.ReadAllTextAsync(filePath); JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load }); // 添加或更新指定的键值对 - jsonObject[key] = value; + + if (filePath.Contains(StaticData.En_US_Json)) + { + jsonObject[key] = value; + + StaticData.En_US_Dic[key] = value; + } + else + { + jsonObject[key] = valueCN; + + StaticData.Zh_CN_Dic[key] = valueCN; + } + await File.WriteAllTextAsync(filePath, jsonObject.ToString()); @@ -60,8 +77,6 @@ namespace IRaCIS.Core.Application.Helper var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json); - - //本地静态文件国际化需要 foreach (var tojsonItem in toJsonList) { @@ -82,6 +97,10 @@ namespace IRaCIS.Core.Application.Helper public static void WatchJsonFile(string filePath) { + if (!File.Exists(filePath)) + { + throw new BusinessValidationFailedException("国际化Json文件不存在"); + } FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath)); watcher.Changed += (sender, e) => LoadJsonFile(filePath); diff --git a/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs b/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs index 0fa7d246c..a076d8bd6 100644 --- a/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs +++ b/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Application.Interfaces; using IRaCIS.Core.Application.ViewModel; using Microsoft.AspNetCore.Authorization; +using IRaCIS.Core.Application.Helper; namespace IRaCIS.Core.Application.Service { @@ -75,6 +76,12 @@ namespace IRaCIS.Core.Application.Service var entity = await _internationalizationRepository.InsertOrUpdateAsync(addOrEditInternationalization, true, verifyExp1); + if (addOrEditInternationalization.InternationalizationType == 1) + { + await InternationalizationHelper.AddOrUpdateJsonKeyValueAsync(entity.Code, entity.Value, entity.ValueCN); + + } + return ResponseOutput.Ok(entity.Id.ToString()); }