Ver.0.3.0.33
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Localization;
|
||||
using UnityEngine.Localization.Settings;
|
||||
using UnityEngine.Localization.Tables;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
public static class LocalizationKit
|
||||
{
|
||||
private const string LocalizationPrefix = "l10n.";
|
||||
private const string ActorTableName = "ActorName";
|
||||
private const string ParamTableName = "Params";
|
||||
|
||||
private static LocalizationTable _paramTable;
|
||||
|
||||
public static LocalizationTable ActorTable { get; private set; }
|
||||
|
||||
static LocalizationKit()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playModeStateChanged += OnPlayModeChanged;
|
||||
#else
|
||||
_paramTable = new LocalizationTable(ParamTableName);
|
||||
ActorTable = new LocalizationTable(ActorTableName);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private static void OnPlayModeChanged(PlayModeStateChange playMode)
|
||||
{
|
||||
if (playMode == PlayModeStateChange.EnteredPlayMode)
|
||||
{
|
||||
// 进入游戏模式的时候
|
||||
_paramTable?.Dispose();
|
||||
ActorTable?.Dispose();
|
||||
_paramTable = new LocalizationTable(ParamTableName);
|
||||
ActorTable = new LocalizationTable(ActorTableName);
|
||||
}
|
||||
else if (playMode == PlayModeStateChange.ExitingPlayMode)
|
||||
{
|
||||
_paramTable?.Dispose();
|
||||
ActorTable?.Dispose();
|
||||
_paramTable = null;
|
||||
ActorTable = null;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
public static void SwitchLanguage(string localeCode)
|
||||
{
|
||||
var selectedLocale =
|
||||
LocalizationSettings.AvailableLocales.Locales.Find(locale => locale.Identifier.Code == localeCode);
|
||||
|
||||
if (selectedLocale)
|
||||
{
|
||||
LocalizationSettings.SelectedLocale = selectedLocale;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 static string GetL10NParamKey(string key)
|
||||
{
|
||||
// 本地化参数则去除前缀,非本地化参数直接返回
|
||||
return key.StartsWith(LocalizationPrefix) ? key[LocalizationPrefix.Length..] : key;
|
||||
}
|
||||
}
|
||||
|
||||
public class LocalizationTable
|
||||
{
|
||||
public string TableName { get; }
|
||||
|
||||
private StringTable _stringTable;
|
||||
|
||||
public LocalizationTable(string tableName)
|
||||
{
|
||||
TableName = tableName;
|
||||
_stringTable = LocalizationSettings.StringDatabase.GetTable(TableName);
|
||||
LocalizationSettings.SelectedLocaleChanged += OnLocaleChanged;
|
||||
|
||||
Debug.Log($"表格{_stringTable.TableCollectionName}已加载");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
LocalizationSettings.SelectedLocaleChanged -= OnLocaleChanged;
|
||||
_stringTable = null;
|
||||
}
|
||||
|
||||
private void OnLocaleChanged(Locale locale)
|
||||
{
|
||||
_stringTable = LocalizationSettings.StringDatabase.GetTable(TableName);
|
||||
}
|
||||
|
||||
public StringTableEntry this[string key] => _stringTable[key];
|
||||
|
||||
public bool TryGetValue(string key, out StringTableEntry value)
|
||||
{
|
||||
value = _stringTable.GetEntry(key);
|
||||
return value != null;
|
||||
}
|
||||
|
||||
public LocaleIdentifier LocaleIdentifier => _stringTable.LocaleIdentifier;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user