diff --git a/JetKarmaBot/CommandRouter.cs b/JetKarmaBot/CommandRouter.cs index 69e5499..f65d5ee 100644 --- a/JetKarmaBot/CommandRouter.cs +++ b/JetKarmaBot/CommandRouter.cs @@ -65,7 +65,7 @@ namespace JetKarmaBot } } - public string GetHelpText() + internal string GetHelpText(Locale loc) { List pieces = new List(); foreach (IChatCommand c in commands.Values.Distinct()) @@ -76,13 +76,13 @@ namespace JetKarmaBot { build = build + "/" + names[i] + "\n"; } - build += "/" + names[names.Count - 1] + " " + string.Join(' ', c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + c.Description + ""; + build += "/" + names[names.Count - 1] + " " + string.Join(' ', c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + getLocalizedCMDDesc(c, loc) + ""; pieces.Add(build); } return string.Join('\n', pieces); } - internal string GetHelpTextFor(string commandname) + internal string GetHelpTextFor(string commandname, Locale loc) { IChatCommand c = commands[commandname]; string build = ""; @@ -91,11 +91,22 @@ namespace JetKarmaBot { build = build + "/" + names[i] + "\n"; } - build += "/" + names[names.Count - 1] + " " + string.Join(' ', c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + c.Description + "\n"; - build += string.Join("\n", c.Arguments.Select(ca => (!ca.Required ? "[" : "") + ca.Name + (!ca.Required ? "]" : "") + ": " + ca.Description + "")); + build += "/" + names[names.Count - 1] + " " + string.Join(' ', c.Arguments.Select(x => (!x.Required ? "[" : "") + x.Name + (!x.Required ? "]" : ""))) + " " + getLocalizedCMDDesc(c, loc) + "\n"; + build += string.Join("\n", c.Arguments.Select(ca => (!ca.Required ? "[" : "") + ca.Name + (!ca.Required ? "]" : "") + ": " + getLocalizedCMDArgDesc(ca, loc) + "")); return build; } + private string getLocalizedCMDDesc(IChatCommand cmd, Locale loc) + { + if (loc.ContainsKey(cmd.DescriptionID)) return loc[cmd.DescriptionID]; + else return cmd.Description; + } + private string getLocalizedCMDArgDesc(ChatCommandArgument arg, Locale loc) + { + if (loc.ContainsKey(arg.DescriptionID)) return loc[arg.DescriptionID]; + else return arg.Description; + } + Dictionary commands = new Dictionary(); } } diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index 00bf025..6fe0467 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -93,13 +93,15 @@ namespace JetKarmaBot.Commands User Me { get; } public string Description => "Awards/revokes an award to a user."; + public string DescriptionID => "jetkarmabot.award.help"; public IReadOnlyCollection Arguments => new ChatCommandArgument[] { new ChatCommandArgument() { Name="awardtype", Required=false, Type=ChatCommandArgumentType.String, - Description="The award to grant to/strip of the specified user" + Description="The award to grant to/strip of the specified user", + DescriptionID="jetkarmabot.award.awardtypehelp" } }; diff --git a/JetKarmaBot/Commands/ChangeLocaleCommand.cs b/JetKarmaBot/Commands/ChangeLocaleCommand.cs index 99f9433..7d8c82c 100644 --- a/JetKarmaBot/Commands/ChangeLocaleCommand.cs +++ b/JetKarmaBot/Commands/ChangeLocaleCommand.cs @@ -69,13 +69,15 @@ namespace JetKarmaBot.Commands [Inject] Localization Locale { get; set; } public string Description => "Switches current chat locale to [locale]"; + public string DescriptionID => "jetkarmabot.changelocale.help"; public IReadOnlyCollection Arguments => new ChatCommandArgument[] { new ChatCommandArgument() { Name="locale", Required=false, Type=ChatCommandArgumentType.String, - Description="The locale to switch to. Can be \"list\" to list all possible locales. Also can be empty to get current locale" + Description="The locale to switch to. Can be \"list\" to list all possible locales. Also can be empty to get current locale.", + DescriptionID="jetkarmabot.changelocale.localehelp" } }; } diff --git a/JetKarmaBot/Commands/HelpCommand.cs b/JetKarmaBot/Commands/HelpCommand.cs index 4f2bd8d..1e455f7 100644 --- a/JetKarmaBot/Commands/HelpCommand.cs +++ b/JetKarmaBot/Commands/HelpCommand.cs @@ -16,13 +16,15 @@ namespace JetKarmaBot.Commands public IReadOnlyCollection Names => new[] { "help" }; public string Description => "Displays help text for all(one) command(s)"; + public string DescriptionID => "jetkarmabot.help.help"; public IReadOnlyCollection Arguments => new ChatCommandArgument[] { new ChatCommandArgument() { Name="command", Required=false, Type=ChatCommandArgumentType.String, - Description="The command to return help text for. If empty shows all commands" + Description="The command to return help text for. If empty shows all commands.", + DescriptionID="jetkarmabot.help.commandhelp" } }; @@ -35,7 +37,7 @@ namespace JetKarmaBot.Commands { Client.SendTextMessageAsync( args.Message.Chat.Id, - Router.GetHelpText(), + Router.GetHelpText(currentLocale), replyToMessageId: args.Message.MessageId, parseMode: ParseMode.Html); return true; @@ -44,7 +46,7 @@ namespace JetKarmaBot.Commands { Client.SendTextMessageAsync( args.Message.Chat.Id, - Router.GetHelpTextFor(cmd.Parameters[0]), + Router.GetHelpTextFor(cmd.Parameters[0], currentLocale), replyToMessageId: args.Message.MessageId, parseMode: ParseMode.Html); return true; diff --git a/JetKarmaBot/Commands/IChatCommand.cs b/JetKarmaBot/Commands/IChatCommand.cs index 69fd0b3..436f789 100644 --- a/JetKarmaBot/Commands/IChatCommand.cs +++ b/JetKarmaBot/Commands/IChatCommand.cs @@ -7,6 +7,7 @@ namespace JetKarmaBot.Commands { IReadOnlyCollection Names { get; } string Description { get; } + string DescriptionID { get; } IReadOnlyCollection Arguments { get; } bool Execute(CommandString cmd, MessageEventArgs messageEventArgs); @@ -18,6 +19,7 @@ namespace JetKarmaBot.Commands public bool Required; public ChatCommandArgumentType Type; public string Description; + public string DescriptionID; } public enum ChatCommandArgumentType diff --git a/JetKarmaBot/Commands/StatusCommand.cs b/JetKarmaBot/Commands/StatusCommand.cs index 6039fa8..db45e3b 100644 --- a/JetKarmaBot/Commands/StatusCommand.cs +++ b/JetKarmaBot/Commands/StatusCommand.cs @@ -68,13 +68,15 @@ namespace JetKarmaBot.Commands [Inject] Localization Locale { get; set; } public string Description => "Shows the amount of awards that you have"; + public string DescriptionID => "jetkarmabot.status.help"; public IReadOnlyCollection Arguments => new ChatCommandArgument[] { new ChatCommandArgument(){ Name="awardtype", Required=false, Type=ChatCommandArgumentType.String, - Description="The awardtype to show. If empty shows everything." + Description="The awardtype to show. If empty shows everything.", + DescriptionID= "jetkarmabot.status.awardtypehelp" } }; } diff --git a/JetKarmaBot/Services/Localization.cs b/JetKarmaBot/Services/Localization.cs index aedba8e..bb4f250 100644 --- a/JetKarmaBot/Services/Localization.cs +++ b/JetKarmaBot/Services/Localization.cs @@ -100,7 +100,7 @@ namespace JetKarmaBot } - public class Locale + public class Locale : IReadOnlyDictionary { private Dictionary locale; private string localeName; @@ -119,6 +119,33 @@ namespace JetKarmaBot public bool HasNote => note != null; public string Note => note; + + public IEnumerable Keys => ((IReadOnlyDictionary)locale).Keys; + + public IEnumerable Values => ((IReadOnlyDictionary)locale).Values; + + public int Count => ((IReadOnlyDictionary)locale).Count; + public string this[string name] => locale.ContainsKey(name) ? locale[name] : "unknown"; + + public bool ContainsKey(string key) + { + return ((IReadOnlyDictionary)locale).ContainsKey(key); + } + + public bool TryGetValue(string key, out string value) + { + return ((IReadOnlyDictionary)locale).TryGetValue(key, out value); + } + + public IEnumerator> GetEnumerator() + { + return ((IReadOnlyDictionary)locale).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IReadOnlyDictionary)locale).GetEnumerator(); + } } } \ No newline at end of file diff --git a/JetKarmaBot/lang/be-BY.json b/JetKarmaBot/lang/be-BY.json index c3db7d2..e977481 100644 --- a/JetKarmaBot/lang/be-BY.json +++ b/JetKarmaBot/lang/be-BY.json @@ -13,14 +13,21 @@ "jetkarmabot.award.awardmessage": "Ўручыў \"{0}\" {1}!", "jetkarmabot.award.revokemessage": "Адабраў \"{0}\" у {1}!", "jetkarmabot.award.statustext": "У {0} цяпер {1}{2}.", + "jetkarmabot.award.help": "", + "jetkarmabot.award.awardtypehelp": "", "jetkarmabot.status.listalltext": "У вас :", "jetkarmabot.status.listspecifictext": "У вас зараз {0}{1}.", "jetkarmabot.status.havenothing": "У вас пакуль нічога няма.", + "jetkarmabot.status.help": "", + "jetkarmabot.status.awardtypehelp": "", "jetkarmabot.changelocale.justchanged": "Так дакладна.", "jetkarmabot.changelocale.getlocale": "Я зараз кажу па-беларускай.", "jetkarmabot.changelocale.listalltext": "Я ведаю:", "jetkarmabot.changelocale.errorall": "Мне б хацелася гаварыць на ўсіх мовах у той жа самы час, але з-за абмежаванняў сусвету, мне гэта не дазваляецца.", "jetkarmabot.changelocale.beforenote": "Увага: ", - "jetkarmabot.help": "TODO TODO TODO TODO" + "jetkarmabot.changelocale.help": "", + "jetkarmabot.changelocale.localehelp": "", + "jetkarmabot.help.help": "", + "jetkarmabot.help.commandhelp": "" } } \ No newline at end of file diff --git a/JetKarmaBot/lang/en-US.json b/JetKarmaBot/lang/en-US.json index b550268..f3eecd2 100644 --- a/JetKarmaBot/lang/en-US.json +++ b/JetKarmaBot/lang/en-US.json @@ -12,14 +12,21 @@ "jetkarmabot.award.awardmessage": "Awarded a {0} to {1}!", "jetkarmabot.award.revokemessage": "Revoked a {0} from {1}!", "jetkarmabot.award.statustext": "{0} is at {1}{2} now.", + "jetkarmabot.award.help": "Awards/revokes an award to a user.", + "jetkarmabot.award.awardtypehelp": "The award to grant to/strip of the specified user", "jetkarmabot.status.listalltext": "Your badges report:", "jetkarmabot.status.listspecifictext": "You are at {0}{1} now.", "jetkarmabot.status.havenothing": "You don't have anything yet.", + "jetkarmabot.status.help": "Shows the amount of awards that you have", + "jetkarmabot.status.awardtypehelp": "The awardtype to show. If empty shows everything.", "jetkarmabot.changelocale.justchanged": "Roger that.", "jetkarmabot.changelocale.getlocale": "I'm currently speaking English.", "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.help": "TODO TODO TODO TODO" + "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)", + "jetkarmabot.help.commandhelp": "The command to return help text for. If empty shows all commands." } } \ No newline at end of file diff --git a/JetKarmaBot/lang/ru-RU.json b/JetKarmaBot/lang/ru-RU.json index 7c744c5..83b7b90 100644 --- a/JetKarmaBot/lang/ru-RU.json +++ b/JetKarmaBot/lang/ru-RU.json @@ -12,14 +12,21 @@ "jetkarmabot.award.awardmessage": "Вручил \"{0}\" {1}!", "jetkarmabot.award.revokemessage": "Отнял \"{0}\" у {1}!", "jetkarmabot.award.statustext": "У {0} теперь {1}{2}.", + "jetkarmabot.award.help": "Поручает/лишает награду от ползователя/пользователю.", + "jetkarmabot.award.awardtypehelp": "Награду которую поручить/лишить от пользователя", "jetkarmabot.status.listalltext": "У вас :", "jetkarmabot.status.listspecifictext": "У вас сейчас {0}{1}.", "jetkarmabot.status.havenothing": "У вас пока ничего нет.", + "jetkarmabot.status.help": "", + "jetkarmabot.status.awardtypehelp": "", "jetkarmabot.changelocale.justchanged": "Так точно.", "jetkarmabot.changelocale.getlocale": "Я сейчас говорю по-русски.", "jetkarmabot.changelocale.listalltext": "Я знаю:", "jetkarmabot.changelocale.errorall": "Мне бы хотелось говорить на всех языках в то же самое время, но из-за ограничений вселенной, мне это не позволяется.", "jetkarmabot.changelocale.beforenote": "Внимание: ", - "jetkarmabot.help": "TODO TODO TODO TODO" + "jetkarmabot.changelocale.help": "", + "jetkarmabot.changelocale.localehelp": "", + "jetkarmabot.help.help": "", + "jetkarmabot.help.commandhelp": "" } } \ No newline at end of file