160 lines
5.2 KiB
C#
160 lines
5.2 KiB
C#
using System.Collections;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using UnityEngine.Localization;
|
|
using UnityEngine.Localization.Settings;
|
|
#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.";
|
|
|
|
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
|
|
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)
|
|
{
|
|
LocalizationSettings.SelectedLocaleChanged += OnLocaleChanged;
|
|
RefreshCurrentLanguageCodeAsync();
|
|
}
|
|
else if (playMode == PlayModeStateChange.ExitingPlayMode)
|
|
{
|
|
LocalizationSettings.SelectedLocaleChanged -= OnLocaleChanged;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
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.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本地化参数
|
|
/// </summary>
|
|
/// <param name="param">参数名</param>
|
|
/// <returns></returns>
|
|
public static string LocalizeParam(string param)
|
|
{
|
|
// 非本地化参数直接返回
|
|
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)
|
|
{
|
|
// 本地化参数则去除前缀,非本地化参数直接返回
|
|
return key.StartsWith(LocalizationPrefix) ? key[LocalizationPrefix.Length..] : key;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 确保本地化系统已初始化
|
|
/// </summary>
|
|
/// <returns>初始化操作</returns>
|
|
public static async void EnsureInitialized()
|
|
{
|
|
await LocalizationSettings.InitializationOperation.Task;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 等待本地化系统初始化完成(含 SettingLoader 触发的语言切换)。
|
|
/// </summary>
|
|
public static IEnumerator WaitUntilReady()
|
|
{
|
|
yield return LocalizationSettings.InitializationOperation;
|
|
yield return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 等待 LocalizeStringEvent 等 UI 组件刷新文案。
|
|
/// </summary>
|
|
public static IEnumerator WaitForUIRefresh()
|
|
{
|
|
yield return null;
|
|
Canvas.ForceUpdateCanvases();
|
|
}
|
|
}
|
|
} |