Translation.cs 4.2 KB

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