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 rows = new Dictionary(); 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("Localization/strings"); if (asset == null) { Debug.LogError("[Locale] Missing Resources/Localization/strings.json"); return; } var table = JsonUtility.FromJson(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); }