60 lines
2.2 KiB
C#
60 lines
2.2 KiB
C#
using System;
|
||
using UnityEngine;
|
||
|
||
namespace BaseGames.Localization
|
||
{
|
||
/// <summary>
|
||
/// 本地化字符串访问工具类(架构 16_SupportingModules §4)。
|
||
/// 当 Unity Localization 包(com.unity.localization)安装后使用 LocalizationSettings 实现完整功能;
|
||
/// 未安装时退回到 Key 直传(返回 entryKey 本身)。
|
||
/// </summary>
|
||
public static class LocalizationManager
|
||
{
|
||
#if UNITY_LOCALIZATION
|
||
private const string DefaultTable = "UI";
|
||
|
||
/// <summary>从指定表中获取本地化字符串。</summary>
|
||
public static string Get(string entryKey, string tableName = DefaultTable)
|
||
{
|
||
try
|
||
{
|
||
var op = UnityEngine.Localization.Settings.LocalizationSettings
|
||
.StringDatabase.GetLocalizedStringAsync(tableName, entryKey);
|
||
return op.IsDone ? op.Result : entryKey;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogWarning($"[Localization] Key '{entryKey}' in table '{tableName}' 读取失败: {e.Message}");
|
||
return entryKey;
|
||
}
|
||
}
|
||
|
||
/// <summary>带格式化参数的本地化字符串。</summary>
|
||
public static string GetFormat(string entryKey, string tableName, params object[] args)
|
||
{
|
||
string template = Get(entryKey, tableName);
|
||
try { return string.Format(template, args); }
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogWarning($"[Localization] GetFormat '{entryKey}' 格式化失败: {e.Message}");
|
||
return template;
|
||
}
|
||
}
|
||
#else
|
||
/// <summary>Unity Localization 包未安装:直接返回 Key。</summary>
|
||
public static string Get(string entryKey, string tableName = "UI")
|
||
=> entryKey;
|
||
|
||
public static string GetFormat(string entryKey, string tableName, params object[] args)
|
||
{
|
||
try { return string.Format(entryKey, args); }
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogWarning($"[Localization] GetFormat '{entryKey}' 格式化失败: {e.Message}");
|
||
return entryKey;
|
||
}
|
||
}
|
||
#endif
|
||
}
|
||
}
|