diff --git a/JetKarmaBot/Commands/ChangeLocaleCommand.cs b/JetKarmaBot/Commands/ChangeLocaleCommand.cs index 7d8c82c..f567e7f 100644 --- a/JetKarmaBot/Commands/ChangeLocaleCommand.cs +++ b/JetKarmaBot/Commands/ChangeLocaleCommand.cs @@ -48,7 +48,18 @@ namespace JetKarmaBot.Commands if (Locale.ContainsLocale(cmd.Parameters[0])) localeId = cmd.Parameters[0]; else - localeId = Locale.FindByCommonName(cmd.Parameters[0]).Name; + try + { + localeId = Locale.FindByCommonName(cmd.Parameters[0]).Name; + } + catch (LocalizationException e) + { + Client.SendTextMessageAsync( + args.Message.Chat.Id, + currentLocale["jetkarmabot.changelocale.toomany"] + "\n" + string.Join("\n", (e.Data["LocaleNames"] as Locale[]).Select(x => x.Name)), + replyToMessageId: args.Message.MessageId); + return true; + } db.Chats.Find(args.Message.Chat.Id).Locale = localeId; log.Debug($"Changed language of chat {args.Message.Chat.Id} to {localeId}"); db.SaveChanges(); diff --git a/JetKarmaBot/Services/Localization.cs b/JetKarmaBot/Services/Localization.cs index a3489cb..9843dfb 100644 --- a/JetKarmaBot/Services/Localization.cs +++ b/JetKarmaBot/Services/Localization.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.Serialization; using Newtonsoft.Json.Linq; using NLog; using Perfusion; @@ -67,6 +68,16 @@ namespace JetKarmaBot return l; } } + // Try to find as locale prefix + IEnumerable matchinglocales = locales.Values.Where(x => x.Name.StartsWith(name + "-")); + if (matchinglocales.Count() > 1) + { + LocalizationException l = new LocalizationException("Too many locales"); + l.Data["LocaleNames"] = matchinglocales.ToArray(); + throw l; + } + else if (matchinglocales.Count() == 1) + return matchinglocales.First(); log.Warn("Failed to find locale " + name); return null; } @@ -148,4 +159,14 @@ namespace JetKarmaBot return ((IReadOnlyDictionary)locale).GetEnumerator(); } } + [System.Serializable] + public class LocalizationException : Exception + { + public LocalizationException() { } + public LocalizationException(string message) : base(message) { } + public LocalizationException(string message, Exception inner) : base(message, inner) { } + protected LocalizationException( + SerializationInfo info, + StreamingContext context) : base(info, context) { } + } } \ No newline at end of file diff --git a/JetKarmaBot/lang/be-BY.json b/JetKarmaBot/lang/be-BY.json index 3d0518a..64896fb 100644 --- a/JetKarmaBot/lang/be-BY.json +++ b/JetKarmaBot/lang/be-BY.json @@ -26,6 +26,7 @@ "jetkarmabot.changelocale.listalltext": "Я ведаю:", "jetkarmabot.changelocale.errorall": "Мне б хацелася гаварыць на ўсіх мовах у той жа самы час, але з-за абмежаванняў сусвету, мне гэта не дазваляецца.", "jetkarmabot.changelocale.beforenote": "Увага: ", + "jetkarmabot.changelocale.toomany": "Не ведаю, на якім дыялекце хочаш, каб я казаў.\nЯ ведаю гэтыя:", "jetkarmabot.changelocale.help": "Змяняе мову, на якім бот кажа ў гэтым чаце", "jetkarmabot.changelocale.localehelp": "Новая лакаль, альбо каманда list - спіс перакладаў. Калі пуста - паказваецца бягучая лакаль.", "jetkarmabot.help.help": "Паказвае інструкцыю да каманды, ці спіс каманд.", diff --git a/JetKarmaBot/lang/en-US.json b/JetKarmaBot/lang/en-US.json index 664a93c..be6d96f 100644 --- a/JetKarmaBot/lang/en-US.json +++ b/JetKarmaBot/lang/en-US.json @@ -25,6 +25,7 @@ "jetkarmabot.changelocale.listalltext": "I know:", "jetkarmabot.changelocale.errorall": "I would like to speak all languages at once, but because of the rules of the universe, I am not allowed to do that.", "jetkarmabot.changelocale.beforenote": "Warning: ", + "jetkarmabot.changelocale.toomany": "I don't know what dialect you want me to speak in.\nI know these:", "jetkarmabot.changelocale.help": "Switches current chat locale to [locale]", "jetkarmabot.changelocale.localehelp": "The locale to switch to. Can be \"list\" to list all possible locales. Also can be empty to get current locale", "jetkarmabot.help.help": "Displays help text for all(one) command(s)", diff --git a/JetKarmaBot/lang/ru-RU.json b/JetKarmaBot/lang/ru-RU.json index 8cf41e0..e854a59 100644 --- a/JetKarmaBot/lang/ru-RU.json +++ b/JetKarmaBot/lang/ru-RU.json @@ -25,6 +25,7 @@ "jetkarmabot.changelocale.listalltext": "Я знаю:", "jetkarmabot.changelocale.errorall": "Мне бы хотелось говорить на всех языках в то же самое время, но из-за ограничений вселенной, мне это не позволяется.", "jetkarmabot.changelocale.beforenote": "Внимание: ", + "jetkarmabot.changelocale.toomany": "Не знаю, на каком диалекте хочешь, чтобы я говорил.\nЯ знаю эти:", "jetkarmabot.changelocale.help": "Меняет язык, на котором бот говорит в этом чате", "jetkarmabot.changelocale.localehelp": "Новая локаль, либо команда list - список переводов. Если пусто - показывается текущая локаль.", "jetkarmabot.help.help": "Показывает инструкцию к команде, или список команд.",