You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.7 KiB
C#
86 lines
2.7 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
using ArabicSupport;
|
|
using System.Linq;
|
|
[RequireComponent(typeof(TextMeshProUGUI))]
|
|
public class LocalizedTextComponent : MonoBehaviour
|
|
{
|
|
public string localizationKey;
|
|
private TextMeshProUGUI tmp;
|
|
public bool forceRightAlignment = false;
|
|
public bool forceSpacingBool= false;
|
|
public Vector2 charLineSpacing;
|
|
private void Start()
|
|
{
|
|
tmp = GetComponent<TextMeshProUGUI>();
|
|
|
|
if (LanguageManager.Instance != null)
|
|
{
|
|
LanguageManager.Instance.Register(this);
|
|
UpdateText();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"LanguageManager is not initialized yet for {gameObject.name}. Skipping registration.");
|
|
}
|
|
}
|
|
void OnDestroy()
|
|
{
|
|
if (LanguageManager.Instance != null)
|
|
{
|
|
LanguageManager.Instance.Unregister(this);
|
|
}
|
|
}
|
|
|
|
//public void UpdateText()
|
|
//{
|
|
// if (LanguageManager.Instance == null)
|
|
// {
|
|
// Debug.LogWarning($"LanguageManager not available when updating text for {gameObject.name}");
|
|
// return;
|
|
// }
|
|
|
|
// bool isArabic = LanguageManager.Instance.currentLanguage == "Arabic";
|
|
// string rawText = LanguageManager.Instance.GetLocalizedText(localizationKey);
|
|
|
|
// tmp.text = isArabic ? ArabicFixer.Fix(rawText) : rawText;
|
|
// tmp.font = LanguageManager.Instance.GetCurrentFont();
|
|
// //tmp.isRightToLeftText = isArabic;
|
|
// //tmp.alignment = isArabic ? TextAlignmentOptions.Right : TextAlignmentOptions.Left;
|
|
//}
|
|
|
|
public void UpdateText()
|
|
{
|
|
if (LanguageManager.Instance == null)
|
|
{
|
|
Debug.LogWarning($"LanguageManager not available when updating text for {gameObject.name}");
|
|
return;
|
|
}
|
|
|
|
bool isArabic = LanguageManager.Instance.currentLanguage == "Arabic";
|
|
string rawText = LanguageManager.Instance.GetLocalizedText(localizationKey);
|
|
|
|
if (isArabic)
|
|
{
|
|
// Split by \n and fix each line
|
|
string[] lines = rawText.Split(new[] { "\\n", "\n" }, System.StringSplitOptions.None);
|
|
tmp.text = string.Join("\n", lines.Select(ArabicFixer.Fix));
|
|
}
|
|
else
|
|
{
|
|
tmp.text = rawText.Replace("\\n", "\n"); // Handle escaped newline
|
|
}
|
|
if (forceRightAlignment && isArabic)
|
|
tmp.alignment = TextAlignmentOptions.Right;
|
|
|
|
if(forceSpacingBool && isArabic)
|
|
{
|
|
tmp.characterSpacing = charLineSpacing.x;
|
|
tmp.lineSpacing = charLineSpacing.y;
|
|
}
|
|
tmp.font = LanguageManager.Instance.GetCurrentFont();
|
|
tmp.ForceMeshUpdate();
|
|
}
|
|
|
|
}
|