redid migration in order to add botconfig.locale, added some improvements

This commit is contained in:
Kwoth 2017-02-13 17:45:29 +01:00
parent de4b1fdbf5
commit 08c96385ea
6 changed files with 39 additions and 6 deletions

View File

@ -10,7 +10,7 @@ using NadekoBot.Modules.Music.Classes;
namespace NadekoBot.Migrations namespace NadekoBot.Migrations
{ {
[DbContext(typeof(NadekoContext))] [DbContext(typeof(NadekoContext))]
[Migration("20170213125444_guild-timezone-and-locale")] [Migration("20170213164350_guild-timezone-and-locale")]
partial class guildtimezoneandlocale partial class guildtimezoneandlocale
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -129,6 +129,8 @@ namespace NadekoBot.Migrations
b.Property<string>("HelpString"); b.Property<string>("HelpString");
b.Property<string>("Locale");
b.Property<int>("MigrationVersion"); b.Property<int>("MigrationVersion");
b.Property<int>("MinimumBetAmount"); b.Property<int>("MinimumBetAmount");

View File

@ -19,6 +19,12 @@ namespace NadekoBot.Migrations
table: "GuildConfigs", table: "GuildConfigs",
nullable: true, nullable: true,
defaultValue: null); defaultValue: null);
migrationBuilder.AddColumn<string>(
name: "Locale",
table: "BotConfig",
nullable: true,
defaultValue: null);
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
@ -30,6 +36,10 @@ namespace NadekoBot.Migrations
migrationBuilder.DropColumn( migrationBuilder.DropColumn(
name: "TimeZoneId", name: "TimeZoneId",
table: "GuildConfigs"); table: "GuildConfigs");
migrationBuilder.DropColumn(
name: "Locale",
table: "BotConfig");
} }
} }
} }

View File

@ -128,6 +128,8 @@ namespace NadekoBot.Migrations
b.Property<string>("HelpString"); b.Property<string>("HelpString");
b.Property<string>("Locale");
b.Property<int>("MigrationVersion"); b.Property<int>("MigrationVersion");
b.Property<int>("MinimumBetAmount"); b.Property<int>("MinimumBetAmount");

View File

@ -84,7 +84,7 @@ namespace NadekoBot
#endif #endif
//initialize Services //initialize Services
Localization = new Localization(NadekoBot.AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.Locale)); Localization = new Localization(NadekoBot.BotConfig.Locale, NadekoBot.AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.Locale));
CommandService = new CommandService(new CommandServiceConfig() { CommandService = new CommandService(new CommandServiceConfig() {
CaseSensitiveCommands = false, CaseSensitiveCommands = false,
DefaultRunMode = RunMode.Sync DefaultRunMode = RunMode.Sync

View File

@ -60,6 +60,7 @@ Nadeko Support Server: https://discord.gg/nadekobot";
public string OkColor { get; set; } = "71cd40"; public string OkColor { get; set; } = "71cd40";
public string ErrorColor { get; set; } = "ee281f"; public string ErrorColor { get; set; } = "ee281f";
public string Locale { get; set; } = null;
} }
public class PlayingStatus :DbEntity public class PlayingStatus :DbEntity

View File

@ -8,17 +8,35 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System; using System;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
using NLog;
namespace NadekoBot.Services namespace NadekoBot.Services
{ {
public class Localization public class Localization
{ {
private readonly Logger _log;
public ConcurrentDictionary<ulong, CultureInfo> GuildCultureInfos { get; } public ConcurrentDictionary<ulong, CultureInfo> GuildCultureInfos { get; }
public CultureInfo DefaultCultureInfo { get; private set; } = CultureInfo.CurrentCulture; public CultureInfo DefaultCultureInfo { get; private set; } = CultureInfo.CurrentCulture;
private Localization() { } private Localization() { }
public Localization(IDictionary<ulong, string> cultureInfoNames) public Localization(string defaultCulture, IDictionary<ulong, string> cultureInfoNames)
{ {
_log = LogManager.GetCurrentClassLogger();
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");
}
}
GuildCultureInfos = new ConcurrentDictionary<ulong, CultureInfo>(cultureInfoNames.ToDictionary(x => x.Key, x => GuildCultureInfos = new ConcurrentDictionary<ulong, CultureInfo>(cultureInfoNames.ToDictionary(x => x.Key, x =>
{ {
CultureInfo cultureInfo = null; CultureInfo cultureInfo = null;
@ -26,9 +44,7 @@ namespace NadekoBot.Services
{ {
cultureInfo = new CultureInfo(x.Value); cultureInfo = new CultureInfo(x.Value);
} }
catch catch { }
{
}
return cultureInfo; return cultureInfo;
}).Where(x => x.Value != null)); }).Where(x => x.Value != null));
} }
@ -50,6 +66,8 @@ namespace NadekoBot.Services
gc.Locale = ci.Name; gc.Locale = ci.Name;
uow.Complete(); uow.Complete();
} }
GuildCultureInfos.AddOrUpdate(guildId, ci, (id, old) => ci);
} }
public void RemoveGuildCulture(IGuild guild) => public void RemoveGuildCulture(IGuild guild) =>