Translation.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using BepInEx.Configuration;
  6. using Newtonsoft.Json.Linq;
  7. namespace MeidoPhotoStudio.Plugin
  8. {
  9. public static class Translation
  10. {
  11. private const string settingsHeader = "Translation";
  12. private static readonly string[] props = { "ui", "props", "bg", "face" };
  13. private static readonly ConfigEntry<string> currentLanguage;
  14. private static readonly ConfigEntry<bool> suppressWarnings;
  15. private static Dictionary<string, Dictionary<string, string>> Translations;
  16. private static bool forceSuppressWarnings;
  17. private static bool suppressWarningsCached;
  18. public static event EventHandler ReloadTranslationEvent;
  19. public static bool SuppressWarnings
  20. {
  21. get => suppressWarningsCached;
  22. set
  23. {
  24. suppressWarningsCached = value;
  25. suppressWarnings.Value = value;
  26. }
  27. }
  28. public static string CurrentLanguage
  29. {
  30. get => currentLanguage.Value;
  31. set => currentLanguage.Value = value;
  32. }
  33. static Translation()
  34. {
  35. currentLanguage = Configuration.Config.Bind(
  36. settingsHeader, "Language",
  37. "en",
  38. "Directory to pull translations from"
  39. + "\nTranslations are found in the 'Translations' folder"
  40. );
  41. suppressWarnings = Configuration.Config.Bind(
  42. settingsHeader, "SuppressWarnings",
  43. false,
  44. "Suppress translation warnings from showing up in the console"
  45. );
  46. suppressWarningsCached = !suppressWarnings.Value;
  47. }
  48. public static void Initialize(string language)
  49. {
  50. forceSuppressWarnings = false;
  51. var rootTranslationPath = Path.Combine(Constants.configPath, Constants.translationDirectory);
  52. var currentTranslationPath = Path.Combine(rootTranslationPath, language);
  53. Translations = new(StringComparer.InvariantCultureIgnoreCase);
  54. if (!Directory.Exists(currentTranslationPath))
  55. {
  56. Utility.LogError($"No translations found for '{language}' in '{currentTranslationPath}'");
  57. forceSuppressWarnings = true;
  58. return;
  59. }
  60. foreach (var prop in props)
  61. {
  62. var translationFile = $"translation.{prop}.json";
  63. try
  64. {
  65. var translationPath = Path.Combine(currentTranslationPath, translationFile);
  66. var translationJson = File.ReadAllText(translationPath);
  67. var translation = JObject.Parse(translationJson);
  68. foreach (var translationProp in translation.AsJEnumerable().Cast<JProperty>())
  69. {
  70. var token = translationProp.Value;
  71. Translations[translationProp.Path] = new(
  72. token.ToObject<Dictionary<string, string>>(), StringComparer.InvariantCultureIgnoreCase
  73. );
  74. }
  75. }
  76. catch
  77. {
  78. forceSuppressWarnings = true;
  79. Utility.LogError($"Could not find translation file '{translationFile}'");
  80. }
  81. }
  82. }
  83. public static void ReinitializeTranslation()
  84. {
  85. Initialize(CurrentLanguage);
  86. ReloadTranslationEvent?.Invoke(null, EventArgs.Empty);
  87. }
  88. public static bool Has(string category, string text, bool warn = false)
  89. {
  90. warn = !forceSuppressWarnings && !SuppressWarnings && warn;
  91. if (!Translations.ContainsKey(category))
  92. {
  93. if (warn)
  94. Utility.LogWarning($"Could not translate '{text}': category '{category}' was not found");
  95. return false;
  96. }
  97. if (!Translations[category].ContainsKey(text))
  98. {
  99. if (warn)
  100. {
  101. Utility.LogWarning(
  102. $"Could not translate '{text}': '{text}' was not found in category '{category}'"
  103. );
  104. }
  105. return false;
  106. }
  107. return true;
  108. }
  109. public static string Get(string category, string text, bool warn = true) =>
  110. Has(category, text, warn) ? Translations[category][text] : text;
  111. public static string[] GetArray(string category, IEnumerable<string> list) =>
  112. GetList(category, list).ToArray();
  113. public static IEnumerable<string> GetList(string category, IEnumerable<string> list) =>
  114. list.Select(uiName => Get(category, uiName));
  115. }
  116. }