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.
105 lines
2.6 KiB
C#
105 lines
2.6 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using TMPro;
|
|
using System.Collections.Generic;
|
|
[System.Serializable]
|
|
public class LocalizedText
|
|
{
|
|
public string key;
|
|
[TextArea(3, 10)]
|
|
public string english;
|
|
[TextArea(3, 10)]
|
|
public string arabic;
|
|
|
|
|
|
}
|
|
public class LanguageManager : MonoBehaviour
|
|
{
|
|
public static LanguageManager Instance;
|
|
|
|
public LanguageDatabase languageDB;
|
|
public string currentLanguage = "English";
|
|
|
|
private List<LocalizedTextComponent> registeredTexts = new();
|
|
public TMP_FontAsset fontEnglish;
|
|
public TMP_FontAsset fontArabic;
|
|
public TextMeshProUGUI languageLabel;
|
|
public bool languageSetBool = false;
|
|
public bool IsArabic => currentLanguage == "Arabic";
|
|
|
|
public TMP_FontAsset GetCurrentFont()
|
|
{
|
|
return currentLanguage == "Arabic" ? fontArabic : fontEnglish;
|
|
}
|
|
void Awake()
|
|
{
|
|
if (Instance == null)
|
|
Instance = this;
|
|
else
|
|
Destroy(gameObject);
|
|
}
|
|
public void ToggleLanguage()
|
|
{
|
|
currentLanguage = currentLanguage == "English" ? "Arabic" : "English";
|
|
PlayerPrefs.SetString("AppLang", currentLanguage);
|
|
UpdateLanguageLabel(); // <--- NEW
|
|
UpdateAllTexts();
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
currentLanguage = PlayerPrefs.GetString("AppLang", "English");
|
|
UpdateLanguageLabel(); // <--- NEW
|
|
UpdateAllTexts();
|
|
}
|
|
|
|
private void UpdateLanguageLabel()
|
|
{
|
|
if (languageLabel != null)
|
|
{
|
|
languageLabel.text = currentLanguage == "English" ? "English" : "Arabic";
|
|
//languageLabel.alignment = currentLanguage == "Arabic" ? TextAlignmentOptions.Right : TextAlignmentOptions.Left;
|
|
//languageLabel.font = GetCurrentFont();
|
|
}
|
|
}
|
|
|
|
|
|
public void Register(LocalizedTextComponent component)
|
|
{
|
|
if (!registeredTexts.Contains(component))
|
|
registeredTexts.Add(component);
|
|
}
|
|
|
|
public void Unregister(LocalizedTextComponent component)
|
|
{
|
|
registeredTexts.Remove(component);
|
|
}
|
|
|
|
public string GetLocalizedText(string key)
|
|
{
|
|
return languageDB.GetText(key, currentLanguage);
|
|
}
|
|
public void SetLanguage(string language)
|
|
{
|
|
languageSetBool = true;
|
|
if (language == currentLanguage) return;
|
|
|
|
if (language == "English" || language == "Arabic")
|
|
{
|
|
currentLanguage = language;
|
|
PlayerPrefs.SetString("AppLang", currentLanguage);
|
|
UpdateLanguageLabel();
|
|
UpdateAllTexts();
|
|
}
|
|
}
|
|
|
|
|
|
public void UpdateAllTexts()
|
|
{
|
|
foreach (var item in registeredTexts)
|
|
{
|
|
item.UpdateText();
|
|
}
|
|
}
|
|
}
|