mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
localization fixes
This commit is contained in:
parent
d55217e539
commit
ef3db7ac50
@ -25,10 +25,18 @@ namespace JetKarmaBot
|
||||
if (cmd.UserName != null && cmd.UserName != BotUser.Username) // directed not at us!
|
||||
return false;
|
||||
|
||||
if (commands.ContainsKey(cmd.Command))
|
||||
return commands[cmd.Command].Execute(cmd, args);
|
||||
try
|
||||
{
|
||||
if (commands.ContainsKey(cmd.Command))
|
||||
return commands[cmd.Command].Execute(cmd, args);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
Log("Initialized!");
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user