2023-07-18 15:25:02 +03:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
|
|
|
using Remora.Results;
|
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.Octobot.Data.Options;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-05-16 18:34:26 +03:00
|
|
|
public sealed class LanguageOption : GuildOption<CultureInfo>
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
|
|
|
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new()
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
{ "en", new CultureInfo("en-US") },
|
2024-04-01 22:20:41 +03:00
|
|
|
{ "ru", new CultureInfo("ru-RU") }
|
2023-07-18 15:25:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
public LanguageOption(string name, string defaultValue) : base(name, CultureInfoCache[defaultValue]) { }
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public override string Display(JsonNode settings)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return Markdown.InlineCode(settings[Name]?.GetValue<string>() ?? "en");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-08-02 23:51:16 +03:00
|
|
|
public override CultureInfo Get(JsonNode settings)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
var property = settings[Name];
|
|
|
|
return property != null ? CultureInfoCache[property.GetValue<string>()] : DefaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-08-02 23:51:16 +03:00
|
|
|
public override Result Set(JsonNode settings, string from)
|
|
|
|
{
|
2023-07-20 10:36:21 +03:00
|
|
|
return CultureInfoCache.ContainsKey(from.ToLowerInvariant())
|
|
|
|
? base.Set(settings, from.ToLowerInvariant())
|
2023-07-28 19:58:55 +03:00
|
|
|
: new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported);
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
}
|