From a7493e4911e562f32e86633f4001b3dfaa119b1c Mon Sep 17 00:00:00 2001 From: Basique Evangelist Date: Thu, 7 Feb 2019 17:14:36 +0300 Subject: [PATCH] Add functionality for awardtype localized names --- JetKarmaBot/Commands/AwardCommand.cs | 18 ++++++++++++++--- JetKarmaBot/Services/Localization.cs | 29 +++++++++++++++++++++++++++- JetKarmaBot/lang/be-BY.json | 3 ++- JetKarmaBot/lang/en-US.json | 3 ++- JetKarmaBot/lang/ru-RU.json | 3 ++- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index 6d07a51..44d321e 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -54,7 +54,7 @@ namespace JetKarmaBot.Commands var text = args.Message.Text; var awardTypeText = cmd.Parameters.FirstOrDefault(); - var awardType = awardTypeText != null + global::JetKarmaBot.Models.AwardType awardType = awardTypeText != null ? db.AwardTypes.First(at => at.CommandName == awardTypeText) : db.AwardTypes.Find((sbyte)1); @@ -70,8 +70,8 @@ namespace JetKarmaBot.Commands db.SaveChanges(); string message = awarding - ? string.Format(currentLocale["jetkarmabot.award.awardmessage"], awardType.Name, "@" + recipient.Username) - : string.Format(currentLocale["jetkarmabot.award.revokemessage"], awardType.Name, "@" + recipient.Username); + ? string.Format(currentLocale["jetkarmabot.award.awardmessage"], getLocalizedName(awardType, currentLocale), "@" + recipient.Username) + : string.Format(currentLocale["jetkarmabot.award.revokemessage"], getLocalizedName(awardType, currentLocale), "@" + recipient.Username); var currentCount = db.Awards .Where(aw => aw.ToId == recipient.Id && aw.AwardTypeId == awardType.AwardTypeId) @@ -87,6 +87,18 @@ namespace JetKarmaBot.Commands } } + private string getLocalizedName(global::JetKarmaBot.Models.AwardType awardType, Locale loc) + { + if (loc.ContainsKey($"jetkarmabot.awardtypes.{awardType.CommandName}")) + { + return loc[$"jetkarmabot.awardtypes.{awardType.CommandName}"]; + } + else + { + return awardType.Name; + } + } + [Inject] KarmaContextFactory Db { get; set; } [Inject] TelegramBotClient Client { get; set; } [Inject] Localization Locale { get; set; } 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 d1a72dd..e9f3b8b 100644 --- a/JetKarmaBot/lang/be-BY.json +++ b/JetKarmaBot/lang/be-BY.json @@ -20,6 +20,7 @@ "jetkarmabot.changelocale.getlocale": "Я зараз кажу па-беларускай.", "jetkarmabot.changelocale.listalltext": "Я ведаю:", "jetkarmabot.changelocale.errorall": "Мне б хацелася гаварыць на ўсіх мовах у той жа самы час, але з-за абмежаванняў сусвету, мне гэта не дазваляецца.", - "jetkarmabot.changelocale.beforenote": "Увага: " + "jetkarmabot.changelocale.beforenote": "Увага: ", + "jetkarmabot.awardtypes.star": "Зорачка" } } \ No newline at end of file diff --git a/JetKarmaBot/lang/en-US.json b/JetKarmaBot/lang/en-US.json index a6450af..1f6fa15 100644 --- a/JetKarmaBot/lang/en-US.json +++ b/JetKarmaBot/lang/en-US.json @@ -19,6 +19,7 @@ "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.changelocale.beforenote": "Warning: ", + "jetkarmabot.awardtypes.star": "Star" } } \ No newline at end of file diff --git a/JetKarmaBot/lang/ru-RU.json b/JetKarmaBot/lang/ru-RU.json index 112b596..03515d1 100644 --- a/JetKarmaBot/lang/ru-RU.json +++ b/JetKarmaBot/lang/ru-RU.json @@ -19,6 +19,7 @@ "jetkarmabot.changelocale.getlocale": "Я сейчас говорю по-русски.", "jetkarmabot.changelocale.listalltext": "Я знаю:", "jetkarmabot.changelocale.errorall": "Мне бы хотелось говорить на всех языках в то же самое время, но из-за ограничений вселенной, мне это не позволяется.", - "jetkarmabot.changelocale.beforenote": "Внимание: " + "jetkarmabot.changelocale.beforenote": "Внимание: ", + "jetkarmabot.awardtypes.star": "Звездочка" } } \ No newline at end of file