Translation.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using Newtonsoft.Json.Linq;
  6. using BepInEx.Configuration;
  7. namespace COM3D2.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 Dictionary<string, Dictionary<string, string>> Translations;
  14. private static readonly ConfigEntry<string> currentLanguage;
  15. private static readonly ConfigEntry<bool> suppressWarnings;
  16. private static bool forceSuppressWarnings;
  17. private static bool suppressWarningsCached;
  18. public static bool SuppressWarnings
  19. {
  20. get => suppressWarningsCached;
  21. set
  22. {
  23. suppressWarningsCached = value;
  24. suppressWarnings.Value = value;
  25. }
  26. }
  27. public static string CurrentLanguage
  28. {
  29. get => currentLanguage.Value;
  30. set => currentLanguage.Value = value;
  31. }
  32. public static event EventHandler ReloadTranslationEvent;
  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. string rootTranslationPath = Path.Combine(Constants.configPath, Constants.translationDirectory);
  52. string currentTranslationPath = Path.Combine(rootTranslationPath, language);
  53. Translations = new Dictionary<string, Dictionary<string, string>>(
  54. StringComparer.InvariantCultureIgnoreCase
  55. );
  56. if (!Directory.Exists(currentTranslationPath))
  57. {
  58. Utility.LogError(
  59. $"No translations found for '{language}' in '{currentTranslationPath}'"
  60. );
  61. forceSuppressWarnings = true;
  62. return;
  63. }
  64. foreach (string prop in props)
  65. {
  66. string translationFile = $"translation.{prop}.json";
  67. try
  68. {
  69. string translationPath = Path.Combine(currentTranslationPath, translationFile);
  70. string translationJson = File.ReadAllText(translationPath);
  71. JObject translation = JObject.Parse(translationJson);
  72. foreach (JProperty translationProp in translation.AsJEnumerable())
  73. {
  74. JToken token = translationProp.Value;
  75. Translations[translationProp.Path] = new Dictionary<string, string>(
  76. token.ToObject<Dictionary<string, string>>(), StringComparer.InvariantCultureIgnoreCase
  77. );
  78. }
  79. }
  80. catch
  81. {
  82. forceSuppressWarnings = true;
  83. Utility.LogError($"Could not find translation file '{translationFile}'");
  84. }
  85. }
  86. }
  87. public static void ReinitializeTranslation()
  88. {
  89. Initialize(CurrentLanguage);
  90. ReloadTranslationEvent?.Invoke(null, EventArgs.Empty);
  91. }
  92. public static bool Has(string category, string text, bool warn = false)
  93. {
  94. warn = !forceSuppressWarnings && !SuppressWarnings && warn;
  95. if (!Translations.ContainsKey(category))
  96. {
  97. if (warn) Utility.LogWarning($"Could not translate '{text}': category '{category}' was not found");
  98. return false;
  99. }
  100. if (!Translations[category].ContainsKey(text))
  101. {
  102. if (warn)
  103. {
  104. Utility.LogWarning(
  105. $"Could not translate '{text}': '{text}' was not found in category '{category}'"
  106. );
  107. }
  108. return false;
  109. }
  110. return true;
  111. }
  112. public static string Get(string category, string text, bool warn = true)
  113. {
  114. return Has(category, text, warn) ? Translations[category][text] : text;
  115. }
  116. public static string[] GetArray(string category, IEnumerable<string> list)
  117. {
  118. return GetList(category, list).ToArray();
  119. }
  120. public static IEnumerable<string> GetList(string category, IEnumerable<string> list)
  121. {
  122. return list.Select(uiName => Get(category, uiName));
  123. }
  124. }
  125. }