Merge branch 'feature/升级Yarn3.2' into feature/Yarn3.0升级合入
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using AibisDream.Framework;
|
||||
using AibisDream.Framework;
|
||||
|
||||
namespace AibisDream.Kit
|
||||
{
|
||||
@@ -6,6 +6,8 @@ namespace AibisDream.Kit
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Cn { get; set; }
|
||||
public string En { get; set; }
|
||||
public string Ja { get; set; }
|
||||
public string NameColor { get; set; }
|
||||
public string HeadPicPath { get; set; }
|
||||
public string VoicePath { get; set; }
|
||||
@@ -22,6 +24,8 @@ namespace AibisDream.Kit
|
||||
CharacterVo vo;
|
||||
vo.key = Key;
|
||||
vo.cn = Cn;
|
||||
vo.en = En;
|
||||
vo.ja = Ja;
|
||||
vo.nameColor = NameColor;
|
||||
vo.headPicPath = HeadPicPath;
|
||||
vo.voicePath = VoicePath;
|
||||
@@ -37,6 +41,8 @@ namespace AibisDream.Kit
|
||||
{
|
||||
public string key;
|
||||
public string cn;
|
||||
public string en;
|
||||
public string ja;
|
||||
public string nameColor;
|
||||
public string headPicPath;
|
||||
public string voicePath;
|
||||
@@ -58,14 +64,27 @@ namespace AibisDream.Kit
|
||||
}
|
||||
else
|
||||
{
|
||||
// 人名能找到
|
||||
var entry = LocalizationKit.ActorTable[key];
|
||||
rawName = entry == null ? key : entry.LocalizedValue;
|
||||
rawName = GetLocalizedNameByKind(LocalizationKit.CurrentLocaleKind);
|
||||
}
|
||||
|
||||
return string.IsNullOrEmpty(nameColor) ? rawName : $"<color={nameColor}>{rawName}</color>";
|
||||
}
|
||||
|
||||
private string GetLocalizedNameByKind(LocaleKind kind)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
case LocaleKind.En:
|
||||
return string.IsNullOrWhiteSpace(en) ? key : en;
|
||||
case LocaleKind.Ja:
|
||||
if (!string.IsNullOrWhiteSpace(ja)) return ja;
|
||||
return string.IsNullOrWhiteSpace(cn) ? key : cn;
|
||||
case LocaleKind.Cn:
|
||||
default:
|
||||
return string.IsNullOrWhiteSpace(cn) ? key : cn;
|
||||
}
|
||||
}
|
||||
|
||||
public string VoicePath
|
||||
{
|
||||
get
|
||||
|
||||
@@ -13,8 +13,10 @@ namespace AibisDream.Kit
|
||||
public class ConfigUtil : SingletonBase<ConfigUtil>
|
||||
{
|
||||
private const string CharacterConfigPath = "/Config/character.csv";
|
||||
private const string ParamConfigPath = "/Config/params.csv";
|
||||
|
||||
private Dictionary<string, Character> _characters;
|
||||
private Dictionary<string, Param> _params;
|
||||
|
||||
/// <summary>
|
||||
/// 用于单例模式
|
||||
@@ -39,12 +41,23 @@ namespace AibisDream.Kit
|
||||
public override void OnSingletonInit()
|
||||
{
|
||||
InitCharacterConfig();
|
||||
InitParamConfig();
|
||||
}
|
||||
|
||||
private void InitCharacterConfig()
|
||||
{
|
||||
var characterList = CsvUtil.ReadAsBean<Character>(Application.streamingAssetsPath + CharacterConfigPath);
|
||||
_characters = characterList.ToDictionary(item => item.Key, item => item);
|
||||
_characters = characterList
|
||||
.Where(item => !string.IsNullOrWhiteSpace(item.Key))
|
||||
.ToDictionary(item => item.Key, item => item);
|
||||
}
|
||||
|
||||
private void InitParamConfig()
|
||||
{
|
||||
var paramList = CsvUtil.ReadAsBean<Param>(Application.streamingAssetsPath + ParamConfigPath);
|
||||
_params = paramList
|
||||
.Where(item => !string.IsNullOrWhiteSpace(item.Key))
|
||||
.ToDictionary(item => item.Key, item => item);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,6 +72,17 @@ namespace AibisDream.Kit
|
||||
return _characters.TryGetValue(key, out character);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按key获取参数本地化配置
|
||||
/// </summary>
|
||||
/// <param name="key">key</param>
|
||||
/// <param name="param">参数配置</param>
|
||||
/// <returns>是否能获取</returns>
|
||||
public bool TryGetParam(string key, out Param param)
|
||||
{
|
||||
return _params.TryGetValue(key, out param);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按类型读取Json配置
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using AibisDream.Framework;
|
||||
|
||||
namespace AibisDream.Kit
|
||||
{
|
||||
public class Param
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Cn { get; set; }
|
||||
public string En { get; set; }
|
||||
public string Ja { get; set; }
|
||||
|
||||
public string LocalizeByKind(LocaleKind kind)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
case LocaleKind.En:
|
||||
return string.IsNullOrWhiteSpace(En) ? Key : En;
|
||||
case LocaleKind.Ja:
|
||||
if (!string.IsNullOrWhiteSpace(Ja)) return Ja;
|
||||
return string.IsNullOrWhiteSpace(Cn) ? Key : Cn;
|
||||
case LocaleKind.Cn:
|
||||
default:
|
||||
return string.IsNullOrWhiteSpace(Cn) ? Key : Cn;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c12e0d2c486f4ea6a5d4f70878b6e7c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,59 +1,83 @@
|
||||
using UnityEngine;
|
||||
using AibisDream.Kit;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Localization;
|
||||
using UnityEngine.Localization.Settings;
|
||||
using UnityEngine.Localization.Tables;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
/// <summary>
|
||||
/// csv 本地化列类别。集中声明"哪些 Locale 归到哪一列",csv 数据类按此枚举取字段。
|
||||
/// </summary>
|
||||
public enum LocaleKind
|
||||
{
|
||||
Cn,
|
||||
En,
|
||||
Ja
|
||||
}
|
||||
|
||||
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; }
|
||||
|
||||
public static string CurrentLanguageCode { get; private set; } = "zh-Hans";
|
||||
public static Locale CurrentLocale => LocalizationSettings.SelectedLocale;
|
||||
|
||||
/// <summary>
|
||||
/// 把 LocaleCode 映射到 csv 列类别。集中在此一处,便于扩展(加新语言时修改这里 + LocaleKind 枚举 + 数据类字段)。
|
||||
/// </summary>
|
||||
public static LocaleKind GetLocaleKind(string localeCode)
|
||||
{
|
||||
if (string.IsNullOrEmpty(localeCode)) return LocaleKind.Cn;
|
||||
if (localeCode.StartsWith("en")) return LocaleKind.En;
|
||||
if (localeCode.StartsWith("ja")) return LocaleKind.Ja;
|
||||
return LocaleKind.Cn;
|
||||
}
|
||||
|
||||
public static LocaleKind CurrentLocaleKind => GetLocaleKind(CurrentLanguageCode);
|
||||
|
||||
static LocalizationKit()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
EditorApplication.playModeStateChanged += OnPlayModeChanged;
|
||||
#else
|
||||
_paramTable = new LocalizationTable(ParamTableName);
|
||||
ActorTable = new LocalizationTable(ActorTableName);
|
||||
LocalizationSettings.SelectedLocaleChanged += OnLocaleChanged;
|
||||
RefreshCurrentLanguageCodeAsync();
|
||||
#endif
|
||||
}
|
||||
|
||||
private static void OnLocaleChanged(Locale locale)
|
||||
{
|
||||
UpdateCurrentLanguageCode(locale);
|
||||
EnumEventSystem.Global.Send(SettingChangeEvent.LocaleChanged, locale);
|
||||
}
|
||||
|
||||
private static void UpdateCurrentLanguageCode(Locale locale)
|
||||
{
|
||||
if (locale != null)
|
||||
{
|
||||
CurrentLanguageCode = locale.Identifier.Code;
|
||||
}
|
||||
}
|
||||
|
||||
private static async void RefreshCurrentLanguageCodeAsync()
|
||||
{
|
||||
await LocalizationSettings.InitializationOperation.Task;
|
||||
UpdateCurrentLanguageCode(LocalizationSettings.SelectedLocale);
|
||||
}
|
||||
|
||||
#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);
|
||||
LocalizationSettings.SelectedLocaleChanged += OnLocaleChanged;
|
||||
RefreshCurrentLanguageCodeAsync();
|
||||
}
|
||||
else if (playMode == PlayModeStateChange.ExitingPlayMode)
|
||||
{
|
||||
_paramTable?.Dispose();
|
||||
ActorTable?.Dispose();
|
||||
_paramTable = null;
|
||||
ActorTable = null;
|
||||
LocalizationSettings.SelectedLocaleChanged -= OnLocaleChanged;
|
||||
}
|
||||
}
|
||||
@@ -81,33 +105,22 @@ namespace AibisDream.Framework
|
||||
/// 本地化参数
|
||||
/// </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);
|
||||
// 非本地化参数直接返回
|
||||
if (string.IsNullOrEmpty(param)) return param;
|
||||
if (!param.StartsWith(LocalizationPrefix)) return param;
|
||||
|
||||
var localizationKey = param[LocalizationPrefix.Length..];
|
||||
if (ConfigUtil.Instance.TryGetParam(localizationKey, out var value))
|
||||
{
|
||||
return value.LocalizeByKind(CurrentLocaleKind);
|
||||
}
|
||||
|
||||
// 如果找不到,则使用原始值
|
||||
Debug.LogWarning($"Variable Key '{localizationKey}' not found in params csv.");
|
||||
return localizationKey;
|
||||
}
|
||||
|
||||
public static string GetL10NParamKey(string key)
|
||||
@@ -125,64 +138,4 @@ namespace AibisDream.Framework
|
||||
await LocalizationSettings.InitializationOperation.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public class LocalizationTable
|
||||
{
|
||||
public string TableName { get; }
|
||||
|
||||
private StringTable _stringTable;
|
||||
private bool _isInitialized;
|
||||
|
||||
public LocalizationTable(string tableName)
|
||||
{
|
||||
TableName = tableName;
|
||||
Debug.Log($"开始初始化表格{tableName}");
|
||||
InitializeAsync();
|
||||
}
|
||||
|
||||
private async void InitializeAsync()
|
||||
{
|
||||
// 等待本地化系统初始化完成
|
||||
await LocalizationSettings.InitializationOperation.Task;
|
||||
|
||||
_stringTable = LocalizationSettings.StringDatabase.GetTable(TableName);
|
||||
LocalizationSettings.SelectedLocaleChanged += OnLocaleChanged;
|
||||
_isInitialized = true;
|
||||
|
||||
Debug.Log($"表格{TableName}已成功加载");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isInitialized)
|
||||
{
|
||||
LocalizationSettings.SelectedLocaleChanged -= OnLocaleChanged;
|
||||
}
|
||||
_stringTable = null;
|
||||
_isInitialized = false;
|
||||
}
|
||||
|
||||
private void OnLocaleChanged(Locale locale)
|
||||
{
|
||||
if (_isInitialized)
|
||||
{
|
||||
_stringTable = LocalizationSettings.StringDatabase.GetTable(TableName);
|
||||
}
|
||||
}
|
||||
|
||||
public StringTableEntry this[string key] => _isInitialized ? _stringTable?[key] : null;
|
||||
|
||||
public bool TryGetValue(string key, out StringTableEntry value)
|
||||
{
|
||||
if (_isInitialized && _stringTable != null)
|
||||
{
|
||||
value = _stringTable.GetEntry(key);
|
||||
return value != null;
|
||||
}
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public LocaleIdentifier? LocaleIdentifier => _isInitialized ? _stringTable?.LocaleIdentifier : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user