TranslateHelper.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text.Json;
  4. namespace AILicenseGenerator.Helper
  5. {
  6. public class TranslateHelper
  7. {
  8. private static Dictionary<string, string> _dict = new Dictionary<string, string>();
  9. public static void Init(Dictionary<string, string> dict)
  10. {
  11. _dict = dict;
  12. }
  13. public static void Init(string path)
  14. {
  15. try
  16. {
  17. if (File.Exists(path))
  18. {
  19. var content = File.ReadAllText(path);
  20. _dict = JsonSerializer.Deserialize<Dictionary<string, string>>(content, new JsonSerializerOptions
  21. {
  22. AllowTrailingCommas = true
  23. });
  24. }
  25. }
  26. catch
  27. {
  28. }
  29. }
  30. public static string Translate(string text)
  31. {
  32. if (string.IsNullOrEmpty(text))
  33. {
  34. return null;
  35. }
  36. if (_dict.ContainsKey(text))
  37. {
  38. return _dict[text];
  39. }
  40. else
  41. {
  42. return text;
  43. }
  44. }
  45. }
  46. }