diff --git a/Assets/TEngine/Editor/UI/LocalizeTextEditor.cs b/Assets/TEngine/Editor/UI/LocalizeTextEditor.cs new file mode 100644 index 00000000..f5989d83 --- /dev/null +++ b/Assets/TEngine/Editor/UI/LocalizeTextEditor.cs @@ -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(); + } + } +} diff --git a/Assets/TEngine/Editor/UI/LocalizeTextEditor.cs.meta b/Assets/TEngine/Editor/UI/LocalizeTextEditor.cs.meta new file mode 100644 index 00000000..5c9e7a07 --- /dev/null +++ b/Assets/TEngine/Editor/UI/LocalizeTextEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 67ec644b4e4c8a8438b4127655440640 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Localize.meta b/Assets/TEngine/Runtime/Localize.meta new file mode 100644 index 00000000..8603054c --- /dev/null +++ b/Assets/TEngine/Runtime/Localize.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7705658e744e8ee47b4575cb0df197b5 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Localize/LocalizeMgr.cs b/Assets/TEngine/Runtime/Localize/LocalizeMgr.cs new file mode 100644 index 00000000..b6d77101 --- /dev/null +++ b/Assets/TEngine/Runtime/Localize/LocalizeMgr.cs @@ -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 + { + private int _curLanguage; + private Dictionary _localDic = new Dictionary(); + + 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(localizeIdJson); + + _curLanguage = id.CurlocalizationId; + + SetLanguage(id.CurlocalizationId); + + Dictionary dic = DeserializeStringToDictionary(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 DeserializeStringToDictionary(string jsonStr) + { + if (string.IsNullOrEmpty(jsonStr)) + { + return new Dictionary(); + } + Dictionary jsonDict = JsonConvert.DeserializeObject>(jsonStr); + + return jsonDict; + } + } +} diff --git a/Assets/TEngine/Runtime/Localize/LocalizeMgr.cs.meta b/Assets/TEngine/Runtime/Localize/LocalizeMgr.cs.meta new file mode 100644 index 00000000..c04cc239 --- /dev/null +++ b/Assets/TEngine/Runtime/Localize/LocalizeMgr.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 39e880034a65e7d40a1f190b90b29e67 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Localize/LocalizeText.cs b/Assets/TEngine/Runtime/Localize/LocalizeText.cs new file mode 100644 index 00000000..3dce9eea --- /dev/null +++ b/Assets/TEngine/Runtime/Localize/LocalizeText.cs @@ -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); + } + } + } +} diff --git a/Assets/TEngine/Runtime/Localize/LocalizeText.cs.meta b/Assets/TEngine/Runtime/Localize/LocalizeText.cs.meta new file mode 100644 index 00000000..cad49e59 --- /dev/null +++ b/Assets/TEngine/Runtime/Localize/LocalizeText.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 11e68aa9889a748498a30d2c31d885de +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: