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; private void Start() { tmp = GetComponent(); 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; tmp.font = LanguageManager.Instance.GetCurrentFont(); tmp.ForceMeshUpdate(); } }