You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.8 KiB
139 lines
3.8 KiB
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class ExtendText : MonoBehaviour
|
|
{
|
|
[SerializeField] TextMeshProUGUI m_text;
|
|
[SerializeField] bool isAutoInjectLocalization = true;
|
|
|
|
SystemLanguage backupTextLanguage = SystemLanguage.Catalan;
|
|
string backupText;
|
|
|
|
public RectTransform rectTransform => transform as RectTransform;
|
|
public TextMeshProUGUI TextComponent => m_text;
|
|
public bool IsAutoInjectLocalization
|
|
{
|
|
get => isAutoInjectLocalization;
|
|
set
|
|
{
|
|
if(isAutoInjectLocalization == value) return;
|
|
|
|
isAutoInjectLocalization = value;
|
|
|
|
if(isAutoInjectLocalization)
|
|
{
|
|
InjectLocalization(m_text.text);
|
|
LocalizationText.OnLanguageChange += HandleLanguageChange;
|
|
}
|
|
else
|
|
{
|
|
LocalizationText.OnLanguageChange -= HandleLanguageChange;
|
|
backupTextLanguage = SystemLanguage.Catalan;
|
|
m_text.text = backupText;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if(m_text is null)
|
|
m_text = GetComponent<TextMeshProUGUI>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if(isAutoInjectLocalization)
|
|
{
|
|
if(backupTextLanguage != LocalizationText.Language)
|
|
InjectLocalization(backupText != null ? backupText : m_text.text);
|
|
|
|
LocalizationText.OnLanguageChange += HandleLanguageChange;
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if(isAutoInjectLocalization)
|
|
LocalizationText.OnLanguageChange -= HandleLanguageChange;
|
|
}
|
|
|
|
private void HandleLanguageChange() => InjectLocalization(backupText);
|
|
|
|
private void InjectLocalization(string targetText)
|
|
{
|
|
backupText = targetText;
|
|
backupTextLanguage = LocalizationText.Language;
|
|
m_text.text = LocalizationInjector.InjectLocalization(targetText);
|
|
}
|
|
|
|
public void SetText(string text)
|
|
{
|
|
if(isAutoInjectLocalization)
|
|
InjectLocalization(text);
|
|
else
|
|
m_text.text = text;
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
if(m_text is null)
|
|
m_text = GetComponent<TextMeshProUGUI>();
|
|
}
|
|
}
|
|
|
|
public static class LocalizationInjector
|
|
{
|
|
public static bool IsValidateText(string targetText)
|
|
{
|
|
int openBracketCount = 0;
|
|
for (int i = 0; i < targetText.Length; i++)
|
|
{
|
|
if (targetText[i] == '[')
|
|
openBracketCount++;
|
|
else if (targetText[i] == ']')
|
|
openBracketCount--;
|
|
|
|
if (openBracketCount < 0) return false;
|
|
}
|
|
|
|
return openBracketCount == 0;
|
|
}
|
|
|
|
public static string InjectLocalization(string targetText)
|
|
{
|
|
#if UNITY_EDITOR // validation check, this is not necessary in build
|
|
if (!IsValidateText(targetText)) return "Invalid Localization Text";
|
|
#endif
|
|
|
|
Utility.StringBuilder.Clear();
|
|
|
|
for (int i = 0; i < targetText.Length; i++)
|
|
{
|
|
if (targetText[i] == '[')
|
|
{
|
|
string keyWord = string.Empty;
|
|
for (++i; i < targetText.Length; i++)
|
|
{
|
|
if (targetText[i] == ']')
|
|
{
|
|
Utility.StringBuilder.Append(LocalizationText.GetText(keyWord));
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
keyWord += targetText[i];
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Utility.StringBuilder.Append(targetText[i]);
|
|
}
|
|
}
|
|
|
|
return Utility.StringBuilder.ToString();
|
|
}
|
|
|
|
public static string MakeToLocalizationKey(string targetText) => FormatString.StringFormat("[{0}]", targetText);
|
|
}
|
|
|