localization fixes

This commit is contained in:
jetsparrow 2019-01-06 19:08:55 +03:00
parent fd732d8719
commit d20cc89f28
5 changed files with 60 additions and 36 deletions

View File

@ -19,16 +19,24 @@ namespace JetKarmaBot
public bool Execute(object sender, MessageEventArgs args) public bool Execute(object sender, MessageEventArgs args)
{ {
var text = args.Message.Text; var text = args.Message.Text;
if (CommandString.TryParse(text, out var cmd)) if (CommandString.TryParse(text, out var cmd))
{ {
if (cmd.UserName != null && cmd.UserName != BotUser.Username) // directed not at us! if (cmd.UserName != null && cmd.UserName != BotUser.Username) // directed not at us!
return false; return false;
if (commands.ContainsKey(cmd.Command)) try
return commands[cmd.Command].Execute(cmd, args); {
if (commands.ContainsKey(cmd.Command))
return commands[cmd.Command].Execute(cmd, args);
}
catch (Exception e)
{
Console.WriteLine(e);
}
} }
return false; return false;
} }

View File

@ -1,16 +1,12 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Args; using Telegram.Bot.Args;
using Telegram.Bot.Types;
using Perfusion; using Perfusion;
using JetKarmaBot.Models;
using JetKarmaBot.Services; using JetKarmaBot.Services;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
class ChangeLocaleCommand : IChatCommand class LocaleCommand : IChatCommand
{ {
public IReadOnlyCollection<string> Names => new[] { "changelocale", "locale" }; public IReadOnlyCollection<string> Names => new[] { "changelocale", "locale" };
@ -28,12 +24,14 @@ namespace JetKarmaBot.Commands
return true; return true;
} }
db.Chats.Find(args.Message.Chat.Id).Locale = cmd.Parameters[0]; db.Chats.Find(args.Message.Chat.Id).Locale = cmd.Parameters[0];
db.SaveChanges();
currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale]; currentLocale = Locale[db.Chats.Find(args.Message.Chat.Id).Locale];
Client.SendTextMessageAsync( Client.SendTextMessageAsync(
args.Message.Chat.Id, args.Message.Chat.Id,
currentLocale["jetkarmabot.changelocale.justchanged"], currentLocale["jetkarmabot.changelocale.justchanged"],
replyToMessageId: args.Message.MessageId); replyToMessageId: args.Message.MessageId);
db.SaveChanges();
return true; return true;
} }
} }

View File

@ -64,7 +64,7 @@ namespace JetKarmaBot
Commands.Add(c.ResolveObject(new StartCommand())); Commands.Add(c.ResolveObject(new StartCommand()));
Commands.Add(c.ResolveObject(new AwardCommand(Me))); Commands.Add(c.ResolveObject(new AwardCommand(Me)));
Commands.Add(c.ResolveObject(new StatusCommand())); Commands.Add(c.ResolveObject(new StatusCommand()));
Commands.Add(c.ResolveObject(new ChangeLocaleCommand())); Commands.Add(c.ResolveObject(new LocaleCommand()));
} }
#endregion #endregion

View File

@ -20,6 +20,12 @@
<None Update="karma.cfg.json"> <None Update="karma.cfg.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Update="lang\en-US.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="lang\ru-RU.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,10 +1,7 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Perfusion; using Perfusion;
@ -18,23 +15,51 @@ namespace JetKarmaBot
public Localization(Config cfg) public Localization(Config cfg)
{ {
Log("Initializing..."); Log("Initializing...");
if (!Directory.Exists("lang")) string langsFolder = "lang";
Directory.CreateDirectory("lang"); if (!Directory.Exists(langsFolder))
Directory.CreateDirectory(langsFolder);
foreach (string lang in Directory.EnumerateFiles("lang")) foreach (string langFilePath in Directory.EnumerateFiles(langsFolder, "*.json"))
{ {
string langname = Path.GetFileName(lang).Split(".")[0]; try
Log("Found " + langname); {
locales[langname] = JObject.Parse(File.ReadAllText(lang)).ToObject<Dictionary<string, string>>(); string langName = Path.GetFileNameWithoutExtension(langFilePath);
string langKey = langName.ToLowerInvariant();
locales[langKey] = JObject.Parse(File.ReadAllText(langFilePath)).ToObject<Dictionary<string, string>>();
Log("Found " + langName);
}
catch (Exception e)
{
Log($"Error while parsing {langFilePath}!");
Log(e);
}
} }
Log("Initialized!");
if (locales.Any())
Log("Initialized!");
else
throw new FileNotFoundException($"No locales found in {langsFolder}!");
} }
public Locale this[string locale] public Locale this[string locale]
{ {
get => new Locale(locales[locale], locale); get
{
locale = locale.ToLowerInvariant();
return new Locale(locales[locale], locale);
}
} }
void Log(string Message) => Console.WriteLine($"[{nameof(Localization)}]: {Message}"); public bool ContainsLocale(string locale)
{
locale = locale.ToLowerInvariant();
return locales.ContainsKey(locale);
}
void Log(string Message)
=> Console.WriteLine($"[{nameof(Localization)}]: {Message}");
void Log(Exception e) => Console.WriteLine(e);
public class Locale public class Locale
{ {
private Dictionary<string, string> locale; private Dictionary<string, string> locale;
@ -44,20 +69,7 @@ namespace JetKarmaBot
this.locale = locale; this.locale = locale;
this.localeName = localeName; this.localeName = localeName;
} }
public string this[string name] public string this[string name] => locale.ContainsKey(name) ? locale[name] : "unknown";
{
get
{
if (!locale.ContainsKey(name))
{
return "unknown";
}
else
{
return locale[name];
}
}
}
} }
} }
} }