LocaleService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. [Serializable]
  6. public class LocaleRow
  7. {
  8. public string key;
  9. public string en;
  10. public string es;
  11. public string pt_BR;
  12. public string de;
  13. public string fr;
  14. public string it;
  15. }
  16. [Serializable]
  17. public class LocaleTable
  18. {
  19. public LocaleRow[] entries;
  20. }
  21. public static class LocaleService
  22. {
  23. public static readonly string[] SupportedLocales = { "en", "es", "pt-BR", "de", "fr", "it" };
  24. static readonly Dictionary<string, LocaleRow> rows = new Dictionary<string, LocaleRow>();
  25. static string currentLocale = "en";
  26. static bool initialized;
  27. public static event Action Changed;
  28. public static string CurrentLocale => currentLocale;
  29. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  30. static void ResetStatics()
  31. {
  32. rows.Clear();
  33. currentLocale = "en";
  34. initialized = false;
  35. Changed = null;
  36. }
  37. public static void EnsureInitialized()
  38. {
  39. if (initialized)
  40. return;
  41. initialized = true;
  42. rows.Clear();
  43. var asset = Resources.Load<TextAsset>("Localization/strings");
  44. if (asset == null)
  45. {
  46. Debug.LogError("[Locale] Missing Resources/Localization/strings.json");
  47. return;
  48. }
  49. var table = JsonUtility.FromJson<LocaleTable>(asset.text);
  50. if (table?.entries == null)
  51. return;
  52. for (int i = 0; i < table.entries.Length; i++)
  53. {
  54. var row = table.entries[i];
  55. if (row == null || string.IsNullOrEmpty(row.key))
  56. continue;
  57. rows[row.key] = row;
  58. }
  59. }
  60. public static void SyncFromSave(string localeId, bool notify = false)
  61. {
  62. EnsureInitialized();
  63. if (!IsSupported(localeId))
  64. localeId = "en";
  65. currentLocale = localeId;
  66. if (notify)
  67. Changed?.Invoke();
  68. }
  69. public static bool SetLocale(string localeId)
  70. {
  71. EnsureInitialized();
  72. if (!IsSupported(localeId))
  73. return false;
  74. if (currentLocale == localeId)
  75. return true;
  76. currentLocale = localeId;
  77. SaveService.Data.settings.localeId = localeId;
  78. SaveService.SaveNow();
  79. Changed?.Invoke();
  80. return true;
  81. }
  82. public static bool IsSupported(string localeId)
  83. {
  84. for (int i = 0; i < SupportedLocales.Length; i++)
  85. {
  86. if (SupportedLocales[i] == localeId)
  87. return true;
  88. }
  89. return false;
  90. }
  91. public static string Get(string key)
  92. {
  93. EnsureInitialized();
  94. if (string.IsNullOrEmpty(key))
  95. return "";
  96. if (!rows.TryGetValue(key, out var row))
  97. return key;
  98. return Pick(row) ?? row.en ?? key;
  99. }
  100. public static string Format(string key, params object[] args)
  101. {
  102. string template = Get(key);
  103. if (args == null || args.Length == 0)
  104. return template;
  105. try
  106. {
  107. return string.Format(template, args);
  108. }
  109. catch (FormatException)
  110. {
  111. return template;
  112. }
  113. }
  114. static string Pick(LocaleRow row)
  115. {
  116. string value = currentLocale switch
  117. {
  118. "es" => row.es,
  119. "pt-BR" => row.pt_BR,
  120. "de" => row.de,
  121. "fr" => row.fr,
  122. "it" => row.it,
  123. _ => row.en
  124. };
  125. if (string.IsNullOrEmpty(value))
  126. value = row.en;
  127. return value;
  128. }
  129. public static string DisplayLanguageName(string localeId)
  130. {
  131. string key = "settings.language.name." + localeId;
  132. string value = Get(key);
  133. return string.IsNullOrEmpty(value) || value == key ? localeId : value;
  134. }
  135. }
  136. public static class L
  137. {
  138. public static string Get(string key) => LocaleService.Get(key);
  139. public static string F(string key, params object[] args) => LocaleService.Format(key, args);
  140. }