修复英文问题,添加遗漏本地化
This commit is contained in:
@@ -3,6 +3,7 @@ using UnityEngine;
|
||||
using UnityEngine.Localization;
|
||||
using UnityEngine.Localization.Settings;
|
||||
using UnityEngine.Localization.Tables;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
@@ -47,20 +48,23 @@ namespace AibisDream.Framework
|
||||
}
|
||||
#endif
|
||||
|
||||
public static void SwitchLanguage(string localeCode)
|
||||
{
|
||||
var selectedLocale =
|
||||
LocalizationSettings.AvailableLocales.Locales.Find(locale => locale.Identifier.Code == localeCode);
|
||||
public static async void SwitchLanguage(string localeCode)
|
||||
{
|
||||
// 等待本地化系统初始化完成
|
||||
await LocalizationSettings.InitializationOperation.Task;
|
||||
|
||||
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.");
|
||||
}
|
||||
if (selectedLocale)
|
||||
{
|
||||
LocalizationSettings.SelectedLocale = selectedLocale;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"Locale with code {localeCode} not found.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本地化参数
|
||||
@@ -100,6 +104,15 @@ namespace AibisDream.Framework
|
||||
// 本地化参数则去除前缀,非本地化参数直接返回
|
||||
return key.StartsWith(LocalizationPrefix) ? key[LocalizationPrefix.Length..] : key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确保本地化系统已初始化
|
||||
/// </summary>
|
||||
/// <returns>初始化操作</returns>
|
||||
public static async System.Threading.Tasks.Task EnsureInitialized()
|
||||
{
|
||||
await LocalizationSettings.InitializationOperation.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public class LocalizationTable
|
||||
@@ -107,36 +120,58 @@ namespace AibisDream.Framework
|
||||
public string TableName { get; }
|
||||
|
||||
private StringTable _stringTable;
|
||||
private bool _isInitialized = false;
|
||||
|
||||
public LocalizationTable(string tableName)
|
||||
{
|
||||
TableName = tableName;
|
||||
Debug.Log($"表格{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($"表格{_stringTable.TableCollectionName}已加载");
|
||||
Debug.Log($"表格{TableName}已成功加载");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
LocalizationSettings.SelectedLocaleChanged -= OnLocaleChanged;
|
||||
if (_isInitialized)
|
||||
{
|
||||
LocalizationSettings.SelectedLocaleChanged -= OnLocaleChanged;
|
||||
}
|
||||
_stringTable = null;
|
||||
_isInitialized = false;
|
||||
}
|
||||
|
||||
private void OnLocaleChanged(Locale locale)
|
||||
{
|
||||
_stringTable = LocalizationSettings.StringDatabase.GetTable(TableName);
|
||||
if (_isInitialized)
|
||||
{
|
||||
_stringTable = LocalizationSettings.StringDatabase.GetTable(TableName);
|
||||
}
|
||||
}
|
||||
|
||||
public StringTableEntry this[string key] => _stringTable[key];
|
||||
public StringTableEntry this[string key] => _isInitialized ? _stringTable?[key] : null;
|
||||
|
||||
public bool TryGetValue(string key, out StringTableEntry value)
|
||||
{
|
||||
value = _stringTable.GetEntry(key);
|
||||
return value != null;
|
||||
if (_isInitialized && _stringTable != null)
|
||||
{
|
||||
value = _stringTable.GetEntry(key);
|
||||
return value != null;
|
||||
}
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public LocaleIdentifier LocaleIdentifier => _stringTable.LocaleIdentifier;
|
||||
public LocaleIdentifier? LocaleIdentifier => _isInitialized ? _stringTable?.LocaleIdentifier : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user