Skip to content

Commit

Permalink
localize text group 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
KorStrix committed Dec 20, 2021
1 parent 9808d8b commit 15b3568
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 13 deletions.
14 changes: 11 additions & 3 deletions Runtime/Component/LocalizeComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public void SetLanguageKey(string key)

_languageKey = key;
_languageParam = Array.Empty<string>();
OnChangeLanguage(s_manager.CurrentLanguage);

if (s_manager != null)
{
OnChangeLanguage(s_manager.CurrentLanguage);
}
}

public void SetLanguageKeyWithParam(string key, params string[] param)
Expand All @@ -48,7 +52,11 @@ public void SetLanguageKeyWithParam(string key, params string[] param)

_languageKey = key;
_languageParam = param;
OnChangeLanguage(s_manager.CurrentLanguage);

if (s_manager != null)
{
OnChangeLanguage(s_manager.CurrentLanguage);
}
}

public void UpdateLocalize()
Expand Down Expand Up @@ -84,7 +92,7 @@ IEnumerator OnEnableCoroutine()
OnSetup();
}

void OnDisable()
protected virtual void OnDisable()
{
if (s_isAppQuitting)
return;
Expand Down
17 changes: 10 additions & 7 deletions Runtime/Component/LocalizeText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ protected override void OnChangeLanguage(SystemLanguage language)
}
}

protected override void OnDisable()
{
base.OnDisable();

if (s_manager != null)
{
s_manager.OnChangeFont -= OnChangeFont;
}
}

void OnChangeFont(Font font)
{
TextComponent.font = font;
}
}

#if UNITY_EDITOR
// [CustomEditor(typeof(LocalizeText))]
// public abstract class LocalizeText_Inspector : LocalizeComponentBase_Inspector<LocalizeText>
// {
// }
#endif
}
135 changes: 135 additions & 0 deletions Runtime/Component/LocalizeTextGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UNKO.Localize
{
public class LocalizeTextGroup : LocalizeComponentBase
{
static string[] _dummyParam = new string[0];

[SerializeField]
int _groupIndex;

[SerializeField]
private Text _text; public Text TextComponent { get { Awake(); return _text; } }
public string Text { get => TextComponent.text; set => TextComponent.text = value; }

bool _isExecuteAwake = false;

public void Awake()
{
if (_isExecuteAwake)
{
return;
}
_isExecuteAwake = true;

if (_text == null)
_text = GetComponent<Text>();
}

public string GetGroupText()
=> GetGroupText(0);

public string GetGroupText(int groupIndex)
{
Awake();

string key = _languageKey;
if (groupIndex > 0)
{
key += $"_{groupIndex}";
}

_languageParam = _dummyParam;
return s_manager.GetLocalizeText(key);
}

public string GetGroupTextWithParam(params string[] param)
=> GetGroupTextWithParam(0, param);

public string GetGroupTextWithParam(int groupIndex, params string[] param)
{
Awake();

string key = _languageKey;
if (groupIndex > 0)
{
key += $"_{groupIndex}";
}

_languageParam = param;
return s_manager.GetLocalizeText(key, param);
}

public void SetGroupText()
=> SetGroupText(0);

public void SetGroupText(int groupIndex)
{
TextComponent.text = GetGroupText(groupIndex);
_groupIndex = groupIndex;
}

public void SetGroupTextWithParam(params string[] param)
=> SetGroupTextWithParam(0, param);

public void SetGroupTextWithParam(int groupIndex, params string[] param)
{
TextComponent.text = GetGroupTextWithParam(groupIndex, param);
_groupIndex = groupIndex;
}

protected override void OnSetup()
{
base.OnSetup();

s_manager.OnChangeFont -= OnChangeFont;
s_manager.OnChangeFont += OnChangeFont;

if (s_manager.TryGetFont(out Font font))
{
OnChangeFont(font);
}
}

protected override void OnChangeLanguage(SystemLanguage language)
{
if (string.IsNullOrEmpty(_languageKey))
return;

if (TextComponent == null)
{
return;
}

if (_languageParam.Length > 0)
{
TextComponent.text = s_manager.GetLocalizeText(_languageKey, _languageParam);
}
else
{
TextComponent.text = s_manager.GetLocalizeText(_languageKey);
}
}
protected override void OnDisable()
{
base.OnDisable();

if (s_manager != null)
{
s_manager.OnChangeFont -= OnChangeFont;
}
}

void OnChangeFont(Font font)
{
TextComponent.font = font;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Component/LocalizeTextGroup.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions Runtime/LocalizeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ public ILocalizeManager AddData(IEnumerable<ILocalizeData> datas)
public ILocalizeManager AddFontData(IEnumerable<ILocalizeFontData> datas)
{
foreach (ILocalizeFontData data in datas)
_fontDictionary.Add(data.GetLanguage(), data);
{
SystemLanguage language = data.GetLanguage();
if (_fontDictionary.TryGetValue(language, out var alreadyExistData))
{
Debug.LogError($"{nameof(LocalizeManager)} {nameof(AddFontData)} already contain font, language:{language}, font:{alreadyExistData.GetFont().name}, other font:{data.GetFont().name}");
continue;
}
_fontDictionary.Add(language, data);
}

return this;
}
Expand All @@ -36,8 +44,10 @@ public ILocalizeManager ChangeLanguage(SystemLanguage language)
_currentLanguage = language;
OnChangeLanguage?.Invoke(language);

TryGetFont(out Font font);
OnChangeFont?.Invoke(font);
if (TryGetFont(out Font font))
{
OnChangeFont?.Invoke(font);
}

return this;
}
Expand Down

0 comments on commit 15b3568

Please sign in to comment.