Neg_Utils/NEG/Localization/LocalizationManager.cs

49 lines
1.4 KiB
C#

using JetBrains.Annotations;
using System;
using System.Collections.Generic;
namespace BEG.Localization
{
public class LocalizationManager
{
public event Action OnLanguageChanged;
public string CurrentLanguage
{
get => currentLanguage?.Name ?? "";
set
{
currentLanguage = languages[value];
OnLanguageChanged?.Invoke();
}
}
public readonly LocalizationContext GlobalContext = new ();
private readonly Dictionary<string, Language> languages = new ();
private Language currentLanguage;
[PublicAPI]
public void AddLanguage(Language language) => languages.Add(language.Name, language);
[PublicAPI]
public ITranslation GetTranslation(string key) =>
currentLanguage.GetTranslation(key);
[PublicAPI] public Action Translate(string key, Action<string> action, params LocalizationContext[] contexts)
{
void Update()
{
if (currentLanguage == null) return;
var translation = currentLanguage.GetTranslation(key);
translation.Get(action, currentLanguage, contexts);
}
Update();
OnLanguageChanged += Update;
return () => OnLanguageChanged -= Update;
}
}
}