new localization interface draft
This commit is contained in:
parent
eba429022b
commit
6ae01f763c
8
NEG/Localization.meta
Normal file
8
NEG/Localization.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e37f36b09b24be54ea4e03cc602bbd59
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
18
NEG/Localization/Language.cs
Normal file
18
NEG/Localization/Language.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BEG.Localization
|
||||
{
|
||||
public class Language
|
||||
{
|
||||
[PublicAPI]
|
||||
public readonly string Name;
|
||||
|
||||
private Dictionary<string, ITranslation> translations = new ();
|
||||
|
||||
public ITranslation GetTranslation(string key) => translations[key];
|
||||
public void AddTranslation(ITranslation translation) => translations.Add(translation.Key, translation);
|
||||
|
||||
public ITranslation this[string key] => GetTranslation(key);
|
||||
}
|
||||
}
|
||||
3
NEG/Localization/Language.cs.meta
Normal file
3
NEG/Localization/Language.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2755a88e374448688202c15c2d67777c
|
||||
timeCreated: 1675712587
|
||||
63
NEG/Localization/LocalizationContext.cs
Normal file
63
NEG/Localization/LocalizationContext.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using JetBrains.Annotations;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BEG.Localization
|
||||
{
|
||||
public class LocalizationContext
|
||||
{
|
||||
public interface IParam
|
||||
{
|
||||
[PublicAPI] public event Action OnValueChanged;
|
||||
}
|
||||
|
||||
public class Param<T>: IParam
|
||||
{
|
||||
public event Action OnValueChanged;
|
||||
|
||||
[PublicAPI]
|
||||
public T Value
|
||||
{
|
||||
get => value;
|
||||
set
|
||||
{
|
||||
OnValueChanged?.Invoke();
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
[NotNull] private readonly Func<T, string> formatter;
|
||||
private T value;
|
||||
|
||||
public Param(T value, Func<T, string> formatter)
|
||||
{
|
||||
this.formatter = formatter ?? (v => v.ToString());
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override string ToString() => formatter(value);
|
||||
}
|
||||
|
||||
private Dictionary<string, IParam> parameters = new ();
|
||||
|
||||
public void SetParam(string name, IParam param)
|
||||
{
|
||||
if (parameters.ContainsKey(name))
|
||||
parameters[name] = param;
|
||||
else
|
||||
parameters.Add(name, param);
|
||||
}
|
||||
|
||||
public void RemoveParam(string name) => parameters.Remove(name);
|
||||
public IParam GetParam(string name) => parameters[name];
|
||||
|
||||
public void ClearParams() => parameters.Clear();
|
||||
|
||||
public IParam this[string name]
|
||||
{
|
||||
get => GetParam(name);
|
||||
set => SetParam(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
NEG/Localization/LocalizationContext.cs.meta
Normal file
3
NEG/Localization/LocalizationContext.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 19a01d756a5548e7ab1e357fee9dc8b6
|
||||
timeCreated: 1675712674
|
||||
49
NEG/Localization/LocalizationManager.cs
Normal file
49
NEG/Localization/LocalizationManager.cs
Normal file
@ -0,0 +1,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
NEG/Localization/LocalizationManager.cs.meta
Normal file
3
NEG/Localization/LocalizationManager.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1544626d93fa411ea713f77d68a831ae
|
||||
timeCreated: 1675712650
|
||||
26
NEG/Localization/Translation.cs
Normal file
26
NEG/Localization/Translation.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using JetBrains.Annotations;
|
||||
using System;
|
||||
|
||||
namespace BEG.Localization
|
||||
{
|
||||
public interface ITranslation
|
||||
{
|
||||
[PublicAPI] string Key { get; }
|
||||
|
||||
[PublicAPI] void Get(Action<string> action, Language language, params LocalizationContext[] contexts);
|
||||
}
|
||||
|
||||
public class SimpleTranslation: ITranslation
|
||||
{
|
||||
public string Key { get; }
|
||||
public void Get(Action<string> action, Language language, params LocalizationContext[] contexts) => action?.Invoke(value);
|
||||
|
||||
private readonly string value;
|
||||
|
||||
public SimpleTranslation(string key, string value)
|
||||
{
|
||||
Key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
NEG/Localization/Translation.cs.meta
Normal file
3
NEG/Localization/Translation.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64ea16abf71b4c36ac03e0b14ef33e1f
|
||||
timeCreated: 1675712656
|
||||
Loading…
x
Reference in New Issue
Block a user