精简国际化代码
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
7d07e18853
commit
ece98887da
|
@ -260,6 +260,7 @@ var hangfireJobService = app.Services.GetRequiredService<IIRaCISHangfireJob>();
|
|||
|
||||
await hangfireJobService.InitHangfireJobTaskAsync();
|
||||
|
||||
//设置国际化I18n
|
||||
var localizer = app.Services.GetRequiredService<IStringLocalizer>();
|
||||
I18n.SetLocalizer(localizer);
|
||||
|
||||
|
|
|
@ -9,9 +9,6 @@ namespace IRaCIS.Core.Application.Service.BackGroundJob
|
|||
|
||||
public interface IIRaCISHangfireJob
|
||||
{
|
||||
|
||||
//Task MemoryCacheTrialStatusAsync();
|
||||
|
||||
Task InitHangfireJobTaskAsync();
|
||||
|
||||
}
|
||||
|
@ -29,32 +26,27 @@ namespace IRaCIS.Core.Application.Service.BackGroundJob
|
|||
|
||||
//初始化国际化
|
||||
|
||||
await InternationalizationHelper.InitInternationlizationDataAndWatchJsonFileAsync(_internationalizationRepository);
|
||||
//查询数据库的数据
|
||||
var toJsonList = await _internationalizationRepository.Where(t => t.InternationalizationType == 1).Select(t => new IRCGlobalInfoDTO()
|
||||
{
|
||||
Code = t.Code,
|
||||
Value = t.Value,
|
||||
ValueCN = t.ValueCN,
|
||||
Description = t.Description
|
||||
}).ToListAsync();
|
||||
|
||||
|
||||
await InternationalizationHelper.BatchAddJsonKeyValueAsync(toJsonList);
|
||||
|
||||
//创建邮件定时任务
|
||||
await InitSysAndTrialCronJobAsync();
|
||||
|
||||
#region 废弃
|
||||
////项目状态 立即加载到缓存中
|
||||
//await MemoryCacheTrialStatusAsync();
|
||||
|
||||
////await MemoryCacheAnonymizeData();
|
||||
|
||||
|
||||
////创建项目缓存 定时任务
|
||||
//HangfireJobHelper.AddOrUpdateInitCronJob<IIRaCISHangfireJob>("RecurringJob_Cache_TrialState", t => t.MemoryCacheTrialStatusAsync(), Cron.Daily());
|
||||
#endregion
|
||||
|
||||
|
||||
_logger.LogInformation("项目启动 hangfire 任务初始化 执行结束");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public async Task InitSysAndTrialCronJobAsync()
|
||||
{
|
||||
//var deleteJobIdList = await _trialEmailNoticeConfigRepository.Where(t => t.Trial.TrialStatusStr != StaticData.TrialState.TrialOngoing && t.EmailCron != string.Empty && t.IsAutoSend)
|
||||
|
|
|
@ -4,203 +4,136 @@ using IRaCIS.Core.Infrastructure;
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SharpCompress.Common;
|
||||
|
||||
namespace IRaCIS.Core.Application.Helper;
|
||||
|
||||
|
||||
public class IRCGlobalInfoDTO
|
||||
{
|
||||
public string Code { get; set; }
|
||||
|
||||
public string Value { get; set; }
|
||||
|
||||
public string ValueCN { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
}
|
||||
|
||||
public static class InternationalizationHelper
|
||||
{
|
||||
public static string JsonFileFolder = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources);
|
||||
|
||||
public static FileSystemWatcher FileSystemWatcher_US { get; set; }
|
||||
public static FileSystemWatcher FileSystemWatcher_CN { get; set; }
|
||||
public static string USJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json);
|
||||
public static string CNJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json);
|
||||
|
||||
private static void VerifyFolder()
|
||||
static InternationalizationHelper()
|
||||
{
|
||||
if (!Directory.Exists(JsonFileFolder) ||
|
||||
!Directory.GetFiles(JsonFileFolder).Any(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
throw new BusinessValidationFailedException("国际化Json文件目录有误");
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task BatchAddJsonKeyValueAsync(List<BatchAddInternationalizationDto> batchAddDtos)
|
||||
{
|
||||
VerifyFolder();
|
||||
|
||||
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 });
|
||||
|
||||
// 添加或更新指定的键值对
|
||||
|
||||
if (filePath.Contains(StaticData.En_US_Json))
|
||||
{
|
||||
foreach (var item in batchAddDtos)
|
||||
{
|
||||
jsonObject[item.Code] = item.Value;
|
||||
|
||||
StaticData.En_US_Dic[item.Code] = item.Value;
|
||||
|
||||
//日志记录该信息方便自己人看, 返回给客户的是配置的
|
||||
StaticData.Localizer_Dev_Dic[item.Code] = item.Description;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in batchAddDtos)
|
||||
{
|
||||
jsonObject[item.Code] = item.Value;
|
||||
|
||||
StaticData.Zh_CN_Dic[item.Code] = item.Value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
await File.WriteAllTextAsync(filePath, jsonObject.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task AddOrUpdateJsonKeyValueAsync(string key, string value, string valueCN, string description)
|
||||
{
|
||||
|
||||
VerifyFolder();
|
||||
|
||||
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 });
|
||||
|
||||
// 添加或更新指定的键值对
|
||||
|
||||
if (filePath.Contains(StaticData.En_US_Json))
|
||||
{
|
||||
jsonObject[key] = value;
|
||||
|
||||
StaticData.En_US_Dic[key] = value;
|
||||
|
||||
//日志记录该信息方便自己人看, 返回给客户的是配置的
|
||||
StaticData.Localizer_Dev_Dic[key] = description;
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonObject[key] = valueCN;
|
||||
|
||||
StaticData.Zh_CN_Dic[key] = valueCN;
|
||||
}
|
||||
|
||||
|
||||
await File.WriteAllTextAsync(filePath, jsonObject.ToString());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static async Task InitInternationlizationDataAndWatchJsonFileAsync(IRepository<Internationalization> _internationalizationRepository)
|
||||
{
|
||||
//查询数据库的数据
|
||||
var toJsonList = await _internationalizationRepository.Where(t => t.InternationalizationType == 1).Select(t => new
|
||||
{
|
||||
t.Code,
|
||||
t.Value,
|
||||
t.ValueCN,
|
||||
t.Description
|
||||
}).ToListAsync();
|
||||
|
||||
//组织成json 文件
|
||||
|
||||
var usJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json);
|
||||
var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json);
|
||||
|
||||
|
||||
//本地静态文件国际化需要
|
||||
foreach (var tojsonItem in toJsonList)
|
||||
{
|
||||
StaticData.En_US_Dic[tojsonItem.Code] = tojsonItem.Value;
|
||||
StaticData.Zh_CN_Dic[tojsonItem.Code] = tojsonItem.ValueCN;
|
||||
|
||||
//日志记录该信息方便自己人看, 返回给客户的是配置的
|
||||
StaticData.Localizer_Dev_Dic[tojsonItem.Code] = tojsonItem.Description;
|
||||
}
|
||||
|
||||
File.WriteAllText(usJsonPath, JsonConvert.SerializeObject(StaticData.En_US_Dic));
|
||||
File.WriteAllText(cnJsonPath, JsonConvert.SerializeObject(StaticData.Zh_CN_Dic));
|
||||
|
||||
|
||||
//监测Json文件变更 实时刷新数据
|
||||
|
||||
if (!File.Exists(usJsonPath) || !File.Exists(cnJsonPath))
|
||||
!Directory.GetFiles(JsonFileFolder).Any(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase))||
|
||||
!File.Exists(USJsonPath) || !File.Exists(CNJsonPath))
|
||||
{
|
||||
throw new BusinessValidationFailedException(I18n.T("IRaCISCHangfireJob_FileNotFound"));
|
||||
}
|
||||
|
||||
|
||||
//监测Json文件变更 实时刷新数据
|
||||
|
||||
|
||||
FileSystemWatcher_US = new FileSystemWatcher
|
||||
{
|
||||
Path = Path.GetDirectoryName(usJsonPath)!,
|
||||
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size,
|
||||
Filter = Path.GetFileName(usJsonPath),
|
||||
EnableRaisingEvents = true,
|
||||
|
||||
};
|
||||
// 添加文件更改事件的处理程序
|
||||
FileSystemWatcher_US.Changed += (sender, e) => LoadJsonFile(StaticData.Folder.Resources + "\\" + StaticData.En_US_Json);
|
||||
|
||||
|
||||
FileSystemWatcher_CN = new FileSystemWatcher
|
||||
{
|
||||
Path = Path.GetDirectoryName(cnJsonPath)!,
|
||||
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size,
|
||||
Filter = Path.GetFileName(cnJsonPath),
|
||||
EnableRaisingEvents = true,
|
||||
|
||||
};
|
||||
FileSystemWatcher_CN.Changed += (sender, e) => LoadJsonFile(StaticData.Folder.Resources + "\\" + StaticData.Zh_CN_Json);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void LoadJsonFile(string filePath)
|
||||
public static async Task BatchAddJsonKeyValueAsync(List<IRCGlobalInfoDTO> list)
|
||||
{
|
||||
Console.WriteLine("刷新json内存数据");
|
||||
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile(filePath, false, false);
|
||||
await StoreInfoToFileAsync(list);
|
||||
}
|
||||
|
||||
IConfigurationRoot enConfiguration = builder.Build();
|
||||
public static async Task AddOrUpdateJsonKeyValueAsync(IRCGlobalInfoDTO info)
|
||||
{
|
||||
var list = new List<IRCGlobalInfoDTO>() { info };
|
||||
|
||||
foreach (IConfigurationSection section in enConfiguration.GetChildren())
|
||||
await StoreInfoToFileAsync(list);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static async Task StoreInfoToFileAsync(List<IRCGlobalInfoDTO> list)
|
||||
{
|
||||
foreach (var filePath in new string[] { USJsonPath, CNJsonPath })
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(filePath);
|
||||
|
||||
JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load });
|
||||
|
||||
if (filePath.Contains(StaticData.En_US_Json))
|
||||
{
|
||||
StaticData.En_US_Dic[section.Key] = section.Value;
|
||||
foreach (var tojsonItem in list)
|
||||
{
|
||||
jsonObject[tojsonItem.Code] = tojsonItem.Value;
|
||||
|
||||
//日志记录该信息方便自己人看, 返回给客户的是配置的
|
||||
StaticData.Localizer_Dev_Dic[tojsonItem.Code] = tojsonItem.Description;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
StaticData.Zh_CN_Dic[section.Key] = section.Value;
|
||||
foreach (var tojsonItem in list)
|
||||
{
|
||||
jsonObject[tojsonItem.Code] = tojsonItem.ValueCN;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
await File.WriteAllTextAsync(filePath, jsonObject.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 监测Json文件变更 实时刷新数据 废弃
|
||||
//FileSystemWatcher_US = new FileSystemWatcher
|
||||
//{
|
||||
// Path = Path.GetDirectoryName(USJsonPath)!,
|
||||
// NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size,
|
||||
// Filter = Path.GetFileName(USJsonPath),
|
||||
// EnableRaisingEvents = true,
|
||||
|
||||
//};
|
||||
//// 添加文件更改事件的处理程序
|
||||
//FileSystemWatcher_US.Changed += (sender, e) => LoadJsonFile(StaticData.Folder.Resources + "\\" + StaticData.En_US_Json);
|
||||
|
||||
|
||||
//FileSystemWatcher_CN = new FileSystemWatcher
|
||||
//{
|
||||
// Path = Path.GetDirectoryName(CNJsonPath)!,
|
||||
// NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size,
|
||||
// Filter = Path.GetFileName(CNJsonPath),
|
||||
// EnableRaisingEvents = true,
|
||||
|
||||
//};
|
||||
//FileSystemWatcher_CN.Changed += (sender, e) => LoadJsonFile(StaticData.Folder.Resources + "\\" + StaticData.Zh_CN_Json);
|
||||
|
||||
|
||||
//private static void LoadJsonFile(string filePath)
|
||||
//{
|
||||
// Console.WriteLine("刷新json内存数据");
|
||||
// IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile(filePath, false, false);
|
||||
|
||||
// IConfigurationRoot enConfiguration = builder.Build();
|
||||
|
||||
// foreach (IConfigurationSection section in enConfiguration.GetChildren())
|
||||
// {
|
||||
// if (filePath.Contains(StaticData.En_US_Json))
|
||||
// {
|
||||
// StaticData.En_US_Dic[section.Key] = section.Value;
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// StaticData.Zh_CN_Dic[section.Key] = section.Value;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -175,7 +175,7 @@ namespace IRaCIS.Core.Application.Service
|
|||
|
||||
if (addOrEditInternationalization.InternationalizationType == 1)
|
||||
{
|
||||
await InternationalizationHelper.AddOrUpdateJsonKeyValueAsync(entity.Code, addOrEditInternationalization.Value, addOrEditInternationalization.ValueCN, addOrEditInternationalization.Description);
|
||||
await InternationalizationHelper.AddOrUpdateJsonKeyValueAsync(_mapper.Map<IRCGlobalInfoDTO>(addOrEditInternationalization));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using AutoMapper;
|
||||
using IRaCIS.Application.Contracts;
|
||||
using IRaCIS.Core.Application.Contracts;
|
||||
using IRaCIS.Core.Application.Helper;
|
||||
using IRaCIS.Core.Application.ViewModel;
|
||||
|
||||
namespace IRaCIS.Core.Application.Service
|
||||
|
@ -70,7 +71,10 @@ namespace IRaCIS.Core.Application.Service
|
|||
|
||||
CreateMap<Internationalization, BatchInternationalizationDto>().ReverseMap();
|
||||
|
||||
CreateMap<Internationalization, IRCGlobalInfoDTO>();
|
||||
|
||||
CreateMap<EventStoreRecord, EventStoreRecordView>();
|
||||
|
||||
CreateMap<BatchAddInternationalizationDto, InternationalizationAddOrEdit>();
|
||||
|
||||
|
||||
|
|
|
@ -8,11 +8,6 @@ namespace IRaCIS.Core.Domain.Share;
|
|||
|
||||
public static class StaticData
|
||||
{
|
||||
|
||||
public static Dictionary<string, string> En_US_Dic = new Dictionary<string, string>();
|
||||
|
||||
public static Dictionary<string, string> Zh_CN_Dic = new Dictionary<string, string>();
|
||||
|
||||
public static Dictionary<string, string> Localizer_Dev_Dic = new Dictionary<string, string>();
|
||||
|
||||
|
||||
|
@ -29,24 +24,33 @@ public static class StaticData
|
|||
public static readonly string en_US_bookMark = "en_us";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取国际化
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static string I18n(string key, params object?[] args)
|
||||
{
|
||||
var isEn_US = System.Globalization.CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
|
||||
#region 国际化废弃
|
||||
|
||||
//public static Dictionary<string, string> En_US_Dic = new Dictionary<string, string>();
|
||||
|
||||
//public static Dictionary<string, string> Zh_CN_Dic = new Dictionary<string, string>();
|
||||
///// <summary>
|
||||
///// 获取国际化
|
||||
///// </summary>
|
||||
///// <param name="data"></param>
|
||||
///// <returns></returns>
|
||||
//public static string I18n(string key, params object?[] args)
|
||||
//{
|
||||
// var isEn_US = System.Globalization.CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
|
||||
|
||||
// if (isEn_US)
|
||||
// {
|
||||
// return En_US_Dic[key];
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return Zh_CN_Dic[key];
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
if (isEn_US)
|
||||
{
|
||||
return En_US_Dic[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
return Zh_CN_Dic[key];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
|
Loading…
Reference in New Issue