현지화 시스템 로직 수정

캐싱된 key값 사용으로 변경
This commit is contained in:
NTG_Lenovo 2025-07-23 14:47:23 +09:00
parent ad6f8fa409
commit 90afe5129a
2 changed files with 65 additions and 20 deletions

View File

@ -1,5 +1,8 @@
using System.Collections.Generic;
using UnityEngine.Localization.Settings;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
namespace DDD
{
@ -11,28 +14,69 @@ public enum TableName
Global_Message,
}
public static class LocalizationManager
public class LocalizationManager : Singleton<LocalizationManager>, IManager
{
private readonly Dictionary<string, string> _localizedCache = new();
private string _currentLocaleCode;
private bool _isInitialized;
public void PreInit()
{
_localizedCache.Clear();
_currentLocaleCode = GetCurrentLocaleCode();
foreach (TableName table in System.Enum.GetValues(typeof(TableName)))
{
if (table == TableName.None) continue;
var tableName = table.ToString();
var stringTable = LocalizationSettings.StringDatabase.GetTable(tableName);
if (stringTable == null)
{
Debug.LogWarning($"[Localization] Table not found: {tableName}");
continue;
}
foreach (var entry in stringTable.Values)
{
if (entry == null || string.IsNullOrEmpty(entry.KeyId.ToString())) continue;
if (!_localizedCache.ContainsKey(entry.Key))
{
_localizedCache.Add(entry.Key, entry.GetLocalizedString());
}
}
}
_isInitialized = true;
}
public Task Init()
{
return Task.CompletedTask;
}
public void PostInit()
{
}
/// <summary>
/// 현재 선택된 로케일 기준으로 로컬라이징 텍스트를 가져옵니다.
/// </summary>
public static string GetString(TableName table, string key)
public string GetString(string key)
{
if (table == TableName.None || string.IsNullOrEmpty(key))
return $"[Invalid:{table}/{key}]";
if (!_isInitialized)
{
Debug.LogWarning("[LocalizationManager] 호출 전에 초기화되지 않았습니다.");
return $"[Uninitialized:{key}]";
}
var locale = LocalizationSettings.SelectedLocale;
var tableName = table.ToString();
var stringTable = LocalizationSettings.StringDatabase.GetTable(tableName, locale);
if (stringTable == null)
return $"[Missing Table:{tableName}]";
var entry = stringTable.GetEntry(key);
if (entry == null)
return $"[Missing Key:{key}]";
return entry.GetLocalizedString();
if (_localizedCache.TryGetValue(key, out var value)) return value;
Debug.LogError("[LocalizationManager] key값이 존재하지 않습니다.");
return null;
}
/// <summary>
@ -46,12 +90,13 @@ public static string GetCurrentLocaleCode()
/// <summary>
/// 로케일 코드로 현재 언어를 설정합니다. (예: "ko", "en", "ja")
/// </summary>
public static void SetLocale(string code)
public void SetLocale(string code)
{
var locale = LocalizationSettings.AvailableLocales.Locales
.FirstOrDefault(l => l.Identifier.Code == code);
var locale = LocalizationSettings.AvailableLocales.Locales.FirstOrDefault(l => l.Identifier.Code == code);
if (locale != null)
{
LocalizationSettings.SelectedLocale = locale;
}
}
}
}

View File

@ -53,7 +53,7 @@ private void TryDisplayNext()
var evt = _messageQueue.Dequeue();
_isDisplayingMessage = true;
_messageText.text = LocalizationManager.GetString(TableName.Global_Message, evt.NewMessageKey);
_messageText.text = LocalizationManager.Instance.GetString(evt.NewMessageKey);
Open();
_fadeTween?.Kill();