mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 09:06:09 +03:00
Add localization
This commit is contained in:
parent
0025be1474
commit
8d0d70553c
@ -16,7 +16,7 @@ namespace JetKarmaBot.Commands
|
||||
{
|
||||
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;
|
||||
}
|
||||
@ -50,10 +52,10 @@ 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.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)
|
||||
|
||||
@ -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; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
66
JetKarmaBot/Services/Localization.cs
Normal file
66
JetKarmaBot/Services/Localization.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
11
JetKarmaBot/lang/en-US.json
Normal file
11
JetKarmaBot/lang/en-US.json
Normal 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."
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user