diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index 0c8bef2..5599c20 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -10,13 +10,13 @@ namespace JetKarmaBot.Commands { class AwardCommand : IChatCommand { - public IReadOnlyCollection Names => new[] { "award", "revoke"}; + public IReadOnlyCollection Names => new[] { "award", "revoke" }; public bool Execute(CommandString cmd, MessageEventArgs args) { if (args.Message.ReplyToMessage == null) { - Client.SendTextMessageAsync(args.Message.Chat.Id, "Please use this command in reply to another user."); + Client.SendTextMessageAsync(args.Message.Chat.Id, Locale["jetkarmabot.award.errawardnoreply"]); return true; } @@ -29,7 +29,7 @@ namespace JetKarmaBot.Commands { Client.SendTextMessageAsync( args.Message.Chat.Id, - "Please stop playing with yourself.", + Locale["jetkarmabot.award.errawardself"], replyToMessageId: args.Message.MessageId); return true; } @@ -38,7 +38,9 @@ namespace JetKarmaBot.Commands { Client.SendTextMessageAsync( args.Message.Chat.Id, - "I am a bot, and have no use for your foolish fake internet points.", + awarding + ? Locale["jetkarmabot.award.errawardbot"] + : Locale["jetkarmabot.award.errrevokebot"], replyToMessageId: args.Message.MessageId); return true; } @@ -49,11 +51,11 @@ namespace JetKarmaBot.Commands Db.AddAward(awardTypeId, awarder.Id, recipient.Id, args.Message.Chat.Id, awarding ? 1 : -1); - string message = awarding - ? $"Awarded a {awardType.Name} to {recipient.Username}!" - : $"Revoked a {awardType.Name} from {recipient.Username}!"; + string message = awarding + ? string.Format(Locale["jetkarmabot.award.awardmessage"], awardType.Name, recipient.Username) + : string.Format(Locale["jetkarmabot.award.revokemessage"], awardType.Name, recipient.Username); - var response = message + "\n" + $"{recipient.Username} is at {Db.CountUserAwards(recipient.Id, awardTypeId)}{awardType.Symbol} now."; + var response = message + "\n" + String.Format(Locale["jetkarmabot.award.statustext"], recipient.Username, Db.CountUserAwards(recipient.Id, awardTypeId), awardType.Symbol); Client.SendTextMessageAsync( args.Message.Chat.Id, @@ -64,6 +66,7 @@ namespace JetKarmaBot.Commands [Inject(true)] Db Db { get; set; } [Inject(true)] TelegramBotClient Client { get; set; } + [Inject(true)] Localization Locale { get; set; } User Me { get; } public AwardCommand(User me) diff --git a/JetKarmaBot/Commands/StatusCommand.cs b/JetKarmaBot/Commands/StatusCommand.cs index 1ff8114..475fa7c 100644 --- a/JetKarmaBot/Commands/StatusCommand.cs +++ b/JetKarmaBot/Commands/StatusCommand.cs @@ -24,7 +24,7 @@ namespace JetKarmaBot.Commands { var awards = Db.CountAllUserAwards(asker.Id); - response = "Your badges report:\n" + response = Locale["jetkarmabot.status.listalltext"] + "\n" + string.Join("\n", awards.Select(a => $" - {Db.AwardTypes[a.AwardTypeId].Symbol} {a.Amount}")); } @@ -33,7 +33,7 @@ namespace JetKarmaBot.Commands var awardTypeId = Db.GetAwardTypeId(cmd.Parameters.FirstOrDefault()); var awardType = Db.AwardTypes[awardTypeId]; - response = $"You are at {Db.CountUserAwards(asker.Id, awardTypeId)}{awardType.Symbol} now."; + response = string.Format(Locale["jetkarmabot.status.listspecifictext"], Db.CountUserAwards(asker.Id, awardTypeId), awardType.Symbol); } Client.SendTextMessageAsync( @@ -45,6 +45,7 @@ namespace JetKarmaBot.Commands [Inject(true)] Db Db { get; set; } [Inject(true)] TelegramBotClient Client { get; set; } + [Inject(true)] Localization Locale { get; set; } } } diff --git a/JetKarmaBot/Config.cs b/JetKarmaBot/Config.cs index 68d2e0f..a88f0c1 100644 --- a/JetKarmaBot/Config.cs +++ b/JetKarmaBot/Config.cs @@ -11,6 +11,7 @@ namespace JetKarmaBot public string ApiKey { get; private set; } public string ConnectionString { get; private set; } + public string Language { get; private set; } public class ProxySettings { @@ -60,4 +61,4 @@ namespace JetKarmaBot } } } - + diff --git a/JetKarmaBot/Services/Localization.cs b/JetKarmaBot/Services/Localization.cs new file mode 100644 index 0000000..593eea4 --- /dev/null +++ b/JetKarmaBot/Services/Localization.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; +using Dapper; +using MySql.Data.MySqlClient; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Perfusion; + +namespace JetKarmaBot +{ + public class Localization + { + private string currentFile; + private Dictionary currentLocalization; + + public Localization([Inject(true)]Config cfg) + { + Log("Initializing..."); + currentFile = cfg.Language; + if (string.IsNullOrEmpty(currentFile)) currentFile = "en-US"; + Log("Language is " + currentFile); + if (!Directory.Exists("lang")) + Directory.CreateDirectory("lang"); + + if (!File.Exists("lang/" + currentFile + ".json") && currentFile != "en-US") + { + Log("Language " + currentFile + " not found, falling back to en-US"); + currentFile = "en-US"; + } + if (!File.Exists("lang/" + currentFile + ".json")) + { + Log("Language en-US doesn't exist! Making empty localization"); + currentLocalization = new Dictionary(); + } + else + { + currentLocalization = JObject.Parse(File.ReadAllText("lang/" + currentFile + ".json")).ToObject>(); + Log("Loaded " + currentFile); + } + Log("Initialized!"); + } + + public string this[string name] + { + get => GetString(name); + } + public string GetString(string name) + { + if (!currentLocalization.ContainsKey(name)) + { + Log(name + " doesn't exist in this localization"); + currentLocalization[name] = "unknown"; + File.WriteAllText("lang/" + currentFile + ".json", JObject.FromObject(currentLocalization).ToString()); + return "unknown"; + } + else + { + return currentLocalization[name]; + } + } + void Log(string Message) => Console.WriteLine($"[{nameof(Localization)}]: {Message}"); + } +} diff --git a/JetKarmaBot/lang/en-US.json b/JetKarmaBot/lang/en-US.json new file mode 100644 index 0000000..bc6a84f --- /dev/null +++ b/JetKarmaBot/lang/en-US.json @@ -0,0 +1,11 @@ +{ + "jetkarmabot.award.errawardnoreply": "Please use this command in reply to another user.", + "jetkarmabot.award.errawardself": "Please stop playing with yourself.", + "jetkarmabot.award.errawardbot": "I am a bot, and have no use for your foolish fake internet points.", + "jetkarmabot.award.errrevokebot": "ಠ_ಠ", + "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.status.listalltext": "Your badges report:", + "jetkarmabot.status.listspecifictext": "You are at {0}{1} now." +} \ No newline at end of file