本地化
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Localization;
|
||||
using UnityEngine.Localization.Settings;
|
||||
using UnityEngine.Localization.Tables;
|
||||
@@ -7,9 +8,43 @@ namespace AibisDream.Framework
|
||||
{
|
||||
public static class LocalizationKit
|
||||
{
|
||||
private static LocalizationTable _actorTableName;
|
||||
private const string LocalizationPrefix = "l10n.";
|
||||
|
||||
public static LocalizationTable ActorTableName => _actorTableName ??= new LocalizationTable("ActorName");
|
||||
private static LocalizationTable _paramTable;
|
||||
|
||||
public static LocalizationTable ActorTableName { get; private set; }
|
||||
|
||||
static LocalizationKit()
|
||||
{
|
||||
Debug.Log("初始化");
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playModeStateChanged += OnPlayModeChanged;
|
||||
#else
|
||||
_paramTable = new LocalizationTable("Param");
|
||||
ActorTableName = new LocalizationTable("ActorName");
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private static void OnPlayModeChanged(PlayModeStateChange playMode)
|
||||
{
|
||||
if (playMode == PlayModeStateChange.EnteredPlayMode)
|
||||
{
|
||||
// 进入游戏模式的时候
|
||||
_paramTable?.Dispose();
|
||||
ActorTableName?.Dispose();
|
||||
_paramTable = new LocalizationTable("Params");
|
||||
ActorTableName = new LocalizationTable("ActorName");
|
||||
}
|
||||
else if (playMode == PlayModeStateChange.ExitingPlayMode)
|
||||
{
|
||||
_paramTable?.Dispose();
|
||||
ActorTableName?.Dispose();
|
||||
_paramTable = null;
|
||||
ActorTableName = null;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public static void SwitchLanguage(string localeCode)
|
||||
{
|
||||
@@ -25,6 +60,39 @@ namespace AibisDream.Framework
|
||||
Debug.LogWarning($"Locale with code {localeCode} not found.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本地化参数
|
||||
/// </summary>
|
||||
/// <param name="param">参数名</param>
|
||||
/// <param name="table">本地化表</param>
|
||||
/// <returns></returns>
|
||||
public static string LocalizeParam(string param, LocalizationTable table)
|
||||
{
|
||||
// 非本地化参数直接返回
|
||||
if (!param.StartsWith(LocalizationPrefix)) return param;
|
||||
|
||||
var localizationKey = param[LocalizationPrefix.Length..];
|
||||
// 获取本地化值
|
||||
if (table.TryGetValue(localizationKey, out var value))
|
||||
{
|
||||
return value.LocalizedValue;
|
||||
}
|
||||
|
||||
// 如果找不到,则使用原始值
|
||||
Debug.LogWarning($"Variable Key '{localizationKey}' not found in localization table.");
|
||||
return localizationKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本地化参数
|
||||
/// </summary>
|
||||
/// <param name="param"></param>
|
||||
/// <returns></returns>
|
||||
public static string LocalizeParam(string param)
|
||||
{
|
||||
return LocalizeParam(param, _paramTable);
|
||||
}
|
||||
}
|
||||
|
||||
public class LocalizationTable
|
||||
|
||||
Reference in New Issue
Block a user