From 6521382567e3cdac035ac956eea669a3ace5bdbf Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Wed, 23 Feb 2022 13:29:10 +0100 Subject: [PATCH] remove unnecessary resource localizer --- zero.Backoffice.UI/index.html | 2 +- zero.Core/Localization/ResourceLocalizer.cs | 116 -------------------- 2 files changed, 1 insertion(+), 117 deletions(-) delete mode 100644 zero.Core/Localization/ResourceLocalizer.cs diff --git a/zero.Backoffice.UI/index.html b/zero.Backoffice.UI/index.html index 8dbb3e01..044618c9 100644 --- a/zero.Backoffice.UI/index.html +++ b/zero.Backoffice.UI/index.html @@ -20,7 +20,7 @@ - + diff --git a/zero.Core/Localization/ResourceLocalizer.cs b/zero.Core/Localization/ResourceLocalizer.cs deleted file mode 100644 index 216c9818..00000000 --- a/zero.Core/Localization/ResourceLocalizer.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System.Collections.Concurrent; -using System.Reflection; - -namespace zero.Localization; - -public class ResourceLocalizer : IResourceLocalizer -{ - protected ConcurrentDictionary Cache { get; private set; } = new(); - - protected IZeroStore Store { get; private set; } - - public ResourceLocalizer(IZeroStore store) - { - Store = store; - } - - - /// - public string Text(string key) => Text(key, null); - - - /// - public string Text(string key, Dictionary tokens) - { - if (key.IsNullOrEmpty()) - { - return null; - } - - if (!Cache.TryGetValue(key, out string value)) - { - Translation translation = LoadTranslation(key); - - if (translation == null) - { - return null; - } - - value = translation.Value; - Cache.TryAdd(key, value); - } - - if (tokens != null) - { - value = TokenReplacement.Apply(value, tokens); - } - - return value; - } - - - /// - public string Text(T enumValue) where T : Enum => Text(enumValue, null); - - - /// - public string Text(T enumValue, Dictionary tokens) where T : Enum - { - Type type = enumValue.GetType(); - MemberInfo memInfo = type.GetMember(enumValue.ToString())[0]; - return Text(memInfo.GetCustomAttribute()?.Key, tokens); - } - - - /// - public string Maybe(string key) => Maybe(key, null); - - - /// - public string Maybe(string key, Dictionary tokens) - { - return key.IsNullOrEmpty() || !key.StartsWith("@") ? key : Text(key.Substring(1), tokens); - } - - - /// - /// Get translation from database or any other source - /// - protected virtual Translation LoadTranslation(string key) - { - return Store.Session().Synchronous.Query().FirstOrDefault(x => x.Key == key); - } -} - -public interface IResourceLocalizer -{ - /// - /// - /// - string Text(string key); - - /// - /// - /// - string Text(string key, Dictionary tokens); - - /// - /// Get a text string from a [Localize] attribute - /// - string Text(T enumValue) where T : Enum; - - /// - /// Get a text string from a [Localize] attribute - /// - string Text(T enumValue, Dictionary tokens) where T : Enum; - - /// - /// Only tries to resolve the key when it is prefixed with an @ - /// - string Maybe(string key); - - /// - /// Only tries to resolve the key when it is prefixed with an @ - /// - string Maybe(string key, Dictionary tokens); -}