2025-07-23 05:47:23 +00:00
|
|
|
using System.Collections.Generic;
|
2025-07-17 10:11:00 +00:00
|
|
|
using UnityEngine.Localization.Settings;
|
|
|
|
using System.Linq;
|
2025-07-23 05:47:23 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using UnityEngine;
|
2025-07-23 06:14:35 +00:00
|
|
|
using UnityEngine.Localization;
|
2025-07-17 03:32:31 +00:00
|
|
|
|
|
|
|
namespace DDD
|
|
|
|
{
|
2025-07-17 10:11:00 +00:00
|
|
|
public enum TableName
|
2025-07-17 03:32:31 +00:00
|
|
|
{
|
2025-07-17 10:11:00 +00:00
|
|
|
None = 0,
|
|
|
|
Item_Name,
|
|
|
|
Item_Description,
|
2025-07-21 07:56:58 +00:00
|
|
|
Global_Message,
|
2025-07-17 03:32:31 +00:00
|
|
|
}
|
2025-07-17 10:11:00 +00:00
|
|
|
|
2025-07-23 05:47:23 +00:00
|
|
|
public class LocalizationManager : Singleton<LocalizationManager>, IManager
|
2025-07-17 10:11:00 +00:00
|
|
|
{
|
2025-07-23 06:14:35 +00:00
|
|
|
private readonly Dictionary<string, LocalizedString> _localizedCache = new();
|
2025-07-23 05:47:23 +00:00
|
|
|
private string _currentLocaleCode;
|
|
|
|
private bool _isInitialized;
|
|
|
|
|
|
|
|
public void PreInit()
|
2025-07-17 10:11:00 +00:00
|
|
|
{
|
2025-07-23 05:47:23 +00:00
|
|
|
_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)
|
|
|
|
{
|
2025-07-23 06:14:35 +00:00
|
|
|
if (entry == null || string.IsNullOrEmpty(entry.Key)) continue;
|
2025-07-23 05:47:23 +00:00
|
|
|
|
|
|
|
if (!_localizedCache.ContainsKey(entry.Key))
|
|
|
|
{
|
2025-07-23 06:14:35 +00:00
|
|
|
var localizedString = new LocalizedString
|
|
|
|
{
|
|
|
|
TableReference = tableName,
|
|
|
|
TableEntryReference = entry.Key
|
|
|
|
};
|
|
|
|
_localizedCache.Add(entry.Key, localizedString);
|
2025-07-23 05:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_isInitialized = true;
|
|
|
|
}
|
2025-07-17 10:11:00 +00:00
|
|
|
|
2025-07-23 05:47:23 +00:00
|
|
|
public Task Init()
|
|
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2025-07-17 10:11:00 +00:00
|
|
|
|
2025-07-23 05:47:23 +00:00
|
|
|
public void PostInit()
|
|
|
|
{
|
2025-07-17 10:11:00 +00:00
|
|
|
|
2025-07-23 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 현재 선택된 로케일 기준으로 로컬라이징 텍스트를 가져옵니다.
|
|
|
|
/// </summary>
|
2025-07-23 06:14:35 +00:00
|
|
|
public LocalizedString GetLocalizedString(string key)
|
2025-07-23 05:47:23 +00:00
|
|
|
{
|
|
|
|
if (!_isInitialized)
|
|
|
|
{
|
|
|
|
Debug.LogWarning("[LocalizationManager] 호출 전에 초기화되지 않았습니다.");
|
2025-07-23 06:14:35 +00:00
|
|
|
return null;
|
2025-07-23 05:47:23 +00:00
|
|
|
}
|
2025-07-17 10:11:00 +00:00
|
|
|
|
2025-07-23 05:47:23 +00:00
|
|
|
if (_localizedCache.TryGetValue(key, out var value)) return value;
|
|
|
|
|
|
|
|
Debug.LogError("[LocalizationManager] key값이 존재하지 않습니다.");
|
|
|
|
return null;
|
2025-07-17 10:11:00 +00:00
|
|
|
}
|
|
|
|
|
2025-07-23 06:14:35 +00:00
|
|
|
public string GetString(string key)
|
|
|
|
{
|
|
|
|
var localizedString = GetLocalizedString(key);
|
|
|
|
return LocalizationSettings.StringDatabase.GetLocalizedString(localizedString.TableReference, key, LocalizationSettings.SelectedLocale);
|
|
|
|
}
|
|
|
|
|
2025-07-17 10:11:00 +00:00
|
|
|
/// <summary>
|
|
|
|
/// 현재 사용 중인 로케일 코드 반환 (예: "ko", "en", "ja")
|
|
|
|
/// </summary>
|
|
|
|
public static string GetCurrentLocaleCode()
|
|
|
|
{
|
|
|
|
return LocalizationSettings.SelectedLocale.Identifier.Code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 로케일 코드로 현재 언어를 설정합니다. (예: "ko", "en", "ja")
|
|
|
|
/// </summary>
|
2025-07-23 05:47:23 +00:00
|
|
|
public void SetLocale(string code)
|
2025-07-17 10:11:00 +00:00
|
|
|
{
|
2025-07-23 05:47:23 +00:00
|
|
|
var locale = LocalizationSettings.AvailableLocales.Locales.FirstOrDefault(l => l.Identifier.Code == code);
|
2025-07-17 10:11:00 +00:00
|
|
|
if (locale != null)
|
2025-07-23 05:47:23 +00:00
|
|
|
{
|
2025-07-17 10:11:00 +00:00
|
|
|
LocalizationSettings.SelectedLocale = locale;
|
2025-07-23 05:47:23 +00:00
|
|
|
}
|
2025-07-17 10:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|