From faf6e27ce734eb7bea95148d9388c105ca913fc5 Mon Sep 17 00:00:00 2001 From: maoling Date: Sun, 21 Mar 2021 10:59:45 +0800 Subject: [PATCH] github issue: How to change language? #8. Fix: add language selection in BlockResSettings --- Source/Script/Core/Blockly.cs | 2 +- Source/Script/I18n/I18n.cs | 6 +- Source/Script/Res/BlockResMgr.cs | 66 +++++++++++++--------- UserData/Resources/BlockResSettings.asset | Bin 9852 -> 9920 bytes 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/Source/Script/Core/Blockly.cs b/Source/Script/Core/Blockly.cs index 22455be..a9bcb89 100644 --- a/Source/Script/Core/Blockly.cs +++ b/Source/Script/Core/Blockly.cs @@ -31,7 +31,7 @@ public class Blockly /// public static void Init() { - BlockResMgr.Get().LoadI18n(I18n.EN); + BlockResMgr.Get().LoadI18n(); BlockResMgr.Get().LoadJsonDefinitions(); } diff --git a/Source/Script/I18n/I18n.cs b/Source/Script/I18n/I18n.cs index a4449c9..e50caa2 100644 --- a/Source/Script/I18n/I18n.cs +++ b/Source/Script/I18n/I18n.cs @@ -32,7 +32,7 @@ public class I18n public static string Get(string key) { if (mMsg == null) - BlockResMgr.Get().LoadI18n(I18n.EN); + BlockResMgr.Get().LoadI18n(); string value; if (mMsg.TryGetValue(key, out value)) @@ -44,7 +44,7 @@ public static string Get(string key) public static void Add(string key, string value) { if (mMsg == null) - BlockResMgr.Get().LoadI18n(I18n.EN); + BlockResMgr.Get().LoadI18n(); mMsg[key] = value; } @@ -52,7 +52,7 @@ public static void Add(string key, string value) public static bool Contains(string key) { if (mMsg == null) - BlockResMgr.Get().LoadI18n(I18n.EN); + BlockResMgr.Get().LoadI18n(); return mMsg.ContainsKey(key); } diff --git a/Source/Script/Res/BlockResMgr.cs b/Source/Script/Res/BlockResMgr.cs index 9e18bcd..2728ca9 100644 --- a/Source/Script/Res/BlockResMgr.cs +++ b/Source/Script/Res/BlockResMgr.cs @@ -57,6 +57,12 @@ public class BlockTextResParam : BlockResParam { public TextAsset TextFile; } + + [Serializable] + public class BlockTextResWithSelectionParam : BlockTextResParam + { + public bool Selected; + } /// /// manage all resources. @@ -66,7 +72,7 @@ public class BlockTextResParam : BlockResParam public class BlockResMgr : ScriptableObject { [SerializeField] private BlockResLoadType m_LoadType; - [SerializeField] private List m_I18nFiles; + [SerializeField] private List m_I18nFiles; [SerializeField] private List m_BlockJsonFiles; [SerializeField] private List m_ToolboxFiles; @@ -115,7 +121,7 @@ public void SetAssetbundleUnloadDelegate(Action del) #region I18n Files - public void LoadI18n(string language) + public void LoadI18n() { if (m_I18nFiles == null || m_I18nFiles.Count == 0) { @@ -123,32 +129,40 @@ public void LoadI18n(string language) return; } - foreach (BlockTextResParam resParam in m_I18nFiles) + var i18nSelected = m_I18nFiles.FindAll(file => file.Selected); + if (i18nSelected.Count == 0) { - if (language.Equals(resParam.IndexName)) - { - TextAsset textAsset = null; - switch (m_LoadType) - { - case BlockResLoadType.Assetbundle: - if (mABSyncLoad != null) - textAsset = mABSyncLoad(resParam.ResName) as TextAsset; - break; - case BlockResLoadType.Resources: - textAsset = Resources.Load(resParam.ResName); - break; - case BlockResLoadType.Serialized: - textAsset = resParam.TextFile; - break; - } - if (textAsset != null) - { - I18n.AddI18nFile(textAsset.text); - if (m_LoadType == BlockResLoadType.Assetbundle && mABUnload != null) - mABUnload(resParam.ResName); - } - } + Debug.LogWarning("Please select an i18n file in BlockResSettings.asset. Default select \'en\'."); + i18nSelected.Add(m_I18nFiles.Find(file => file.IndexName == "en")); + } + else if (i18nSelected.Count > 1) + { + Debug.LogWarning("You have selected more than one i18n files in BlockResSettings.asset. The first one will be used."); + } + + var resParam = i18nSelected[0]; + TextAsset textAsset = null; + switch (m_LoadType) + { + case BlockResLoadType.Assetbundle: + if (mABSyncLoad != null) + textAsset = mABSyncLoad(resParam.ResName) as TextAsset; + break; + case BlockResLoadType.Resources: + textAsset = Resources.Load(resParam.ResName); + break; + case BlockResLoadType.Serialized: + textAsset = resParam.TextFile; + break; } + if (textAsset != null) + { + I18n.AddI18nFile(textAsset.text); + if (m_LoadType == BlockResLoadType.Assetbundle && mABUnload != null) + mABUnload(resParam.ResName); + } + + Debug.Log("Select I18n: " + resParam.IndexName); } #endregion diff --git a/UserData/Resources/BlockResSettings.asset b/UserData/Resources/BlockResSettings.asset index 442d48a7d526bf075a5edad38f9b7b92319dfe04..dfbd920dae6f6a572d8cbde25be83aa242fa7d01 100644 GIT binary patch delta 942 zcmZvbO;1xn6o${-TZ}1?P?~U4cPtb{6pA8(inM%xO3O!4w561bt)eyBh3+m}R^adh zaN!RyBqVG|S@0u>2?;AUtXP}iId8r62AE`$IcJ_Z_c=4Uf5(dBHAJ;1M0bir6d(%H zMonv7=Ei2zkNo;xDV5dV&H0MYyt%3m%}9ff zwoG!?I95Cxc~V5yiZm~O@jT~oSMM8^YSIy(6}~474@D-J_e72`&60E^@1r5EndDIN z_jSE#sR#O}&r&TqW?G?E-jT1lWcEK92H6JkA;?D_63!e~@od+}T=TI`S6HDAFb-EM zIqz^R-+cn-PB?dQPR3>Dr;>EaCa&`{m^_C`H%wmObeVLWdq74&MnU#^NUw7rocrNC z0H)+DcXhzAI1j>kNGrn%4`0+VDaMv^&vI)c7kNd0czw!K&W_^j82TOOoDAt#8261P zBIde)Udb?PvbM7fIgG_py1)bn>wkH&J%&xQX$G9m`&jfboP&8}7(k4eMdp0K-NYHc93&!?)<$jK1&C zw*@utXYmbNLAHU6fNT$tLBsbj?0{h>n6#m8=1|^h*agE6Y-E$&9LaUup1W#>l5#)G zki8&3>MT`#@~6j;4Q5ql(rW$s>D@c~`~L*z0XPrBSu_4t)yJQ12;?xx5s;$+@|WB% zEb?^!W0-Cn(@lU$Pp8gT66!Mv=P5XA2R;8gPwS`m(D^e=W?&MB$*h2nInRNd2e|-p kF+c{Lm*Bjt&Sr(nLS38)weWdp*PJAm^1@G&iEu~w7Q2VPQ~&?~