FileWay.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.Extension;
  9. namespace TEAMModelOS.SDK
  10. {
  11. /// <summary>
  12. /// 文件方法
  13. /// </summary>
  14. public static class FileWay
  15. {
  16. /// <summary>
  17. /// 读取文件类容替换方法
  18. /// </summary>
  19. /// <param name="rootPath"></param>
  20. /// <param name="fileLang"></param>
  21. /// <param name="keyType"></param>
  22. /// <param name="replaceData"></param>
  23. /// <returns></returns>
  24. public static string FileValue(string rootPath, string fileLang, string keyType, Dictionary<string, object> replaceData)
  25. {
  26. string name = "";
  27. string path = Path.Combine(rootPath, $"LogLang/{fileLang}.json");
  28. var sampleJson = File.ReadAllBytes(path).AsSpan();
  29. Utf8JsonReader reader = new Utf8JsonReader(sampleJson);
  30. if (JsonDocument.TryParseValue(ref reader, out JsonDocument jsonDoc) && jsonDoc.RootElement.TryGetProperty(keyType, out JsonElement json))
  31. {
  32. string msgs = json.ToObject<string>();
  33. if (!string.IsNullOrEmpty(msgs))
  34. {
  35. replaceData.Keys.ToList().ForEach(x => {
  36. msgs = msgs.Replace("{" + x + "}", $"{replaceData[x]}");
  37. });
  38. name = msgs;
  39. //notifyData.body = msgs[1];
  40. }
  41. }
  42. return name;
  43. }
  44. }
  45. }