2017-11-15 01:08:20 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-11-16 00:51:36 +01:00
|
|
|
|
using Discord.Commands;
|
2017-11-15 01:08:20 +01:00
|
|
|
|
using Discord.WebSocket;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Logger;
|
2017-11-15 01:08:20 +01:00
|
|
|
|
using StackExchange.Redis;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
using Utf8Json;
|
2017-11-15 01:08:20 +01:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Lib.Localization
|
2017-11-15 01:08:20 +01:00
|
|
|
|
{
|
|
|
|
|
public class TranslationHandler : ITranslationHandler
|
|
|
|
|
{
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2017-11-15 01:08:20 +01:00
|
|
|
|
private readonly IDatabase _redis;
|
|
|
|
|
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> _translations;
|
|
|
|
|
private Dictionary<ulong, string> _serverLanguages;
|
2017-11-16 00:51:36 +01:00
|
|
|
|
private List<string> _supportedLanguages;
|
2017-11-15 01:08:20 +01:00
|
|
|
|
|
2018-01-20 01:38:49 +01:00
|
|
|
|
public TranslationHandler(IReadOnlyCollection<SocketGuild> clientGuilds, IDatabase redis, IGeekbotLogger logger)
|
2017-11-15 01:08:20 +01:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_redis = redis;
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, "Loading Translations");
|
2017-11-15 01:08:20 +01:00
|
|
|
|
LoadTranslations();
|
|
|
|
|
LoadServerLanguages(clientGuilds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadTranslations()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-03 00:56:06 +02:00
|
|
|
|
var translationFile = File.ReadAllText(Path.GetFullPath("./Lib/Localization/Translations.json"));
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var rawTranslations = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(translationFile);
|
2017-11-16 00:51:36 +01:00
|
|
|
|
var sortedPerLanguage = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
|
|
|
|
|
foreach (var command in rawTranslations)
|
|
|
|
|
{
|
|
|
|
|
foreach (var str in command.Value)
|
|
|
|
|
{
|
|
|
|
|
foreach (var lang in str.Value)
|
|
|
|
|
{
|
|
|
|
|
if (!sortedPerLanguage.ContainsKey(lang.Key))
|
|
|
|
|
{
|
|
|
|
|
var commandDict = new Dictionary<string, Dictionary<string, string>>();
|
|
|
|
|
var strDict = new Dictionary<string, string>();
|
|
|
|
|
strDict.Add(str.Key, lang.Value);
|
|
|
|
|
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;
|
|
|
|
|
|
2017-11-15 01:08:20 +01:00
|
|
|
|
_supportedLanguages = new List<string>();
|
2017-11-16 00:51:36 +01:00
|
|
|
|
foreach (var lang in sortedPerLanguage)
|
2017-11-15 01:08:20 +01:00
|
|
|
|
{
|
|
|
|
|
_supportedLanguages.Add(lang.Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Failed to load Translations", e);
|
2018-05-06 02:00:45 +02:00
|
|
|
|
Environment.Exit(GeekbotExitCode.TranslationsFailed.GetHashCode());
|
2017-11-15 01:08:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-16 00:51:36 +01:00
|
|
|
|
|
2017-11-15 01:08:20 +01:00
|
|
|
|
private void LoadServerLanguages(IReadOnlyCollection<SocketGuild> clientGuilds)
|
|
|
|
|
{
|
|
|
|
|
_serverLanguages = new Dictionary<ulong, string>();
|
|
|
|
|
foreach (var guild in clientGuilds)
|
|
|
|
|
{
|
|
|
|
|
var language = _redis.HashGet($"{guild.Id}:Settings", "Language");
|
|
|
|
|
if (string.IsNullOrEmpty(language) || !_supportedLanguages.Contains(language))
|
|
|
|
|
{
|
|
|
|
|
_serverLanguages[guild.Id] = "EN";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_serverLanguages[guild.Id] = language.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetString(ulong guildId, string command, string stringName)
|
|
|
|
|
{
|
2017-11-16 00:51:36 +01:00
|
|
|
|
var translation = _translations[_serverLanguages[guildId]][command][stringName];
|
2017-11-15 01:08:20 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(translation)) return translation;
|
|
|
|
|
translation = _translations[command][stringName]["EN"];
|
|
|
|
|
if (string.IsNullOrWhiteSpace(translation))
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Warning(LogSource.Geekbot, $"No translation found for {command} - {stringName}");
|
2017-11-15 01:08:20 +01:00
|
|
|
|
}
|
|
|
|
|
return translation;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-16 00:51:36 +01:00
|
|
|
|
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)
|
|
|
|
|
{
|
2018-05-06 02:00:45 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "No translations for command found", e);
|
|
|
|
|
return new Dictionary<string, string>();
|
2017-11-16 00:51:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-16 17:19:43 +01:00
|
|
|
|
|
|
|
|
|
public Dictionary<string, string> GetDict(ICommandContext context, string command)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _translations[_serverLanguages[context.Guild.Id]][command];
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 02:00:45 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "No translations for command found", e);
|
2017-11-16 17:19:43 +01:00
|
|
|
|
return new Dictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-16 00:51:36 +01:00
|
|
|
|
|
2017-11-15 01:08:20 +01:00
|
|
|
|
public bool SetLanguage(ulong guildId, string language)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!_supportedLanguages.Contains(language)) return false;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_redis.HashSet($"{guildId}:Settings", new[]{ new HashEntry("Language", language) });
|
2017-11-15 01:08:20 +01:00
|
|
|
|
_serverLanguages[guildId] = language;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Error while changing language", e);
|
2017-11-15 01:08:20 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> GetSupportedLanguages()
|
|
|
|
|
{
|
|
|
|
|
return _supportedLanguages;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|