AnPv2/CSharp/Managers/I18NManager.cs

58 lines
2.4 KiB
C#

using System.Collections.Generic;
using AnP.Types;
using AnP.Utils;
using AnP.Interfaces.Application;
namespace AnP.Managers{
public class I18NManager{
private static readonly Dictionary<string, Dictionary<string, I18NSentenceType>> DEFAULT_SENTENCES = new Dictionary<string, Dictionary<string, I18NSentenceType>>{
{"english", new Dictionary<string, I18NSentenceType>{
{"greeting", new I18NSentenceType.Text("Hello, World!")}
}}
};
private static readonly Options GET_OPTIONS = new Options();
public AnPInterface anp;
private Dictionary<string, Dictionary<string, I18NSentenceType>> sentences;
private string language;
private string default_language;
public I18NManager(AnPInterface anp){
this.anp = anp;
sentences = new Dictionary<string, Dictionary<string, I18NSentenceType>>(DEFAULT_SENTENCES);
language = default_language = "english";
}
public string get(object strings, object? inputs = null, object? languages = null, int custom_options = 0){
List<string> keys = Common.get_keys(strings);
Options options = new Options(custom_options, GET_OPTIONS);
if(keys.Count != 0){
List<string> languages_used = new List<string>();
foreach(string language in Common.get_keys(languages).Concat<string>(
new List<string>{this.language, default_language}
).Concat<string>(sentences.Keys))
if(!languages_used.Contains(language)){
languages_used.Add(language);
if(sentences.ContainsKey(language))
foreach(string key in keys)
if(sentences[language].ContainsKey(key))
return sentences[language][key] switch{
I18NSentenceType.Text text => text.value,
I18NSentenceType.List list => string.Join("", list.value),
_ => ""
};
}
};
return Common.get_strings(strings).FirstOrDefault() ?? "";
}
public void add(object? items, int custom_options = 0){}
}
}