123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelOS.SDK
- {
- /// <summary>
- /// 文件方法
- /// </summary>
- public static class FileWay
- {
- /// <summary>
- /// 读取文件类容替换方法
- /// </summary>
- /// <param name="rootPath"></param>
- /// <param name="fileLang"></param>
- /// <param name="keyType"></param>
- /// <param name="replaceData"></param>
- /// <returns></returns>
- public static string FileValue(string rootPath, string fileLang, string keyType, Dictionary<string, object> replaceData)
- {
- string name = "";
- string path = Path.Combine(rootPath, $"LogLang/{fileLang}.json");
- var sampleJson = File.ReadAllBytes(path).AsSpan();
- Utf8JsonReader reader = new Utf8JsonReader(sampleJson);
- if (JsonDocument.TryParseValue(ref reader, out JsonDocument jsonDoc) && jsonDoc.RootElement.TryGetProperty(keyType, out JsonElement json))
- {
- string msgs = json.ToObject<string>();
- if (!string.IsNullOrEmpty(msgs))
- {
- replaceData.Keys.ToList().ForEach(x => {
- msgs = msgs.Replace("{" + x + "}", $"{replaceData[x]}");
- });
- name = msgs;
- //notifyData.body = msgs[1];
- }
- }
- return name;
- }
- }
- }
|