| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- [Serializable]
- public class LocaleRow
- {
- public string key;
- public string en;
- public string es;
- public string pt_BR;
- public string de;
- public string fr;
- public string it;
- }
- [Serializable]
- public class LocaleTable
- {
- public LocaleRow[] entries;
- }
- public static class LocaleService
- {
- public static readonly string[] SupportedLocales = { "en", "es", "pt-BR", "de", "fr", "it" };
- static readonly Dictionary<string, LocaleRow> rows = new Dictionary<string, LocaleRow>();
- static string currentLocale = "en";
- static bool initialized;
- public static event Action Changed;
- public static string CurrentLocale => currentLocale;
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
- static void ResetStatics()
- {
- rows.Clear();
- currentLocale = "en";
- initialized = false;
- Changed = null;
- }
- public static void EnsureInitialized()
- {
- if (initialized)
- return;
- initialized = true;
- rows.Clear();
- var asset = Resources.Load<TextAsset>("Localization/strings");
- if (asset == null)
- {
- Debug.LogError("[Locale] Missing Resources/Localization/strings.json");
- return;
- }
- var table = JsonUtility.FromJson<LocaleTable>(asset.text);
- if (table?.entries == null)
- return;
- for (int i = 0; i < table.entries.Length; i++)
- {
- var row = table.entries[i];
- if (row == null || string.IsNullOrEmpty(row.key))
- continue;
- rows[row.key] = row;
- }
- }
- public static void SyncFromSave(string localeId, bool notify = false)
- {
- EnsureInitialized();
- if (!IsSupported(localeId))
- localeId = "en";
- currentLocale = localeId;
- if (notify)
- Changed?.Invoke();
- }
- public static bool SetLocale(string localeId)
- {
- EnsureInitialized();
- if (!IsSupported(localeId))
- return false;
- if (currentLocale == localeId)
- return true;
- currentLocale = localeId;
- SaveService.Data.settings.localeId = localeId;
- SaveService.SaveNow();
- Changed?.Invoke();
- return true;
- }
- public static bool IsSupported(string localeId)
- {
- for (int i = 0; i < SupportedLocales.Length; i++)
- {
- if (SupportedLocales[i] == localeId)
- return true;
- }
- return false;
- }
- public static string Get(string key)
- {
- EnsureInitialized();
- if (string.IsNullOrEmpty(key))
- return "";
- if (!rows.TryGetValue(key, out var row))
- return key;
- return Pick(row) ?? row.en ?? key;
- }
- public static string Format(string key, params object[] args)
- {
- string template = Get(key);
- if (args == null || args.Length == 0)
- return template;
- try
- {
- return string.Format(template, args);
- }
- catch (FormatException)
- {
- return template;
- }
- }
- static string Pick(LocaleRow row)
- {
- string value = currentLocale switch
- {
- "es" => row.es,
- "pt-BR" => row.pt_BR,
- "de" => row.de,
- "fr" => row.fr,
- "it" => row.it,
- _ => row.en
- };
- if (string.IsNullOrEmpty(value))
- value = row.en;
- return value;
- }
- public static string DisplayLanguageName(string localeId)
- {
- string key = "settings.language.name." + localeId;
- string value = Get(key);
- return string.IsNullOrEmpty(value) || value == key ? localeId : value;
- }
- }
- public static class L
- {
- public static string Get(string key) => LocaleService.Get(key);
- public static string F(string key, params object[] args) => LocaleService.Format(key, args);
- }
|