Translation.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEngine;
  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. public static string CurrentLanguage { get; private set; }
  14. public static event EventHandler ReloadTranslationEvent;
  15. public static void Initialize(string language)
  16. {
  17. CurrentLanguage = language;
  18. Translations = new Dictionary<string, Dictionary<string, string>>(
  19. StringComparer.InvariantCultureIgnoreCase
  20. );
  21. string rootTranslationPath = Path.Combine(Constants.configPath, "Translations");
  22. string currentTranslationPath = Path.Combine(rootTranslationPath, CurrentLanguage);
  23. if (!Directory.Exists(currentTranslationPath))
  24. {
  25. // Directory.CreateDirectory(currentTranslationPath);
  26. Debug.LogWarning(
  27. $"No translations found for '{CurrentLanguage}' in '{currentTranslationPath}'"
  28. );
  29. return;
  30. }
  31. foreach (string prop in props)
  32. {
  33. string translationFile = $"translation.{prop}.json";
  34. try
  35. {
  36. string translationPath = Path.Combine(currentTranslationPath, translationFile);
  37. string translationJson = File.ReadAllText(translationPath);
  38. JObject translation = JObject.Parse(translationJson);
  39. foreach (JProperty translationProp in translation.AsJEnumerable())
  40. {
  41. JToken token = translationProp.Value;
  42. Translations[translationProp.Path] = new Dictionary<string, string>(
  43. token.ToObject<Dictionary<string, string>>(), StringComparer.InvariantCultureIgnoreCase
  44. );
  45. }
  46. }
  47. catch
  48. {
  49. Debug.LogError($"Could not find translation file '{translationFile}'");
  50. }
  51. }
  52. }
  53. public static void SetLanguage(string language)
  54. {
  55. Initialize(language);
  56. OnReloadTranslation();
  57. }
  58. public static void ReloadTranslation()
  59. {
  60. Initialize(CurrentLanguage);
  61. OnReloadTranslation();
  62. }
  63. public static void OnReloadTranslation()
  64. {
  65. ReloadTranslationEvent?.Invoke(null, EventArgs.Empty);
  66. }
  67. public static bool Has(string category, string text, bool warn = false)
  68. {
  69. if (!Translations.ContainsKey(category))
  70. {
  71. if (warn) Debug.LogWarning($"Could not find translation category '{category}'");
  72. return false;
  73. }
  74. if (!Translations[category].ContainsKey(text))
  75. {
  76. if (warn) Debug.LogWarning($"Could not find translation for '{text}' in '{category}'");
  77. return false;
  78. }
  79. return true;
  80. }
  81. public static string Get(string category, string text, bool warn = true)
  82. {
  83. return Has(category, text, warn) ? Translations[category][text] : text;
  84. }
  85. public static string[] GetArray(string category, IEnumerable<string> list)
  86. {
  87. return GetList(category, list).ToArray();
  88. }
  89. public static IEnumerable<string> GetList(string category, IEnumerable<string> list)
  90. {
  91. return list.Select(uiName => Get(category, uiName));
  92. }
  93. public static string[] GetList(string category, IEnumerable<KeyValuePair<string, string>> list)
  94. {
  95. return list.Select(kvp => Get(category, kvp.Key)).ToArray();
  96. }
  97. }
  98. }