NadekoBot/NadekoBot.Core/Services/Impl/Localization.cs

147 lines
4.5 KiB
C#
Raw Normal View History

2017-07-17 19:42:36 +00:00
using System.Collections.Concurrent;
2017-02-13 09:29:34 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2017-07-17 19:42:36 +00:00
using Discord;
using NLog;
2017-07-25 16:31:30 +00:00
using NadekoBot.Services.Database.Models;
using NadekoBot.Common;
using Newtonsoft.Json;
using System.IO;
2017-02-13 09:29:34 +00:00
2017-07-17 19:42:36 +00:00
namespace NadekoBot.Services.Impl
2016-08-13 18:45:08 +00:00
{
2017-05-22 23:59:31 +00:00
public class Localization : ILocalization
2016-08-13 18:45:08 +00:00
{
private readonly Logger _log;
private readonly DbService _db;
2017-02-13 09:29:34 +00:00
public ConcurrentDictionary<ulong, CultureInfo> GuildCultureInfos { get; }
2017-02-13 09:50:58 +00:00
public CultureInfo DefaultCultureInfo { get; private set; } = CultureInfo.CurrentCulture;
2016-08-13 18:45:08 +00:00
private static readonly Dictionary<string, CommandData> _commandData;
2017-09-22 04:59:57 +00:00
static Localization()
{
_commandData = JsonConvert.DeserializeObject<Dictionary<string, CommandData>>(
File.ReadAllText("./data/command_strings.json"));
}
2017-02-13 09:29:34 +00:00
private Localization() { }
2017-07-25 16:31:30 +00:00
public Localization(IBotConfigProvider bcp, IEnumerable<GuildConfig> gcs, DbService db)
2016-08-13 18:45:08 +00:00
{
_log = LogManager.GetCurrentClassLogger();
2017-07-25 16:31:30 +00:00
var cultureInfoNames = gcs.ToDictionary(x => x.GuildId, x => x.Locale);
var defaultCulture = bcp.BotConfig.Locale;
2017-05-22 23:59:31 +00:00
_db = db;
2017-07-25 16:31:30 +00:00
if (string.IsNullOrWhiteSpace(defaultCulture))
DefaultCultureInfo = new CultureInfo("en-US");
else
{
try
{
DefaultCultureInfo = new CultureInfo(defaultCulture);
}
catch
{
_log.Warn("Unable to load default bot's locale/language. Using en-US.");
DefaultCultureInfo = new CultureInfo("en-US");
}
}
2017-02-13 09:29:34 +00:00
GuildCultureInfos = new ConcurrentDictionary<ulong, CultureInfo>(cultureInfoNames.ToDictionary(x => x.Key, x =>
{
CultureInfo cultureInfo = null;
try
{
if (x.Value == null)
return null;
2017-02-13 09:29:34 +00:00
cultureInfo = new CultureInfo(x.Value);
}
catch { }
2017-02-13 09:29:34 +00:00
return cultureInfo;
}).Where(x => x.Value != null));
}
public void SetGuildCulture(IGuild guild, CultureInfo ci) =>
SetGuildCulture(guild.Id, ci);
public void SetGuildCulture(ulong guildId, CultureInfo ci)
{
if (ci == DefaultCultureInfo)
{
RemoveGuildCulture(guildId);
return;
}
2017-05-22 23:59:31 +00:00
using (var uow = _db.UnitOfWork)
2017-02-13 09:29:34 +00:00
{
var gc = uow.GuildConfigs.For(guildId, set => set);
gc.Locale = ci.Name;
uow.Complete();
}
GuildCultureInfos.AddOrUpdate(guildId, ci, (id, old) => ci);
2017-02-13 09:29:34 +00:00
}
public void RemoveGuildCulture(IGuild guild) =>
RemoveGuildCulture(guild.Id);
public void RemoveGuildCulture(ulong guildId) {
2017-07-25 16:31:30 +00:00
if (GuildCultureInfos.TryRemove(guildId, out var _))
2017-02-13 09:29:34 +00:00
{
2017-05-22 23:59:31 +00:00
using (var uow = _db.UnitOfWork)
2017-02-13 09:29:34 +00:00
{
var gc = uow.GuildConfigs.For(guildId, set => set);
gc.Locale = null;
uow.Complete();
}
}
2016-08-15 14:57:40 +00:00
}
2017-02-13 14:29:02 +00:00
public void SetDefaultCulture(CultureInfo ci)
2017-02-13 09:50:58 +00:00
{
2017-05-22 23:59:31 +00:00
using (var uow = _db.UnitOfWork)
{
var bc = uow.BotConfig.GetOrCreate();
bc.Locale = ci.Name;
uow.Complete();
}
2017-02-13 09:50:58 +00:00
DefaultCultureInfo = ci;
}
public void ResetDefaultCulture() =>
SetDefaultCulture(CultureInfo.CurrentCulture);
2017-02-13 09:50:58 +00:00
2017-02-13 09:29:34 +00:00
public CultureInfo GetCultureInfo(IGuild guild) =>
GetCultureInfo(guild?.Id);
2017-02-13 09:29:34 +00:00
public CultureInfo GetCultureInfo(ulong? guildId)
2017-02-13 09:29:34 +00:00
{
if (guildId == null)
return DefaultCultureInfo;
2017-02-13 09:29:34 +00:00
CultureInfo info = null;
GuildCultureInfos.TryGetValue(guildId.Value, out info);
2017-02-13 09:29:34 +00:00
return info ?? DefaultCultureInfo;
}
2017-02-13 10:12:13 +00:00
public static CommandData LoadCommand(string key)
2017-02-13 10:12:13 +00:00
{
_commandData.TryGetValue(key, out var toReturn);
if (toReturn == null)
return new CommandData
{
Cmd = key,
Desc = key,
2017-09-18 07:24:42 +00:00
Usage = new[] { key },
};
return toReturn;
2017-02-13 10:12:13 +00:00
}
2016-08-13 18:45:08 +00:00
}
}