2017-04-16 21:31:12 +00:00
|
|
|
|
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;
|
2017-04-16 21:31:12 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Services
|
|
|
|
|
{
|
|
|
|
|
public class NadekoStrings
|
|
|
|
|
{
|
2017-05-08 11:43:38 +00:00
|
|
|
|
public const string stringsPath = @"_strings/";
|
2017-04-16 21:31:12 +00:00
|
|
|
|
|
|
|
|
|
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-04-16 21:31:12 +00:00
|
|
|
|
|
2017-05-27 08:19:27 +00:00
|
|
|
|
public NadekoStrings(ILocalization loc)
|
2017-04-16 21:31:12 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-05-27 08:19:27 +00:00
|
|
|
|
_localization = loc;
|
|
|
|
|
|
2017-04-16 21:31:12 +00:00
|
|
|
|
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)
|
2017-04-16 21:31:12 +00:00
|
|
|
|
{
|
|
|
|
|
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.";
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-16 21:31:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|