Delete the TranslationHandler and the old translations file. Refactor GeekbotCommandBase to get the server language from guild settings. Create DateLocalization to create a localized relative time remaining string.

This commit is contained in:
runebaas 2020-08-14 23:15:11 +02:00
parent 078c884df7
commit 33829e91bc
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
22 changed files with 156 additions and 579 deletions

View file

@ -31,10 +31,4 @@
<PackageReference Include="YamlDotNet" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<Content Include="Localization\Translations.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View file

@ -1,9 +1,6 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.Net;
using Geekbot.Core.Localization;
using Geekbot.Core.Logger;
using SharpRaven;
using SharpRaven.Data;
@ -14,14 +11,14 @@ namespace Geekbot.Core.ErrorHandling
public class ErrorHandler : IErrorHandler
{
private readonly IGeekbotLogger _logger;
private readonly ITranslationHandler _translation;
private readonly Func<string> _getDefaultErrorText;
private readonly IRavenClient _raven;
private readonly bool _errorsInChat;
public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, RunParameters runParameters)
public ErrorHandler(IGeekbotLogger logger, RunParameters runParameters, Func<string> getDefaultErrorText)
{
_logger = logger;
_translation = translation;
_getDefaultErrorText = getDefaultErrorText;
_errorsInChat = runParameters.ExposeErrors;
var sentryDsn = runParameters.SentryEndpoint;
@ -40,7 +37,9 @@ namespace Geekbot.Core.ErrorHandling
{
try
{
var errorString = errorMessage == "def" ? await _translation.GetString(context.Guild?.Id ?? 0, "errorHandler", "SomethingWentWrong") : errorMessage;
var errorString = errorMessage == "def"
? _getDefaultErrorText()
: errorMessage;
var errorObj = SimpleConextConverter.ConvertContext(context);
if (e.Message.Contains("50007")) return;
if (e.Message.Contains("50013")) return;
@ -76,17 +75,6 @@ namespace Geekbot.Core.ErrorHandling
}
}
public async Task HandleHttpException(HttpException e, ICommandContext context)
{
var errorStrings = await _translation.GetDict(context, "httpErrors");
switch(e.HttpCode)
{
case HttpStatusCode.Forbidden:
await context.Channel.SendMessageAsync(errorStrings["403"]);
break;
}
}
private void ReportExternal(Exception e, MessageDto errorObj)
{
if (_raven == null) return;

View file

@ -1,13 +1,11 @@
using System;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.Net;
namespace Geekbot.Core.ErrorHandling
{
public interface IErrorHandler
{
Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def");
Task HandleHttpException(HttpException e, ICommandContext context);
}
}

View file

@ -1,26 +1,29 @@
using System.Globalization;
using System.Threading;
using Discord.Commands;
using Geekbot.Core.Database.Models;
using Geekbot.Core.ErrorHandling;
using Geekbot.Core.Localization;
using Geekbot.Core.GuildSettingsManager;
namespace Geekbot.Core
{
public class GeekbotCommandBase : ModuleBase<ICommandContext>
{
protected readonly IGuildSettingsManager GuildSettingsManager;
protected GuildSettingsModel GuildSettings;
protected readonly IErrorHandler ErrorHandler;
protected readonly ITranslationHandler Translations;
protected GeekbotCommandBase(IErrorHandler errorHandler, ITranslationHandler translations)
protected GeekbotCommandBase(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager)
{
GuildSettingsManager = guildSettingsManager;
ErrorHandler = errorHandler;
Translations = translations;
}
protected override void BeforeExecute(CommandInfo command)
{
base.BeforeExecute(command);
var language = Translations.GetServerLanguage(Context.Guild.Id);
GuildSettings = GuildSettingsManager.GetSettings(Context?.Guild?.Id ?? 0);
var language = GuildSettings.Language;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language == "CHDE" ? "de-ch" : language);
}
}

View file

@ -1,17 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Discord.Commands;
namespace Geekbot.Core.Localization
{
public interface ITranslationHandler
{
Task<string> GetString(ulong guildId, string command, string stringName);
string GetString(string language, string command, string stringName);
Task<Dictionary<string, string>> GetDict(ICommandContext context, string command);
Task<TranslationGuildContext> GetGuildContext(ICommandContext context);
Task<bool> SetLanguage(ulong guildId, string language);
List<string> SupportedLanguages { get; }
string GetServerLanguage(ulong guildId);
}
}

View file

@ -1,90 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Geekbot.Core.Localization
{
public class TranslationGuildContext
{
public ITranslationHandler TranslationHandler { get; }
public string Language { get; }
public Dictionary<string, string> Dict { get; }
public TranslationGuildContext(ITranslationHandler translationHandler, string language, Dictionary<string, string> dict)
{
TranslationHandler = translationHandler;
Language = language;
Dict = dict;
}
public string GetString(string stringToFormat, params object[] args)
{
return string.Format(Dict[stringToFormat] ?? "", args);
}
public string FormatDateTimeAsRemaining(DateTimeOffset dateTime)
{
var remaining = dateTime - DateTimeOffset.Now;
const string formattable = "{0} {1}";
var sb = new StringBuilder();
if (remaining.Days > 0)
{
var s = GetTimeString(TimeTypes.Days);
sb.AppendFormat(formattable, remaining.Days, GetSingOrPlur(remaining.Days, s));
}
if (remaining.Hours > 0)
{
if (sb.Length > 0) sb.Append(", ");
var s = GetTimeString(TimeTypes.Hours);
sb.AppendFormat(formattable, remaining.Hours, GetSingOrPlur(remaining.Hours, s));
}
if (remaining.Minutes > 0)
{
if (sb.Length > 0) sb.Append(", ");
var s = GetTimeString(TimeTypes.Minutes);
sb.AppendFormat(formattable, remaining.Minutes, GetSingOrPlur(remaining.Minutes, s));
}
if (remaining.Seconds > 0)
{
if (sb.Length > 0)
{
var and = TranslationHandler.GetString(Language, "dateTime", "And");
sb.AppendFormat(" {0} ", and);
}
var s = GetTimeString(TimeTypes.Seconds);
sb.AppendFormat(formattable, remaining.Seconds, GetSingOrPlur(remaining.Seconds, s));
}
return sb.ToString().Trim();
}
public Task<bool> SetLanguage(ulong guildId, string language)
{
return TranslationHandler.SetLanguage(guildId, language);
}
private string GetTimeString(TimeTypes type)
{
return TranslationHandler.GetString(Language, "dateTime", type.ToString());
}
private string GetSingOrPlur(int number, string rawString)
{
var versions = rawString.Split('|');
return number == 1 ? versions[0] : versions[1];
}
private enum TimeTypes
{
Days,
Hours,
Minutes,
Seconds
}
}
}

View file

@ -1,194 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.Core.GuildSettingsManager;
using Geekbot.Core.Logger;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
namespace Geekbot.Core.Localization
{
public class TranslationHandler : ITranslationHandler
{
private readonly IGeekbotLogger _logger;
private readonly IGuildSettingsManager _guildSettingsManager;
private readonly Dictionary<ulong, string> _serverLanguages;
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> _translations;
public TranslationHandler(IGeekbotLogger logger, IGuildSettingsManager guildSettingsManager)
{
_logger = logger;
_guildSettingsManager = guildSettingsManager;
_logger.Information(LogSource.Geekbot, "Loading Translations");
LoadTranslations();
_serverLanguages = new Dictionary<ulong, string>();
}
private void LoadTranslations()
{
try
{
// Read the file
var translationFile = File.ReadAllText(Path.GetFullPath("./Localization/Translations.yml"));
// Deserialize
var input = new StringReader(translationFile);
var mergingParser = new MergingParser(new Parser(input));
var deserializer = new DeserializerBuilder().Build();
var rawTranslations = deserializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(mergingParser);
// Sort
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>
{
{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>
{
{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;
// Find Languages
SupportedLanguages = new List<string>();
foreach (var lang in sortedPerLanguage)
{
SupportedLanguages.Add(lang.Key);
}
}
catch (Exception e)
{
_logger.Error(LogSource.Geekbot, "Failed to load Translations", e);
Environment.Exit(GeekbotExitCode.TranslationsFailed.GetHashCode());
}
}
public string GetServerLanguage(ulong guildId)
{
try
{
string lang;
try
{
lang = _serverLanguages[guildId];
if (!string.IsNullOrEmpty(lang))
{
return lang;
}
throw new Exception();
}
catch
{
lang = _guildSettingsManager.GetSettings(guildId, false)?.Language ?? "EN";
_serverLanguages[guildId] = lang;
return lang;
}
}
catch (Exception e)
{
_logger.Error(LogSource.Geekbot, "Could not get guild language", e);
return "EN";
}
}
public async Task<string> GetString(ulong guildId, string command, string stringName)
{
var serverLang = GetServerLanguage(guildId);
return GetString(serverLang, command, stringName);
}
public string GetString(string language, string command, string stringName)
{
var translation = _translations[language][command][stringName];
if (!string.IsNullOrWhiteSpace(translation)) return translation;
translation = _translations[command][stringName]["EN"];
if (string.IsNullOrWhiteSpace(translation))
{
_logger.Warning(LogSource.Geekbot, $"No translation found for {command} - {stringName}");
}
return translation;
}
private async Task<Dictionary<string, string>> GetDict(ICommandContext context)
{
try
{
var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower();
var serverLanguage = GetServerLanguage(context.Guild?.Id ?? 0);
return _translations[serverLanguage][command];
}
catch (Exception e)
{
_logger.Error(LogSource.Geekbot, "No translations for command found", e);
return new Dictionary<string, string>();
}
}
public async Task<TranslationGuildContext> GetGuildContext(ICommandContext context)
{
var dict = await GetDict(context);
var language = GetServerLanguage(context.Guild?.Id ?? 0);
return new TranslationGuildContext(this, language, dict);
}
public async Task<Dictionary<string, string>> GetDict(ICommandContext context, string command)
{
try
{
var serverLanguage = GetServerLanguage(context.Guild?.Id ?? 0);
return _translations[serverLanguage][command];
}
catch (Exception e)
{
_logger.Error(LogSource.Geekbot, "No translations for command found", e);
return new Dictionary<string, string>();
}
}
public async Task<bool> SetLanguage(ulong guildId, string language)
{
try
{
if (!SupportedLanguages.Contains(language)) return false;
var guild = _guildSettingsManager.GetSettings(guildId);
guild.Language = language;
await _guildSettingsManager.UpdateSettings(guild);
_serverLanguages[guildId] = language;
return true;
}
catch (Exception e)
{
_logger.Error(LogSource.Geekbot, "Error while changing language", e);
return false;
}
}
public List<string> SupportedLanguages { get; private set; }
}
}

View file

@ -1,25 +0,0 @@
---
dateTime:
Days:
EN: "day|days"
CHDE: "tag|täg"
Hours:
EN: "hour|hours"
CHDE: "stund|stunde"
Minutes:
EN: "minute|minutes"
CHDE: "minute|minute"
Seconds:
EN: "second|seconds"
CHDE: "sekunde|sekunde"
And:
EN: "and"
CHDE: "und"
errorHandler:
SomethingWentWrong:
EN: "Something went wrong :confused:"
CHDE: "Öppis isch schief gange :confused:"
httpErrors:
403:
EN: "Seems like i don't have enough permission to that :confused:"
CHDE: "Gseht danach us das ich nid gnueg recht han zum das mache :confused:"