protected virtual string GetTranslation(Query query) { var key = query.Key; var language = query.Language; var cacheKey = CacheKeyHelper.BuildKey(key); var localizationResource = ConfigurationContext.Current.CacheManager.Get(cacheKey) as LocalizationResource; if (localizationResource != null) { return(GetTranslationFromAvailableList(localizationResource.Translations, language, query.UseFallback)?.Value); } var resource = GetResourceFromDb(key); LocalizationResourceTranslation localization = null; // create empty null resource - to indicate non-existing one if (resource == null) { resource = LocalizationResource.CreateNonExisting(key); } else { localization = GetTranslationFromAvailableList(resource.Translations, language, query.UseFallback); } ConfigurationContext.Current.CacheManager.Insert(cacheKey, resource); return(localization?.Value); }
private string GetTranslation(string key, CultureInfo language) { var cacheKey = CacheKeyHelper.BuildKey(key); var localizationResource = ConfigurationContext.Current.CacheManager.Get(cacheKey) as LocalizationResource; if (localizationResource != null) { // if value for the cache key is null - this is non-existing resource (no hit) return(localizationResource.Translations?.FirstOrDefault(t => t.Language == language.Name)?.Value); } var resource = GetResourceFromDb(key); LocalizationResourceTranslation localization = null; if (resource == null) { // create empty null resource - to indicate non-existing one resource = LocalizationResource.CreateNonExisting(key); } else { localization = resource.Translations.FirstOrDefault(t => t.Language == language.Name); } ConfigurationContext.Current.CacheManager.Insert(cacheKey, resource); return(localization?.Value); }
public void Execute(Command command) { if (string.IsNullOrEmpty(command.Key)) { throw new ArgumentNullException(nameof(command.Key)); } using (var db = new LanguageEntities()) { var existingResource = db.LocalizationResources.FirstOrDefault(r => r.ResourceKey == command.Key); if (existingResource == null) { return; } if (existingResource.FromCode) { throw new InvalidOperationException($"Cannot delete resource `{command.Key}` that is synced with code"); } db.LocalizationResources.Remove(existingResource); db.SaveChanges(); } ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key)); }
public string Execute(GetTranslation.Query query) { if (!ConfigurationContext.Current.EnableLocalization()) { return(query.Key); } var key = query.Key; var language = query.Language; var cacheKey = CacheKeyHelper.BuildKey(key); var localizationResource = ConfigurationContext.Current.CacheManager.Get(cacheKey) as LocalizationResource; if (localizationResource != null) { return(GetTranslationFromAvailableList(localizationResource.Translations, language, query.UseFallback)?.Value); } var resource = GetResourceFromDb(key); LocalizationResourceTranslation localization = null; if (resource == null) { resource = LocalizationResource.CreateNonExisting(key); } else { localization = GetTranslationFromAvailableList(resource.Translations, language, query.UseFallback); } ConfigurationContext.Current.CacheManager.Insert(cacheKey, resource); return(localization?.Value); }
/// <summary> /// Handles the command. Actual instance of the command being executed is passed-in as argument /// </summary> /// <param name="command">Actual command instance being executed</param> /// <exception cref="InvalidOperationException">Cannot delete translation for not modified resource (key: `{command.Key}`</exception> public void Execute(RemoveTranslation.Command command) { var repository = new ResourceRepository(_configurationContext); var resource = repository.GetByKey(command.Key); if (resource == null) { return; } if (!resource.IsModified.HasValue || !resource.IsModified.Value) { throw new InvalidOperationException( $"Cannot delete translation for not modified resource (key: `{command.Key}`"); } var t = resource.Translations.FirstOrDefault(_ => _.Language == command.Language.Name); if (t != null) { repository.DeleteTranslation(resource, t); } _configurationContext.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key)); }
public void Execute(RemoveTranslation.Command command) { using (var db = new LanguageEntities()) { var resource = db.LocalizationResources.Include(r => r.Translations).FirstOrDefault(r => r.ResourceKey == command.Key); if (resource == null) { return; } if (!resource.IsModified.HasValue || !resource.IsModified.Value) { throw new InvalidOperationException($"Cannot delete translation for unmodified resource `{command.Key}`"); } var t = resource.Translations.FirstOrDefault(_ => _.Language == command.Language.Name); if (t != null) { db.LocalizationResourceTranslations.Remove(t); db.SaveChanges(); } } ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key)); }
public void Execute(ClearCache.Command command) { var manager = ConfigurationContext.Current.CacheManager; foreach (var key in ConfigurationContext.Current.BaseCacheManager.KnownResourceKeys.Keys) { var cachedKey = CacheKeyHelper.BuildKey(key); manager.Remove(cachedKey); } }
/// <summary> Populate cache. </summary> // ReSharper disable once MemberCanBeMadeStatic.Local private void PopulateCache() { new ClearCache.Command().Execute(); var allResources = new GetAllResources.Query().Execute(); foreach (var resource in allResources) { var key = CacheKeyHelper.BuildKey(resource.ResourceKey); ConfigurationContext.Current.CacheManager.Insert(key, resource); } }
public IEnumerable <CultureInfo> Execute(AvailableLanguages.Query query) { var cacheKey = CacheKeyHelper.BuildKey($"AvailableLanguages_{query.IncludeInvariant}"); if (HttpRuntime.Cache?.Get(cacheKey) is IEnumerable <CultureInfo> cachedLanguages) { return(cachedLanguages); } var languages = GetAvailableLanguages(query.IncludeInvariant); HttpRuntime.Cache?.Insert(cacheKey, languages); return(languages); }
/// <summary> /// Place where query handling happens /// </summary> /// <param name="query">This is the query instance</param> /// <returns> /// You have to return something from the query execution. Of course you can return <c>null</c> as well if you /// will. /// </returns> public IEnumerable <CultureInfo> Execute(AvailableLanguages.Query query) { var cacheKey = CacheKeyHelper.BuildKey($"AvailableLanguages_{query.IncludeInvariant}"); if (_configurationContext.CacheManager.Get(cacheKey) is IEnumerable <CultureInfo> cachedLanguages) { return(cachedLanguages); } var languages = GetAvailableLanguages(query.IncludeInvariant); _configurationContext.CacheManager.Insert(cacheKey, languages, false); return(languages); }
public IEnumerable <CultureInfo> Execute(Query query) { var cacheKey = CacheKeyHelper.BuildKey("AvailableLanguages"); var cachedLanguages = ConfigurationContext.Current.CacheManager.Get(cacheKey) as IEnumerable <CultureInfo>; if (cachedLanguages != null) { return(cachedLanguages); } var languages = GetAvailableLanguages(); ConfigurationContext.Current.CacheManager.Insert(cacheKey, languages); return(languages); }
public string Execute(GetTranslation.Query query) { if (!ConfigurationContext.Current.EnableLocalization()) { return(query.Key); } var key = query.Key; var language = query.Language; var cacheKey = CacheKeyHelper.BuildKey(key); var localizationResource = ConfigurationContext.Current.CacheManager.Get(cacheKey) as LocalizationResource; if (localizationResource != null) { return(GetTranslationFromAvailableList(localizationResource.Translations, language, query.UseFallback)?.Value); } LocalizationResourceTranslation localization = null; LocalizationResource resource; try { resource = new GetResource.Query(key).Execute(); } catch (KeyNotFoundException) { // this can be a case when Episerver initialization infrastructure calls localization provider way too early // and there is no registration for the GetResourceHandler - so we can fallback to default implementation // TODO: maybe we should just have default mapping (even if init has not been called) // (before any of the setup code in the provider is executed). this happens if you have DisplayChannels in codebase resource = new GetResourceHandler().Execute(new GetResource.Query(query.Key)); } if (resource == null) { resource = LocalizationResource.CreateNonExisting(key); } else { localization = GetTranslationFromAvailableList(resource.Translations, language, query.UseFallback); } ConfigurationContext.Current.CacheManager.Insert(cacheKey, resource); return(localization?.Value); }
private static void StoreKnownResourcesAndPopulateCache(IEnumerable <LocalizationResource> syncedResources) { if (ConfigurationContext.Current.PopulateCacheOnStartup) { new ClearCache.Command().Execute(); foreach (var resource in syncedResources) { var key = CacheKeyHelper.BuildKey(resource.ResourceKey); ConfigurationContext.Current.CacheManager.Insert(key, resource, true); } } else { // just store resource cache keys syncedResources.ForEach(r => ConfigurationContext.Current.BaseCacheManager.StoreKnownKey(r.ResourceKey)); } }
/// <summary> /// Place where query handling happens /// </summary> /// <param name="query">This is the query instance</param> /// <returns> /// You have to return something from the query execution. Of course you can return <c>null</c> as well if you /// will. /// </returns> public string Execute(GetTranslation.Query query) { var context = ConfigurationContext.Current; if (!context.EnableLocalization()) { return(query.Key); } var key = query.Key; // we can check whether we know this resource at all // if not - we can break circuit here if (!ConfigurationContext.Current.BaseCacheManager.KnownResourceKeys.ContainsKey(key)) { return(null); } var cacheKey = CacheKeyHelper.BuildKey(key); if (context.DiagnosticsEnabled) { context.Logger?.Debug($"Executing query for resource key `{query.Key}` (lang: `{query.Language.Name}).."); } var localizationResource = context.CacheManager.Get(cacheKey) as LocalizationResource; if (localizationResource == null) { if (context.DiagnosticsEnabled) { context.Logger?.Info( $"MISSING: Resource Key (culture: {query.Language.Name}): {query.Key}. Probably class is not decorated with either [LocalizedModel] or [LocalizedResource] attribute."); } // resource is not found in the cache, let's check database localizationResource = GetResourceFromDb(key) ?? LocalizationResource.CreateNonExisting(key); context.CacheManager.Insert(cacheKey, localizationResource, true); } return(localizationResource.Translations.GetValueWithFallback( query.Language, context.FallbackCultures)); }
private void StoreKnownResourcesAndPopulateCache() { var allResources = new GetAllResources.Query(true).Execute(); if (ConfigurationContext.Current.PopulateCacheOnStartup) { new ClearCache.Command().Execute(); foreach (var resource in allResources) { var key = CacheKeyHelper.BuildKey(resource.ResourceKey); ConfigurationContext.Current.CacheManager.Insert(key, resource); } } else { // just store known resource keys in cache allResources.ForEach(r => ConfigurationContext.Current.BaseCacheManager.StoreKnownKey(r.ResourceKey)); } }
public IEnumerable <LocalizationResource> Execute(GetAllResources.Query query) { if (query.ForceReadFromDb) { return(_inner.Execute(query)); } // get keys for known resources var keys = ConfigurationContext.Current.BaseCacheManager.KnownResourceKeys.Keys; // if keys = 0, execute inner query to actually get resources from the db // this is usually called during initialization when cache is not yet filled up if (keys.Count == 0) { return(_inner.Execute(query)); } var result = new List <LocalizationResource>(); foreach (var key in keys) { var cacheKey = CacheKeyHelper.BuildKey(key); if (ConfigurationContext.Current.CacheManager.Get(cacheKey) is LocalizationResource localizationResource) { result.Add(localizationResource); } else { // failed to get from cache, should call database var resourceFromDb = new GetResource.Query(key).Execute(); if (resourceFromDb != null) { ConfigurationContext.Current.CacheManager.Insert(cacheKey, resourceFromDb); result.Add(resourceFromDb); } } } return(result); }
private LocalizationResource GetCachedResourceOrReadFromStorage(Query query) { var key = query.Key; var cacheKey = CacheKeyHelper.BuildKey(key); if (_configurationContext.CacheManager.Get(cacheKey) is LocalizationResource localizationResource) { return(localizationResource); } if (_configurationContext.DiagnosticsEnabled) { _logger.Info( $"MISSING: Resource Key (culture: {query.Language.Name}): {key}. Probably class is not decorated with either [LocalizedModel] or [LocalizedResource] attribute."); } // resource is not found in the cache, let's check database localizationResource = GetResourceFromDb(key) ?? LocalizationResource.CreateNonExisting(key); _configurationContext.CacheManager.Insert(cacheKey, localizationResource, true); return(localizationResource); }
/// <summary> /// Handles the command. Actual instance of the command being executed is passed-in as argument /// </summary> /// <param name="command">Actual command instance being executed</param> public void Execute(CreateOrUpdateTranslation.Command command) { var repository = new ResourceRepository(_configurationContext); var resource = repository.GetByKey(command.Key); var now = DateTime.UtcNow; if (resource == null) { return; } var translation = resource.Translations.FindByLanguage(command.Language); if (translation == null) { var newTranslation = new LocalizationResourceTranslation { Value = command.Translation, Language = command.Language.Name, ResourceId = resource.Id, ModificationDate = now }; repository.AddTranslation(resource, newTranslation); } else { translation.Value = command.Translation; translation.ModificationDate = now; repository.UpdateTranslation(resource, translation); } resource.ModificationDate = now; resource.IsModified = true; repository.UpdateResource(resource); _configurationContext.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key)); }
public void Execute(Command command) { using (var db = new LanguageEntities()) { var resource = db.LocalizationResources.Include(r => r.Translations).FirstOrDefault(r => r.ResourceKey == command.Key); if (resource == null) { // TODO: return some status response obj return; } var translation = resource.Translations.FirstOrDefault(t => t.Language == command.Language.Name); if (translation != null) { // update existing translation translation.Value = command.Translation; } else { var newTranslation = new LocalizationResourceTranslation { Value = command.Translation, Language = command.Language.Name, ResourceId = resource.Id }; db.LocalizationResourceTranslations.Add(newTranslation); } resource.ModificationDate = DateTime.UtcNow; resource.IsModified = true; db.SaveChanges(); } ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key)); }
/// <summary> /// Handles the command. Actual instance of the command being executed is passed-in as argument /// </summary> /// <param name="command">Actual command instance being executed</param> /// <exception cref="ArgumentNullException">Key</exception> /// <exception cref="InvalidOperationException">Cannot delete resource `{command.Key}` that is synced with code</exception> public void Execute(DeleteResource.Command command) { if (string.IsNullOrEmpty(command.Key)) { throw new ArgumentNullException(nameof(command.Key)); } var repo = new ResourceRepository(); var resource = repo.GetByKey(command.Key); if (resource == null) { return; } if (resource.FromCode) { throw new InvalidOperationException($"Cannot delete resource `{command.Key}` that is synced with code"); } repo.DeleteResource(resource); ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key)); }