2026-04-25 18:20:16 +07:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace UI
|
|
|
|
|
{
|
|
|
|
|
public class LocalizationManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public static LocalizationManager Instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> _localizedText;
|
|
|
|
|
private string _currentLanguage = "en";
|
|
|
|
|
|
|
|
|
|
public event Action OnLanguageChanged;
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
if (Instance == null)
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
2026-04-26 05:20:47 +07:00
|
|
|
if (transform.parent == null)
|
|
|
|
|
DontDestroyOnLoad(gameObject);
|
2026-04-25 18:20:16 +07:00
|
|
|
LoadLanguage(_currentLanguage);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadLanguage(string langCode)
|
|
|
|
|
{
|
|
|
|
|
TextAsset targetFile = Resources.Load<TextAsset>($"Localization/{langCode}");
|
|
|
|
|
if (targetFile != null)
|
|
|
|
|
{
|
|
|
|
|
// Simple JSON parsing (For production, consider using a proper JSON library like Newtonsoft)
|
|
|
|
|
string json = targetFile.text;
|
|
|
|
|
_localizedText = ParseJson(json);
|
|
|
|
|
_currentLanguage = langCode;
|
|
|
|
|
OnLanguageChanged?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Get(string key)
|
|
|
|
|
{
|
|
|
|
|
if (_localizedText != null && _localizedText.ContainsKey(key))
|
|
|
|
|
return _localizedText[key];
|
|
|
|
|
return $"[{key}]";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> ParseJson(string json)
|
|
|
|
|
{
|
|
|
|
|
// Dummy parser for demonstration, replace with JsonUtility if using wrapper class
|
|
|
|
|
// or Newtonsoft for direct dictionary parsing
|
|
|
|
|
var dict = new Dictionary<string, string>();
|
|
|
|
|
string[] lines = json.Split(new[] { ',', '{', '}', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
{
|
|
|
|
|
string[] parts = line.Split(':');
|
|
|
|
|
if (parts.Length == 2)
|
|
|
|
|
{
|
|
|
|
|
string key = parts[0].Trim(' ', '"');
|
|
|
|
|
string val = parts[1].Trim(' ', '"');
|
|
|
|
|
dict[key] = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|