@@ -1,7 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading.Tasks;
@@ -15,11 +18,13 @@ namespace Discord.Interactions
{
private const string NameIdentifier = "name";
private const string DescriptionIdentifier = "description";
private const string SpaceToken = "~";
private readonly string _baseResource;
private readonly Assembly _assembly;
private static readonly ConcurrentDictionary<string, ResourceManager> _localizerCache = new();
private readonly IEnumerable<CultureInfo> _supportedLocales;
private readonly IEnumerable<string> _resourceNames;
/// <summary>
/// Initializes a new instance of the <see cref="ResxLocalizationManager"/> class.
@@ -32,6 +37,7 @@ namespace Discord.Interactions
_baseResource = baseResource;
_assembly = assembly;
_supportedLocales = supportedLocales;
_resourceNames = assembly.GetManifestResourceNames();
}
/// <inheritdoc />
@@ -44,20 +50,19 @@ namespace Discord.Interactions
private IDictionary<string, string> GetValues(IList<string> key, string identifier)
{
var result = new Dictionary<string, string>();
var resourceName = (_baseResource + "." + string.Join(".", key)).Replace(" ", SpaceToken);
if (!_resourceNames.Any(x => string.Equals(resourceName + ".resources", x, StringComparison.OrdinalIgnoreCase)))
return ImmutableDictionary<string, string>.Empty;
var resourceName = _baseResource + "." + string.Join(".", key);
var result = new Dictionary<string, string>( );
var resourceManager = _localizerCache.GetOrAdd(resourceName, new ResourceManager(resourceName, _assembly));
foreach (var locale in _supportedLocales)
{
try
{
var value = resourceManager.GetString(identifier, locale);
if (value is not null)
result[locale.Name] = value;
}
catch (MissingManifestResourceException){}
var value = resourceManager.GetString(identifier, locale);
if (value is not null)
result[locale.Name] = value;
}
return result;