Update
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
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;
|
||||
if (transform.parent == null)
|
||||
DontDestroyOnLoad(gameObject);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user