67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
using TechnologyAssembler.Core.Language;
|
|
|
|
namespace HashCalculator.GUI
|
|
{
|
|
internal static class Translator
|
|
{
|
|
private static ITranslationProvider? _provider;
|
|
public static ITranslationProvider? Provider
|
|
{
|
|
get => Interlocked.CompareExchange(ref _provider, null, null);
|
|
set
|
|
{
|
|
var provider = value is null
|
|
? null
|
|
: new CachedCsfTranslationProvider(value);
|
|
Interlocked.Exchange(ref _provider, provider);
|
|
ProviderChanged?.Invoke(null, null);
|
|
}
|
|
}
|
|
|
|
public static bool HasProvider => Provider != null;
|
|
public static EventHandler? ProviderChanged;
|
|
|
|
public static string Translate(string what)
|
|
{
|
|
var provider = Provider;
|
|
if(provider is null)
|
|
{
|
|
return $"NoProvider:{what}";
|
|
}
|
|
return provider.GetString(what);
|
|
}
|
|
}
|
|
|
|
internal class CachedCsfTranslationProvider : ITranslationProvider
|
|
{
|
|
private readonly ITranslationProvider _provider;
|
|
private readonly ConcurrentDictionary<string, string> _cache = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
public string Name => $"Cached{_provider.Name}";
|
|
|
|
public CachedCsfTranslationProvider(ITranslationProvider provider)
|
|
{
|
|
_provider = provider;
|
|
}
|
|
|
|
public string GetString(string str)
|
|
{
|
|
if(!_cache.TryGetValue(str, out var value))
|
|
{
|
|
value = _provider.GetString(str);
|
|
_cache.TryAdd(str, value);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public string GetPluralString(string str, string strPlural, long count)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|