NadekoBot/src/NadekoBot/Services/Impl/NadekoStrings.cs

96 lines
3.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using NLog;
using System.Diagnostics;
using Newtonsoft.Json;
2017-05-22 23:59:31 +00:00
using System;
namespace NadekoBot.Services
{
public class NadekoStrings
{
public const string stringsPath = @"_strings/";
private readonly ImmutableDictionary<string, ImmutableDictionary<string, string>> responseStrings;
private readonly Logger _log;
2017-05-22 23:59:31 +00:00
/// <summary>
/// Used as failsafe in case response key doesn't exist in the selected or default language.
/// </summary>
private readonly CultureInfo _usCultureInfo = new CultureInfo("en-US");
2017-05-27 08:19:27 +00:00
private readonly ILocalization _localization;
2017-05-27 08:19:27 +00:00
public NadekoStrings(ILocalization loc)
{
_log = LogManager.GetCurrentClassLogger();
2017-05-27 08:19:27 +00:00
_localization = loc;
var sw = Stopwatch.StartNew();
var allLangsDict = new Dictionary<string, ImmutableDictionary<string, string>>(); // lang:(name:value)
foreach (var file in Directory.GetFiles(stringsPath))
{
var langDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(file));
allLangsDict.Add(GetLocaleName(file).ToLowerInvariant(), langDict.ToImmutableDictionary());
}
responseStrings = allLangsDict.ToImmutableDictionary();
sw.Stop();
_log.Info("Loaded {0} languages ({1}) in {2:F2}s",
responseStrings.Count,
string.Join(",", responseStrings.Keys),
sw.Elapsed.TotalSeconds);
}
private string GetLocaleName(string fileName)
{
var dotIndex = fileName.IndexOf('.') + 1;
var secondDotINdex = fileName.LastIndexOf('.');
return fileName.Substring(dotIndex, secondDotINdex - dotIndex);
}
2017-05-22 23:59:31 +00:00
private string GetString(string text, CultureInfo cultureInfo)
{
if (!responseStrings.TryGetValue(cultureInfo.Name.ToLowerInvariant(), out ImmutableDictionary<string, string> strings))
return null;
strings.TryGetValue(text, out string val);
return val;
}
2017-05-22 23:59:31 +00:00
2017-05-27 08:19:27 +00:00
public string GetText(string key, ulong guildId, string lowerModuleTypeName, params object[] replacements) =>
GetText(key, _localization.GetCultureInfo(guildId), lowerModuleTypeName);
2017-05-22 23:59:31 +00:00
public string GetText(string key, CultureInfo cultureInfo, string lowerModuleTypeName)
{
var text = GetString(lowerModuleTypeName + "_" + key, cultureInfo);
if (string.IsNullOrWhiteSpace(text))
{
LogManager.GetCurrentClassLogger().Warn(lowerModuleTypeName + "_" + key + " key is missing from " + cultureInfo + " response strings. PLEASE REPORT THIS.");
text = GetString(lowerModuleTypeName + "_" + key, _usCultureInfo) ?? $"Error: dkey {lowerModuleTypeName + "_" + key} not found!";
if (string.IsNullOrWhiteSpace(text))
return "I can't tell you if the command is executed, because there was an error printing out the response. Key '" +
lowerModuleTypeName + "_" + key + "' " + "is missing from resources. Please report this.";
}
return text;
}
public string GetText(string key, CultureInfo cultureInfo, string lowerModuleTypeName,
params object[] replacements)
{
try
{
return string.Format(GetText(key, cultureInfo, lowerModuleTypeName), replacements);
}
catch (FormatException)
{
return "I can't tell you if the command is executed, because there was an error printing out the response. Key '" +
lowerModuleTypeName + "_" + key + "' " + "is not properly formatted. Please report this.";
}
}
}
}