LocalizeText

LocalizeText
This commit is contained in:
ALEXTANG
2022-05-25 20:19:52 +08:00
parent d478617453
commit 086e2caaf6
7 changed files with 225 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
using UnityEditor;
using UnityEditor.UI;
namespace TEngine
{
[CustomEditor(typeof(LocalizeText), true)]
[CanEditMultipleObjects]
public class LocalizeTextEditor : TextEditor
{
public SerializedProperty Key;
protected override void OnEnable()
{
UnityEngine.Debug.Log(serializedObject.FindProperty("Key"));
base.OnEnable();
Key = serializedObject.FindProperty("Key");
}
public override void OnInspectorGUI()
{
EditorGUILayout.Space();
serializedObject.Update();
EditorGUILayout.PropertyField(Key);
serializedObject.ApplyModifiedProperties();
base.OnInspectorGUI();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 67ec644b4e4c8a8438b4127655440640
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7705658e744e8ee47b4575cb0df197b5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
namespace TEngine
{
public enum Language
{
Chinese = 1,
English = 2,
}
public struct LocalizationId
{
public int CurlocalizationId;
}
public class LocalizeConfigNode
{
public string Chinese;
public string EngLish;
}
public class LocalizeMgr : TSingleton<LocalizeMgr>
{
private int _curLanguage;
private Dictionary<int, string> _localDic = new Dictionary<int, string>();
const string LocalizeIdPath = "Localization/LocalizationId.json";
const string ConfigPath = "Localization/Localization.json";
public void InitLocalize()
{
_localDic.Clear();
string localizeIdJson = ResMgr.Instance.GetStringFromAsset(LocalizeIdPath);
string localizeConfigJson = ResMgr.Instance.GetStringFromAsset(ConfigPath);
if (localizeIdJson == null)
{
TLogger.LogError("当前国际化地区配置不存在,请检查配置");
return;
}
if (localizeConfigJson == null)
{
TLogger.LogError("国际化配置表找不到Json文件请检查配置");
return;
}
LocalizationId id = JsonUtility.FromJson<LocalizationId>(localizeIdJson);
_curLanguage = id.CurlocalizationId;
SetLanguage(id.CurlocalizationId);
Dictionary<int, LocalizeConfigNode> dic = DeserializeStringToDictionary<int, LocalizeConfigNode>(localizeConfigJson);
foreach (var item in dic)
{
switch ((Language)_curLanguage)
{
case Language.Chinese:
_localDic.Add(item.Key, item.Value.Chinese);
break;
case Language.English:
_localDic.Add(item.Key, item.Value.EngLish);
break;
default:
break;
}
}
}
public void SetLanguage(int type)
{
_curLanguage = type;
}
public string GetLocalizeStr(int localizeKey)
{
if (localizeKey <= 0)
{
TLogger.LogError($"国际化表内未配置key为 {localizeKey},请检查配置");
return null;
}
if (_localDic[localizeKey] == null || _localDic[localizeKey] == "")
{
TLogger.LogError($"当前语言为: {(Language)_curLanguage},国际化表内未配置key为 {localizeKey} 的value请检查配置");
return null;
}
return _localDic[localizeKey];
}
public int GetLocalId(string _value)
{
foreach (var item in _localDic)
{
if (item.Value == _value)
{
return item.Key;
}
}
return 0;
}
public int GetCurLanguage()
{
return (int)_curLanguage;
}
private static Dictionary<TKey, TValue> DeserializeStringToDictionary<TKey, TValue>(string jsonStr)
{
if (string.IsNullOrEmpty(jsonStr))
{
return new Dictionary<TKey, TValue>();
}
Dictionary<TKey, TValue> jsonDict = JsonConvert.DeserializeObject<Dictionary<TKey, TValue>>(jsonStr);
return jsonDict;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 39e880034a65e7d40a1f190b90b29e67
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
using UnityEngine;
using UnityEngine.UI;
namespace TEngine
{
public class LocalizeText:Text
{
[SerializeField] public int Key;
public string SetText(int key)
{
return LocalizeMgr.Instance.GetLocalizeStr(key);
}
protected override void Start()
{
base.Start();
if (string.IsNullOrEmpty(this.text))
{
Key = LocalizeMgr.Instance.GetLocalId(this.text);
if (Key <= 0)
{
return;
}
text = SetText(Key);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 11e68aa9889a748498a30d2c31d885de
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: