Skip to content

Commit

Permalink
github issue: How to change language? #8. Fix: add language selection…
Browse files Browse the repository at this point in the history
… in BlockResSettings
  • Loading branch information
maoling committed Mar 21, 2021
1 parent 5236963 commit faf6e27
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Source/Script/Core/Blockly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Blockly
/// </summary>
public static void Init()
{
BlockResMgr.Get().LoadI18n(I18n.EN);
BlockResMgr.Get().LoadI18n();
BlockResMgr.Get().LoadJsonDefinitions();
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Script/I18n/I18n.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -44,15 +44,15 @@ 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;
}

public static bool Contains(string key)
{
if (mMsg == null)
BlockResMgr.Get().LoadI18n(I18n.EN);
BlockResMgr.Get().LoadI18n();

return mMsg.ContainsKey(key);
}
Expand Down
66 changes: 40 additions & 26 deletions Source/Script/Res/BlockResMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class BlockTextResParam : BlockResParam
{
public TextAsset TextFile;
}

[Serializable]
public class BlockTextResWithSelectionParam : BlockTextResParam
{
public bool Selected;
}

/// <summary>
/// manage all resources.
Expand All @@ -66,7 +72,7 @@ public class BlockTextResParam : BlockResParam
public class BlockResMgr : ScriptableObject
{
[SerializeField] private BlockResLoadType m_LoadType;
[SerializeField] private List<BlockTextResParam> m_I18nFiles;
[SerializeField] private List<BlockTextResWithSelectionParam> m_I18nFiles;
[SerializeField] private List<BlockTextResParam> m_BlockJsonFiles;
[SerializeField] private List<BlockTextResParam> m_ToolboxFiles;

Expand Down Expand Up @@ -115,40 +121,48 @@ public void SetAssetbundleUnloadDelegate(Action<string> del)

#region I18n Files

public void LoadI18n(string language)
public void LoadI18n()
{
if (m_I18nFiles == null || m_I18nFiles.Count == 0)
{
Debug.LogError("LoadI18n failed. Please assign i18n files to BlockResSettings.asset.");
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<TextAsset>(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<TextAsset>(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
Expand Down
Binary file modified UserData/Resources/BlockResSettings.asset
Binary file not shown.

0 comments on commit faf6e27

Please sign in to comment.