From 08c96385ea97f3f81285417df8711b39488a6cfc Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 13 Feb 2017 17:45:29 +0100 Subject: [PATCH] redid migration in order to add botconfig.locale, added some improvements --- ...350_guild-timezone-and-locale.Designer.cs} | 4 ++- ...170213164350_guild-timezone-and-locale.cs} | 10 +++++++ .../NadekoSqliteContextModelSnapshot.cs | 2 ++ src/NadekoBot/NadekoBot.cs | 2 +- .../Services/Database/Models/BotConfig.cs | 1 + src/NadekoBot/Services/Impl/Localization.cs | 26 ++++++++++++++++--- 6 files changed, 39 insertions(+), 6 deletions(-) rename src/NadekoBot/Migrations/{20170213125444_guild-timezone-and-locale.Designer.cs => 20170213164350_guild-timezone-and-locale.Designer.cs} (99%) rename src/NadekoBot/Migrations/{20170213125444_guild-timezone-and-locale.cs => 20170213164350_guild-timezone-and-locale.cs} (77%) diff --git a/src/NadekoBot/Migrations/20170213125444_guild-timezone-and-locale.Designer.cs b/src/NadekoBot/Migrations/20170213164350_guild-timezone-and-locale.Designer.cs similarity index 99% rename from src/NadekoBot/Migrations/20170213125444_guild-timezone-and-locale.Designer.cs rename to src/NadekoBot/Migrations/20170213164350_guild-timezone-and-locale.Designer.cs index 670a7e40..1dc832d9 100644 --- a/src/NadekoBot/Migrations/20170213125444_guild-timezone-and-locale.Designer.cs +++ b/src/NadekoBot/Migrations/20170213164350_guild-timezone-and-locale.Designer.cs @@ -10,7 +10,7 @@ using NadekoBot.Modules.Music.Classes; namespace NadekoBot.Migrations { [DbContext(typeof(NadekoContext))] - [Migration("20170213125444_guild-timezone-and-locale")] + [Migration("20170213164350_guild-timezone-and-locale")] partial class guildtimezoneandlocale { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -129,6 +129,8 @@ namespace NadekoBot.Migrations b.Property("HelpString"); + b.Property("Locale"); + b.Property("MigrationVersion"); b.Property("MinimumBetAmount"); diff --git a/src/NadekoBot/Migrations/20170213125444_guild-timezone-and-locale.cs b/src/NadekoBot/Migrations/20170213164350_guild-timezone-and-locale.cs similarity index 77% rename from src/NadekoBot/Migrations/20170213125444_guild-timezone-and-locale.cs rename to src/NadekoBot/Migrations/20170213164350_guild-timezone-and-locale.cs index 6aab0a04..757d9150 100644 --- a/src/NadekoBot/Migrations/20170213125444_guild-timezone-and-locale.cs +++ b/src/NadekoBot/Migrations/20170213164350_guild-timezone-and-locale.cs @@ -19,6 +19,12 @@ namespace NadekoBot.Migrations table: "GuildConfigs", nullable: true, defaultValue: null); + + migrationBuilder.AddColumn( + name: "Locale", + table: "BotConfig", + nullable: true, + defaultValue: null); } protected override void Down(MigrationBuilder migrationBuilder) @@ -30,6 +36,10 @@ namespace NadekoBot.Migrations migrationBuilder.DropColumn( name: "TimeZoneId", table: "GuildConfigs"); + + migrationBuilder.DropColumn( + name: "Locale", + table: "BotConfig"); } } } diff --git a/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs b/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs index 36fc1624..5a03594c 100644 --- a/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs +++ b/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs @@ -128,6 +128,8 @@ namespace NadekoBot.Migrations b.Property("HelpString"); + b.Property("Locale"); + b.Property("MigrationVersion"); b.Property("MinimumBetAmount"); diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 65f1e02b..5df52a6e 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -84,7 +84,7 @@ namespace NadekoBot #endif //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() { CaseSensitiveCommands = false, DefaultRunMode = RunMode.Sync diff --git a/src/NadekoBot/Services/Database/Models/BotConfig.cs b/src/NadekoBot/Services/Database/Models/BotConfig.cs index 6b35e5f2..8ff90e8a 100644 --- a/src/NadekoBot/Services/Database/Models/BotConfig.cs +++ b/src/NadekoBot/Services/Database/Models/BotConfig.cs @@ -60,6 +60,7 @@ Nadeko Support Server: https://discord.gg/nadekobot"; public string OkColor { get; set; } = "71cd40"; public string ErrorColor { get; set; } = "ee281f"; + public string Locale { get; set; } = null; } public class PlayingStatus :DbEntity diff --git a/src/NadekoBot/Services/Impl/Localization.cs b/src/NadekoBot/Services/Impl/Localization.cs index 73891d4a..df703381 100644 --- a/src/NadekoBot/Services/Impl/Localization.cs +++ b/src/NadekoBot/Services/Impl/Localization.cs @@ -8,17 +8,35 @@ using System.Linq; using System.Threading.Tasks; using System; using NadekoBot.Services.Database; +using NLog; namespace NadekoBot.Services { public class Localization { + private readonly Logger _log; + public ConcurrentDictionary GuildCultureInfos { get; } public CultureInfo DefaultCultureInfo { get; private set; } = CultureInfo.CurrentCulture; private Localization() { } - public Localization(IDictionary cultureInfoNames) + public Localization(string defaultCulture, IDictionary 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(cultureInfoNames.ToDictionary(x => x.Key, x => { CultureInfo cultureInfo = null; @@ -26,9 +44,7 @@ namespace NadekoBot.Services { cultureInfo = new CultureInfo(x.Value); } - catch - { - } + catch { } return cultureInfo; }).Where(x => x.Value != null)); } @@ -50,6 +66,8 @@ namespace NadekoBot.Services gc.Locale = ci.Name; uow.Complete(); } + + GuildCultureInfos.AddOrUpdate(guildId, ci, (id, old) => ci); } public void RemoveGuildCulture(IGuild guild) =>