mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
LocalizeText
LocalizeText
This commit is contained in:
28
Assets/TEngine/Editor/UI/LocalizeTextEditor.cs
Normal file
28
Assets/TEngine/Editor/UI/LocalizeTextEditor.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Editor/UI/LocalizeTextEditor.cs.meta
Normal file
11
Assets/TEngine/Editor/UI/LocalizeTextEditor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67ec644b4e4c8a8438b4127655440640
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Assets/TEngine/Runtime/Localize.meta
Normal file
8
Assets/TEngine/Runtime/Localize.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7705658e744e8ee47b4575cb0df197b5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
127
Assets/TEngine/Runtime/Localize/LocalizeMgr.cs
Normal file
127
Assets/TEngine/Runtime/Localize/LocalizeMgr.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Localize/LocalizeMgr.cs.meta
Normal file
11
Assets/TEngine/Runtime/Localize/LocalizeMgr.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39e880034a65e7d40a1f190b90b29e67
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
29
Assets/TEngine/Runtime/Localize/LocalizeText.cs
Normal file
29
Assets/TEngine/Runtime/Localize/LocalizeText.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Localize/LocalizeText.cs.meta
Normal file
11
Assets/TEngine/Runtime/Localize/LocalizeText.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11e68aa9889a748498a30d2c31d885de
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user