91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using AlicizaX.Localization.Runtime;
|
|
using AlicizaX.Runtime;
|
|
using Newtonsoft.Json;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace AlicizaX.UI.Extension.Editor
|
|
{
|
|
internal class LocalizeItem
|
|
{
|
|
public string key;
|
|
public string ChineseSimplified;
|
|
public string English;
|
|
public string Japanese;
|
|
}
|
|
|
|
internal static class LocalizeStaticKey
|
|
{
|
|
static readonly string LocalizeStaticConfigPath = "Assets/AssetArt/Configs/json/tables_tblocalization.json";
|
|
|
|
private static bool IsLoaded = false;
|
|
|
|
private static List<LocalizeItem> LocalizeItems;
|
|
private static Dictionary<string, Dictionary<Language, string>> LocalizeDic;
|
|
|
|
|
|
/// <summary>
|
|
/// 获取所有静态Keys
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string[] GetAllStaticKeys()
|
|
{
|
|
LoadStaticKey();
|
|
List<string> keys = LocalizeItems.Select(v => v.key).ToList();
|
|
keys.Insert(0, "None");
|
|
return keys.ToArray();
|
|
}
|
|
|
|
public static string GetText(string key)
|
|
{
|
|
LoadStaticKey();
|
|
Language languageType = (Language)EditorPrefs.GetInt(LocalizationComponent.PrefsKey);
|
|
string value = key;
|
|
|
|
if (LocalizeDic.TryGetValue(key, out Dictionary<Language, string> dic))
|
|
{
|
|
dic.TryGetValue(languageType, out value);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
[MenuItem("开发工具/Force Reload多语言静态Key")]
|
|
public static void ForceReload()
|
|
{
|
|
IsLoaded = false;
|
|
LoadStaticKey();
|
|
}
|
|
|
|
static void LoadStaticKey()
|
|
{
|
|
if (IsLoaded) return;
|
|
if (!File.Exists(LocalizeStaticConfigPath))
|
|
{
|
|
Debug.LogError("静态多语言文本文件不存在!");
|
|
return;
|
|
}
|
|
|
|
TextAsset textAsset =
|
|
UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(LocalizeStaticConfigPath);
|
|
|
|
LocalizeItems = JsonConvert.DeserializeObject<System.Collections.Generic.List<LocalizeItem>>(textAsset.text);
|
|
|
|
LocalizeDic = new Dictionary<string, Dictionary<Language, string>>();
|
|
foreach (var v in LocalizeItems)
|
|
{
|
|
Dictionary<Language, string> valueDic = new Dictionary<Language, string>();
|
|
valueDic.Add(Language.ChineseSimplified, v.ChineseSimplified);
|
|
valueDic.Add(Language.English, v.English);
|
|
valueDic.Add(Language.Japanese, v.Japanese);
|
|
LocalizeDic.Add(v.key, valueDic);
|
|
}
|
|
|
|
IsLoaded = true;
|
|
}
|
|
}
|
|
}
|