using Raven.Client.Documents.Session; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using zero.Core.Attributes; using zero.Core.Database; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Renderer; namespace zero.Core.Services { public class Localizer : ILocalizer, IDisposable { protected Dictionary Cache { get; private set; } = new(); protected IZeroStore Store { get; private set; } IDocumentSession _session = null; protected IDocumentSession Session { get { return _session ?? (_session = Store.OpenSession()); } } public Localizer(IZeroStore store) { Store = store; } /// public string Text(string key) => Text(key, null); /// public string Text(string key, IDictionary tokens) { if (key.IsNullOrEmpty()) { return null; } if (!Cache.TryGetValue(key, out string value)) { ITranslation translation = LoadTranslation(key); if (translation == null) { return null; } value = translation.Value; lock (Cache) { Cache[key] = value; } } if (tokens != null) { value = TokenReplacement.Apply(value, tokens); } return value; } /// /// Get a text string from a Text attribute on a specific enum value /// public string Text(T enumValue) where T : Enum { Type type = enumValue.GetType(); MemberInfo memInfo = type.GetMember(enumValue.ToString())[0]; return Text(memInfo.GetCustomAttribute()?.Key); } /// public void Dispose() { Session?.Dispose(); } /// /// Get translation from database or any other source /// protected virtual ITranslation LoadTranslation(string key) { return Session.Query().FirstOrDefault(x => x.Key == key); } } public interface ILocalizer : IDisposable { /// /// /// string Text(string key); /// /// /// string Text(string key, IDictionary tokens); /// /// Get a text string from a [Localize] attribute /// public string Text(T enumValue) where T : Enum; } }