141 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			141 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			C#
		
	
	
using IRaCIS.Core.Application.ViewModel;
 | 
						|
using IRaCIS.Core.Domain.Share;
 | 
						|
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 string USJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json);
 | 
						|
    public static string CNJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json);
 | 
						|
 | 
						|
    static InternationalizationHelper()
 | 
						|
    {
 | 
						|
        if (!Directory.Exists(JsonFileFolder) ||
 | 
						|
          !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"));
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
    public static async Task BatchAddJsonKeyValueAsync(List<IRCGlobalInfoDTO> list)
 | 
						|
    {
 | 
						|
        await StoreInfoToFileAsync(list);
 | 
						|
    }
 | 
						|
 | 
						|
    public static async Task AddOrUpdateJsonKeyValueAsync(IRCGlobalInfoDTO info)
 | 
						|
    {
 | 
						|
        var list = new List<IRCGlobalInfoDTO>() { info };
 | 
						|
 | 
						|
        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))
 | 
						|
            {
 | 
						|
                foreach (var tojsonItem in list)
 | 
						|
                {
 | 
						|
                    jsonObject[tojsonItem.Code] = tojsonItem.Value;
 | 
						|
 | 
						|
                    //日志记录该信息方便自己人看, 返回给客户的是配置的
 | 
						|
                    StaticData.Localizer_Dev_Dic[tojsonItem.Code] = tojsonItem.Description;
 | 
						|
                }
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                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
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
}
 |