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

@ -25,9 +25,17 @@ namespace JetKarmaBot
if (cmd.UserName != null && cmd.UserName != BotUser.Username) // directed not at us!
return false;
try
{
if (commands.ContainsKey(cmd.Command))
return commands[cmd.Command].Execute(cmd, args);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
return false;
}

View File

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

View File

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

View File

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

View File

@ -1,10 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Perfusion;
@ -18,23 +15,51 @@ namespace JetKarmaBot
public Localization(Config cfg)
{
Log("Initializing...");
if (!Directory.Exists("lang"))
Directory.CreateDirectory("lang");
string langsFolder = "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];
Log("Found " + langname);
locales[langname] = JObject.Parse(File.ReadAllText(lang)).ToObject<Dictionary<string, string>>();
try
{
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);
}
}
if (locales.Any())
Log("Initialized!");
else
throw new FileNotFoundException($"No locales found in {langsFolder}!");
}
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
{
private Dictionary<string, string> locale;
@ -44,20 +69,7 @@ namespace JetKarmaBot
this.locale = locale;
this.localeName = localeName;
}
public string this[string name]
{
get
{
if (!locale.ContainsKey(name))
{
return "unknown";
}
else
{
return locale[name];
}
}
}
public string this[string name] => locale.ContainsKey(name) ? locale[name] : "unknown";
}
}
}