Make Translations easier to get
This commit is contained in:
parent
7584b09d35
commit
f0ba7bb9fc
4 changed files with 78 additions and 26 deletions
|
@ -5,7 +5,6 @@ using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Geekbot.net.Lib;
|
using Geekbot.net.Lib;
|
||||||
using Serilog;
|
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
|
||||||
namespace Geekbot.net.Commands
|
namespace Geekbot.net.Commands
|
||||||
|
@ -121,7 +120,8 @@ namespace Geekbot.net.Commands
|
||||||
var success = _translation.SetLanguage(Context.Guild.Id, language);
|
var success = _translation.SetLanguage(Context.Guild.Id, language);
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
await ReplyAsync(_translation.GetString(Context.Guild.Id, "LanguageChanger", "Confirm"));
|
var trans = _translation.GetDict(Context);
|
||||||
|
await ReplyAsync(trans["Confirm"]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await ReplyAsync(
|
await ReplyAsync(
|
||||||
|
@ -132,5 +132,21 @@ namespace Geekbot.net.Commands
|
||||||
_errorHandler.HandleCommandException(e, Context);
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Command("lang", RunMode = RunMode.Async)]
|
||||||
|
[Remarks(CommandCategories.Admin)]
|
||||||
|
[Summary("Change the bots language")]
|
||||||
|
public async Task getLanguage()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var trans = _translation.GetDict(Context);
|
||||||
|
await ReplyAsync(trans["GetLanguage"]);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
@ -14,7 +15,7 @@ namespace Geekbot.net.Lib
|
||||||
private readonly IDatabase _redis;
|
private readonly IDatabase _redis;
|
||||||
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> _translations;
|
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> _translations;
|
||||||
private Dictionary<ulong, string> _serverLanguages;
|
private Dictionary<ulong, string> _serverLanguages;
|
||||||
public List<string> _supportedLanguages;
|
private List<string> _supportedLanguages;
|
||||||
|
|
||||||
public TranslationHandler(IReadOnlyCollection<SocketGuild> clientGuilds, IDatabase redis, ILogger logger)
|
public TranslationHandler(IReadOnlyCollection<SocketGuild> clientGuilds, IDatabase redis, ILogger logger)
|
||||||
{
|
{
|
||||||
|
@ -22,7 +23,6 @@ namespace Geekbot.net.Lib
|
||||||
_redis = redis;
|
_redis = redis;
|
||||||
_logger.Information("[Geekbot] Loading Translations");
|
_logger.Information("[Geekbot] Loading Translations");
|
||||||
LoadTranslations();
|
LoadTranslations();
|
||||||
CheckSupportedLanguages();
|
|
||||||
LoadServerLanguages(clientGuilds);
|
LoadServerLanguages(clientGuilds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,24 +30,40 @@ namespace Geekbot.net.Lib
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var translations = File.ReadAllText(Path.GetFullPath("./Storage/Translations.json"));
|
var translationFile = File.ReadAllText(Path.GetFullPath("./Storage/Translations.json"));
|
||||||
_translations =
|
var rawTranslations = Utf8Json.JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(translationFile);
|
||||||
Utf8Json.JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(
|
var sortedPerLanguage = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
|
||||||
translations);
|
foreach (var command in rawTranslations)
|
||||||
}
|
{
|
||||||
catch (Exception e)
|
foreach (var str in command.Value)
|
||||||
{
|
{
|
||||||
_logger.Fatal(e, "Failed to load Translations");
|
foreach (var lang in str.Value)
|
||||||
Environment.Exit(110);
|
{
|
||||||
}
|
if (!sortedPerLanguage.ContainsKey(lang.Key))
|
||||||
}
|
{
|
||||||
|
var commandDict = new Dictionary<string, Dictionary<string, string>>();
|
||||||
private void CheckSupportedLanguages()
|
var strDict = new Dictionary<string, string>();
|
||||||
{
|
strDict.Add(str.Key, lang.Value);
|
||||||
try
|
commandDict.Add(command.Key, strDict);
|
||||||
{
|
sortedPerLanguage.Add(lang.Key, commandDict);
|
||||||
|
}
|
||||||
|
if (!sortedPerLanguage[lang.Key].ContainsKey(command.Key))
|
||||||
|
{
|
||||||
|
var strDict = new Dictionary<string, string>();
|
||||||
|
strDict.Add(str.Key, lang.Value);
|
||||||
|
sortedPerLanguage[lang.Key].Add(command.Key, strDict);
|
||||||
|
}
|
||||||
|
if (!sortedPerLanguage[lang.Key][command.Key].ContainsKey(str.Key))
|
||||||
|
{
|
||||||
|
sortedPerLanguage[lang.Key][command.Key].Add(str.Key, lang.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_translations = sortedPerLanguage;
|
||||||
|
|
||||||
_supportedLanguages = new List<string>();
|
_supportedLanguages = new List<string>();
|
||||||
foreach (var lang in _translations.First().Value.First().Value)
|
foreach (var lang in sortedPerLanguage)
|
||||||
{
|
{
|
||||||
_supportedLanguages.Add(lang.Key);
|
_supportedLanguages.Add(lang.Key);
|
||||||
}
|
}
|
||||||
|
@ -58,7 +74,7 @@ namespace Geekbot.net.Lib
|
||||||
Environment.Exit(110);
|
Environment.Exit(110);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LoadServerLanguages(IReadOnlyCollection<SocketGuild> clientGuilds)
|
private void LoadServerLanguages(IReadOnlyCollection<SocketGuild> clientGuilds)
|
||||||
{
|
{
|
||||||
_serverLanguages = new Dictionary<ulong, string>();
|
_serverLanguages = new Dictionary<ulong, string>();
|
||||||
|
@ -78,7 +94,7 @@ namespace Geekbot.net.Lib
|
||||||
|
|
||||||
public string GetString(ulong guildId, string command, string stringName)
|
public string GetString(ulong guildId, string command, string stringName)
|
||||||
{
|
{
|
||||||
var translation = _translations[command][stringName][_serverLanguages[guildId]];
|
var translation = _translations[_serverLanguages[guildId]][command][stringName];
|
||||||
if (!string.IsNullOrWhiteSpace(translation)) return translation;
|
if (!string.IsNullOrWhiteSpace(translation)) return translation;
|
||||||
translation = _translations[command][stringName]["EN"];
|
translation = _translations[command][stringName]["EN"];
|
||||||
if (string.IsNullOrWhiteSpace(translation))
|
if (string.IsNullOrWhiteSpace(translation))
|
||||||
|
@ -88,6 +104,20 @@ namespace Geekbot.net.Lib
|
||||||
return translation;
|
return translation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, string> GetDict(ICommandContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower();
|
||||||
|
return _translations[_serverLanguages[context.Guild.Id]][command];
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.Error(e, "lol nope");
|
||||||
|
return new Dictionary<string, string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool SetLanguage(ulong guildId, string language)
|
public bool SetLanguage(ulong guildId, string language)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -113,6 +143,7 @@ namespace Geekbot.net.Lib
|
||||||
public interface ITranslationHandler
|
public interface ITranslationHandler
|
||||||
{
|
{
|
||||||
string GetString(ulong guildId, string command, string stringName);
|
string GetString(ulong guildId, string command, string stringName);
|
||||||
|
Dictionary<string, string> GetDict(ICommandContext context);
|
||||||
bool SetLanguage(ulong guildId, string language);
|
bool SetLanguage(ulong guildId, string language);
|
||||||
List<string> GetSupportedLanguages();
|
List<string> GetSupportedLanguages();
|
||||||
}
|
}
|
||||||
|
|
|
@ -248,6 +248,7 @@ namespace Geekbot.net
|
||||||
case LogSeverity.Critical:
|
case LogSeverity.Critical:
|
||||||
case LogSeverity.Error:
|
case LogSeverity.Error:
|
||||||
case LogSeverity.Warning:
|
case LogSeverity.Warning:
|
||||||
|
if (logMessage.Contains("VOICE_STATE_UPDATE")) break;
|
||||||
logger.Error(message.Exception, logMessage);
|
logger.Error(message.Exception, logMessage);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
{
|
{
|
||||||
"LanguageChanger": {
|
"admin": {
|
||||||
"Confirm": {
|
"NewLanguageSet": {
|
||||||
"EN": "I will in english from now on",
|
"EN": "I will in english from now on",
|
||||||
"CHDE": "I werd ab jetzt in schwiizerdüütsch antworte, äuuä"
|
"CHDE": "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä"
|
||||||
|
},
|
||||||
|
"GetLanguage": {
|
||||||
|
"EN": "I'm talking english",
|
||||||
|
"CHDE": "I red schwiizerdüütsch"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue