mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
Add autocomplete based on language identifier
Signed-off-by: Basique Evangelist <basiqueevangelist@yandex.ru>
This commit is contained in:
parent
58ed942ca3
commit
e3ece07d50
@ -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();
|
||||
|
||||
@ -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<Locale> 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<string, string>)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) { }
|
||||
}
|
||||
}
|
||||
@ -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": "Паказвае інструкцыю да каманды, ці спіс каманд.",
|
||||
|
||||
@ -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)",
|
||||
|
||||
@ -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": "Показывает инструкцию к команде, или список команд.",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user