Add localization

This commit is contained in:
Basique Evangelist 2018-12-21 18:13:36 +03:00
parent 4355ae4058
commit 2cdb9587ee
5 changed files with 93 additions and 11 deletions

View File

@ -10,13 +10,13 @@ namespace JetKarmaBot.Commands
{
class AwardCommand : IChatCommand
{
public IReadOnlyCollection<string> Names => new[] { "award", "revoke"};
public IReadOnlyCollection<string> 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)

View File

@ -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; }
}
}

View File

@ -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
}
}
}

View File

@ -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<string, string> 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<string, string>();
}
else
{
currentLocalization = JObject.Parse(File.ReadAllText("lang/" + currentFile + ".json")).ToObject<Dictionary<string, string>>();
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}");
}
}

View File

@ -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."
}