149 lines
3.4 KiB
C#
149 lines
3.4 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.Serialization.Json;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Pf2eModel.Model.Common.I18n;
|
|
|
|
public class LocaleString : ICollection<LocaleItem>
|
|
{
|
|
private readonly List<LocaleItem> _items = [];
|
|
private void AssureDefault()
|
|
{
|
|
if (_items.Count == 0)
|
|
{
|
|
_items.Add(new LocaleItem(LocaleType.English, string.Empty));
|
|
}
|
|
}
|
|
|
|
public int Count => _items.Count;
|
|
|
|
public bool IsReadOnly => false;
|
|
|
|
public ICollection<LocaleType> Types => _items.Select(c => c.Locale).ToList();
|
|
|
|
public LocaleString()
|
|
{
|
|
AssureDefault();
|
|
}
|
|
|
|
private LocaleType _E(string localeString) => LocaleExtension.GetTypeByString(localeString);
|
|
|
|
private LocaleType RealLocale(LocaleType locale) => Contains(locale) ? locale : LocaleType.English;
|
|
private LocaleType RealLocale(string localeString) => RealLocale(_E(localeString));
|
|
|
|
public LocaleItem GetItem(LocaleType locale) => this.First(c => c.Locale == RealLocale(locale));
|
|
public LocaleItem GetItem(string locale) => this.First(c => c.Locale == RealLocale(locale));
|
|
|
|
public string this[LocaleType locale]
|
|
{
|
|
get => GetItem(locale).Value;
|
|
set
|
|
{
|
|
if (Types.Contains(locale))
|
|
{
|
|
GetItem(locale).Value = value;
|
|
}
|
|
else
|
|
{
|
|
_items.Add(new LocaleItem(locale, value));
|
|
}
|
|
}
|
|
}
|
|
public string this[string localeString]
|
|
{
|
|
get => this[_E(localeString)];
|
|
set => this[_E(localeString)] = value;
|
|
}
|
|
|
|
public void Add(LocaleItem item)
|
|
{
|
|
this[item.Locale] = item.Value;
|
|
}
|
|
public void Add(LocaleType locale, string value)
|
|
{
|
|
this[locale] = value;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_items.Clear();
|
|
AssureDefault();
|
|
}
|
|
|
|
public bool Contains(LocaleItem item)
|
|
{
|
|
return _items.Contains(item);
|
|
}
|
|
public bool Contains(LocaleType locale)
|
|
{
|
|
return Types.Contains(locale);
|
|
}
|
|
public bool Contains(string value)
|
|
{
|
|
return this.Any(item => item.Value == value);
|
|
}
|
|
|
|
public void CopyTo(LocaleItem[] array, int arrayIndex)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public bool Remove(LocaleItem item)
|
|
{
|
|
bool result = _items.Remove(item);
|
|
|
|
if (Count == 0)
|
|
{
|
|
AssureDefault();
|
|
return false;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public IEnumerator<LocaleItem> GetEnumerator()
|
|
{
|
|
return _items.GetEnumerator();
|
|
}
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return this[LocaleType.English];
|
|
}
|
|
|
|
public string Serialize()
|
|
{
|
|
JObject json = [];
|
|
|
|
foreach (LocaleItem item in _items)
|
|
{
|
|
json.Add(item.Locale.GetTypeString(), item.Value);
|
|
}
|
|
|
|
return json.ToString();
|
|
}
|
|
public static LocaleString Deserialize(string input)
|
|
{
|
|
JObject json = JObject.Parse(input);
|
|
LocaleString result = new();
|
|
|
|
foreach (var item in json)
|
|
{
|
|
result[item.Key] = item.ToString();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|