Lazyload server language setting in translationHandler

This commit is contained in:
Runebaas 2018-05-26 02:55:18 +02:00
parent 35f0a5c8f8
commit 80c2bd8500
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
5 changed files with 36 additions and 39 deletions

View file

@ -138,7 +138,7 @@ namespace Geekbot.net.Commands.Admin
}
await ReplyAsync(
$"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.GetSupportedLanguages())}");
$"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.SupportedLanguages)}");
}
catch (Exception e)
{

View file

@ -9,9 +9,6 @@ namespace Geekbot.net.Lib.AlmostRedis
{
private readonly GeekbotLogger _logger;
private readonly RunParameters _runParameters;
private IDatabase _database;
private ConnectionMultiplexer _connection;
private IAlmostRedis _almostRedisImplementation;
public AlmostRedis(GeekbotLogger logger, RunParameters runParameters)
{
@ -21,24 +18,18 @@ namespace Geekbot.net.Lib.AlmostRedis
public void Connect()
{
_connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}");
_database = _connection.GetDatabase(int.Parse(_runParameters.RedisDatabase));
_logger.Information(LogSource.Redis, $"Connected to Redis on {_connection.Configuration} at {_database.Database}");
Connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}");
Db = Connection.GetDatabase(int.Parse(_runParameters.RedisDatabase));
_logger.Information(LogSource.Redis, $"Connected to Redis on {Connection.Configuration} at {Db.Database}");
}
public IDatabase Db
{
get { return _database; }
}
public IDatabase Db { get; private set; }
public ConnectionMultiplexer Connection
{
get { return _connection; }
}
public ConnectionMultiplexer Connection { get; private set; }
public IEnumerable<RedisKey> GetAllKeys()
{
return _connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys();
return Connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys();
}
}
}

View file

@ -9,6 +9,6 @@ namespace Geekbot.net.Lib.Localization
Dictionary<string, string> GetDict(ICommandContext context);
Dictionary<string, string> GetDict(ICommandContext context, string command);
bool SetLanguage(ulong guildId, string language);
List<string> GetSupportedLanguages();
List<string> SupportedLanguages { get; }
}
}

View file

@ -16,17 +16,17 @@ namespace Geekbot.net.Lib.Localization
{
private readonly DatabaseContext _database;
private readonly IGeekbotLogger _logger;
private readonly Dictionary<ulong, string> _serverLanguages;
private Dictionary<string, Dictionary<string, Dictionary<string, string>>> _translations;
private Dictionary<ulong, string> _serverLanguages;
private List<string> _supportedLanguages;
public TranslationHandler(IReadOnlyCollection<SocketGuild> clientGuilds, DatabaseContext database, IGeekbotLogger logger)
public TranslationHandler(DatabaseContext database, IGeekbotLogger logger)
{
_database = database;
_logger = logger;
_logger.Information(LogSource.Geekbot, "Loading Translations");
LoadTranslations();
LoadServerLanguages(clientGuilds);
_serverLanguages = new Dictionary<ulong, string>();
}
private void LoadTranslations()
@ -78,28 +78,37 @@ namespace Geekbot.net.Lib.Localization
}
}
private void LoadServerLanguages(IReadOnlyCollection<SocketGuild> clientGuilds)
private string GetServerLanguage(ulong guildId)
{
_serverLanguages = new Dictionary<ulong, string>();
foreach (var guild in clientGuilds)
try
{
var language = _database.GuildSettings
.FirstOrDefault(g => g.GuildId.Equals(guild.Id.AsLong()))
?.Language ?? "EN";
if (string.IsNullOrEmpty(language) || !_supportedLanguages.Contains(language))
string lang;
try
{
_serverLanguages[guild.Id] = "EN";
lang = _serverLanguages[guildId];
if (!string.IsNullOrEmpty(lang))
{
return lang;
}
throw new Exception();
}
else
catch
{
_serverLanguages[guild.Id] = language;
lang = GetGuild(guildId).Language ?? "EN";
_serverLanguages[guildId] = lang;
return lang;
}
}
catch (Exception e)
{
_logger.Error(LogSource.Geekbot, "Could not get guild langage", e);
return "EN";
}
}
public string GetString(ulong guildId, string command, string stringName)
{
var translation = _translations[_serverLanguages[guildId]][command][stringName];
var translation = _translations[GetServerLanguage(guildId)][command][stringName];
if (!string.IsNullOrWhiteSpace(translation)) return translation;
translation = _translations[command][stringName]["EN"];
if (string.IsNullOrWhiteSpace(translation))
@ -114,7 +123,7 @@ namespace Geekbot.net.Lib.Localization
try
{
var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower();
return _translations[_serverLanguages[context.Guild.Id]][command];
return _translations[GetServerLanguage(context.Guild.Id)][command];
}
catch (Exception e)
{
@ -127,7 +136,7 @@ namespace Geekbot.net.Lib.Localization
{
try
{
return _translations[_serverLanguages[context.Guild.Id]][command];
return _translations[GetServerLanguage(context.Guild.Id)][command];
}
catch (Exception e)
{
@ -154,10 +163,7 @@ namespace Geekbot.net.Lib.Localization
}
}
public List<string> GetSupportedLanguages()
{
return _supportedLanguages;
}
public List<string> SupportedLanguages => _supportedLanguages;
private GuildSettingsModel GetGuild(ulong guildId)
{

View file

@ -152,7 +152,7 @@ namespace Geekbot.net
_logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers");
_logger.Information(LogSource.Geekbot, "Registering Stuff");
var translationHandler = new TranslationHandler(_client.Guilds, _database, _logger);
var translationHandler = new TranslationHandler(_database, _logger);
var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors);
var reactionListener = new ReactionListener(_redis.Db);
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());