Translation.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. internal static class Translation
  10. {
  11. private static readonly string[] props = { "ui", "props", "bg", "face" };
  12. private static Dictionary<string, Dictionary<string, string>> Translations;
  13. private static readonly ConfigEntry<string> currentLanguage;
  14. public static string CurrentLanguage
  15. {
  16. get => currentLanguage.Value;
  17. private set => currentLanguage.Value = value;
  18. }
  19. public static event EventHandler ReloadTranslationEvent;
  20. static Translation()
  21. {
  22. currentLanguage = Configuration.Config.Bind(
  23. "Translation", "Language",
  24. "en",
  25. "Directory to pull translations from"
  26. + "\nTranslations are found in the 'Translations' folder"
  27. );
  28. Configuration.Config.ConfigReloaded += OnSettingChange;
  29. }
  30. private static void OnSettingChange(object sender, EventArgs args) => ReloadTranslation();
  31. public static void Initialize(string language)
  32. {
  33. string rootTranslationPath = Path.Combine(Constants.configPath, Constants.translationDirectory);
  34. string currentTranslationPath = Path.Combine(rootTranslationPath, language);
  35. if (!Directory.Exists(currentTranslationPath))
  36. {
  37. Utility.LogWarning(
  38. $"No translations found for '{language}' in '{currentTranslationPath}'"
  39. );
  40. return;
  41. }
  42. Translations = new Dictionary<string, Dictionary<string, string>>(
  43. StringComparer.InvariantCultureIgnoreCase
  44. );
  45. foreach (string prop in props)
  46. {
  47. string translationFile = $"translation.{prop}.json";
  48. try
  49. {
  50. string translationPath = Path.Combine(currentTranslationPath, translationFile);
  51. string translationJson = File.ReadAllText(translationPath);
  52. JObject translation = JObject.Parse(translationJson);
  53. foreach (JProperty translationProp in translation.AsJEnumerable())
  54. {
  55. JToken token = translationProp.Value;
  56. Translations[translationProp.Path] = new Dictionary<string, string>(
  57. token.ToObject<Dictionary<string, string>>(), StringComparer.InvariantCultureIgnoreCase
  58. );
  59. }
  60. }
  61. catch
  62. {
  63. Utility.LogError($"Could not find translation file '{translationFile}'");
  64. }
  65. }
  66. }
  67. public static void ReinitializeTranslation() => Configuration.Config.Reload();
  68. public static void ReloadTranslation()
  69. {
  70. Initialize(CurrentLanguage);
  71. OnReloadTranslation();
  72. }
  73. public static void OnReloadTranslation()
  74. {
  75. ReloadTranslationEvent?.Invoke(null, EventArgs.Empty);
  76. }
  77. public static bool Has(string category, string text, bool warn = false)
  78. {
  79. if (!Translations.ContainsKey(category))
  80. {
  81. if (warn) Utility.LogWarning($"Could not translate '{text}': category '{category}' was not found");
  82. return false;
  83. }
  84. if (!Translations[category].ContainsKey(text))
  85. {
  86. if (warn)
  87. {
  88. Utility.LogWarning(
  89. $"Could not translate '{text}': '{text}' was not found in category '{category}'"
  90. );
  91. }
  92. return false;
  93. }
  94. return true;
  95. }
  96. public static string Get(string category, string text, bool warn = true)
  97. {
  98. return Has(category, text, warn) ? Translations[category][text] : text;
  99. }
  100. public static string[] GetArray(string category, IEnumerable<string> list)
  101. {
  102. return GetList(category, list).ToArray();
  103. }
  104. public static IEnumerable<string> GetList(string category, IEnumerable<string> list)
  105. {
  106. return list.Select(uiName => Get(category, uiName));
  107. }
  108. }
  109. }