using System.Collections.Generic; using UnityEngine.Localization.Settings; using System.Linq; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Localization; using UnityEngine.Localization.SmartFormat.PersistentVariables; namespace DDD { public class LocalizationManager : Singleton, IManager { private readonly Dictionary _localizedCache = new(); private string _currentLocaleCode; private bool _isInitialized; private const string Name = "_name"; private const string Description = "_description"; private readonly List _singleArgBuffer = new(1); public async void PreInit() { _localizedCache.Clear(); _currentLocaleCode = GetCurrentLocaleCode(); var tables = await LocalizationSettings.StringDatabase.GetAllTables().Task; foreach (var table in tables) { var tableName = table.SharedData.TableCollectionName; if (table == null) { Debug.LogWarning($"[Localization] Table not found: {tableName}"); continue; } foreach (var entry in table.Values) { if (entry == null || string.IsNullOrEmpty(entry.Key)) continue; if (!_localizedCache.ContainsKey(entry.Key)) { var localizedString = new LocalizedString { TableReference = tableName, TableEntryReference = entry.Key }; _localizedCache.Add(entry.Key, localizedString); } } } _isInitialized = true; } public Task Init() { return Task.CompletedTask; } public void PostInit() { } /// /// 현재 선택된 로케일 기준으로 로컬라이징 텍스트를 가져옵니다. /// public LocalizedString GetLocalizedString(string key) { if (!_isInitialized) { Debug.LogWarning("[LocalizationManager] 호출 전에 초기화되지 않았습니다."); return null; } if (_localizedCache.TryGetValue(key, out var value)) return value; Debug.LogError($"[LocalizationManager] {key}값이 존재하지 않습니다."); return null; } public LocalizedString GetLocalizedName(string key) => GetLocalizedString(key + Name); public LocalizedString GetLocalizedDescription(string key) => GetLocalizedString(key + Description); /// /// 현재 로케일 기준 동기 문자열 반환 (스마트 문자열 인자 지원) /// public string GetString(string key) { var localizedString = GetLocalizedString(key); if (localizedString == null) return key; // fallback var tableRef = localizedString.TableReference; var entryRef = key; var locale = LocalizationSettings.SelectedLocale; VariablesGroupAsset variables = SmartStringVariables.Instance.GetVariablesGroupAsset(); if (variables != null) { _singleArgBuffer.Clear(); _singleArgBuffer.Add(variables); // SmartFormat이 {name}을 variables에서 찾음 return LocalizationSettings.StringDatabase.GetLocalizedString(tableRef, key, _singleArgBuffer, locale); } // 인자 없음 return LocalizationSettings.StringDatabase.GetLocalizedString(tableRef, entryRef, locale); } public string GetName(string key, VariablesGroupAsset variables = null) => GetString(key + Name); public string GetDescription(string key, VariablesGroupAsset variables = null) => GetString(key + Description); /// /// 현재 사용 중인 로케일 코드 반환 (예: "ko", "en", "ja") /// public static string GetCurrentLocaleCode() { return LocalizationSettings.SelectedLocale.Identifier.Code; } /// /// 로케일 코드로 현재 언어를 설정합니다. (예: "ko", "en", "ja") /// public void SetLocale(string code) { var locale = LocalizationSettings.AvailableLocales.Locales.FirstOrDefault(l => l.Identifier.Code == code); if (locale != null) { LocalizationSettings.SelectedLocale = locale; } } } }