Beispiel #1
0
        private void RegisterIfNotExist(LanguageEntities db, string resourceKey, string resourceValue, string defaultCulture, string author = "type-scanner")
        {
            var existingResource = db.LocalizationResources.Include(r => r.Translations).FirstOrDefault(r => r.ResourceKey == resourceKey);

            if (existingResource != null)
            {
                existingResource.FromCode = true;

                // if resource is not modified - we can sync default value from code
                if (existingResource.IsModified.HasValue && !existingResource.IsModified.Value)
                {
                    existingResource.ModificationDate = DateTime.UtcNow;
                    var defaultTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == defaultCulture);
                    if (defaultTranslation != null)
                    {
                        defaultTranslation.Value = resourceValue;
                    }
                }

                var fromCodeTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == ConfigurationContext.CultureForTranslationsFromCode);
                if (fromCodeTranslation != null)
                {
                    fromCodeTranslation.Value = resourceValue;
                }
                else
                {
                    fromCodeTranslation = new LocalizationResourceTranslation
                    {
                        Language = ConfigurationContext.CultureForTranslationsFromCode,
                        Value    = resourceValue
                    };

                    existingResource.Translations.Add(fromCodeTranslation);
                }
            }
            else
            {
                // create new resource
                var resource = new LocalizationResource(resourceKey)
                {
                    ModificationDate = DateTime.UtcNow,
                    Author           = author,
                    FromCode         = true,
                    IsModified       = false
                };

                resource.Translations.Add(new LocalizationResourceTranslation
                {
                    Language = defaultCulture,
                    Value    = resourceValue
                });

                resource.Translations.Add(new LocalizationResourceTranslation
                {
                    Language = ConfigurationContext.CultureForTranslationsFromCode,
                    Value    = resourceValue
                });
                db.LocalizationResources.Add(resource);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Adds the translation for the resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="translation">The translation.</param>
        /// <exception cref="ArgumentNullException">
        /// resource
        /// or
        /// translation
        /// </exception>
        public void AddTranslation(LocalizationResource resource, LocalizationResourceTranslation translation)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (translation == null)
            {
                throw new ArgumentNullException(nameof(translation));
            }

            using (var conn = new SqlConnection(Settings.DbContextConnectionString))
            {
                conn.Open();

                var cmd = new SqlCommand(
                    "INSERT INTO [dbo].[LocalizationResourceTranslations] ([Language], [ResourceId], [Value], [ModificationDate]) VALUES (@language, @resourceId, @translation, @modificationDate)",
                    conn);
                cmd.Parameters.AddWithValue("language", translation.Language);
                cmd.Parameters.AddWithValue("resourceId", translation.ResourceId);
                cmd.Parameters.AddWithValue("translation", translation.Value);
                cmd.Parameters.AddWithValue("modificationDate", translation.ModificationDate);

                cmd.ExecuteNonQuery();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Updates the translation for the resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="translation">The translation.</param>
        /// <exception cref="ArgumentNullException">
        /// resource
        /// or
        /// translation
        /// </exception>
        public void UpdateTranslation(LocalizationResource resource, LocalizationResourceTranslation translation)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (translation == null)
            {
                throw new ArgumentNullException(nameof(translation));
            }

            using (var conn = new SqlConnection(Settings.DbContextConnectionString))
            {
                conn.Open();

                var cmd = new SqlCommand(
                    "UPDATE [dbo].[LocalizationResourceTranslations] SET [Value] = @translation, [ModificationDate] = @modificationDate WHERE [Id] = @id",
                    conn);
                cmd.Parameters.AddWithValue("translation", translation.Value);
                cmd.Parameters.AddWithValue("id", translation.Id);
                cmd.Parameters.AddWithValue("modificationDate", DateTime.UtcNow);

                cmd.ExecuteNonQuery();
            }
        }
Beispiel #4
0
            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);
            }
Beispiel #5
0
            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);
            }
Beispiel #6
0
        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);
        }
Beispiel #7
0
 private LocalizationResourceTranslationEntity ToTranslationEntity(LocalizationResourceTranslation translation)
 {
     return(new LocalizationResourceTranslationEntity
     {
         Language = translation.Language,
         Translation = translation.Value,
         ModificationDate = DateTime.UtcNow
     });
 }
Beispiel #8
0
        /// <summary>
        /// Updates the translation for the resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="translation">The translation.</param>
        /// <exception cref="ArgumentNullException">
        /// resource
        /// or
        /// translation
        /// </exception>
        public void UpdateTranslation(LocalizationResource resource, LocalizationResourceTranslation translation)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (translation == null)
            {
                throw new ArgumentNullException(nameof(translation));
            }

            var table = GetTable();

            table.Execute(TableOperation.Replace(ToEntity(resource)));
        }
        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);
        }
Beispiel #10
0
        /// <summary>
        /// Adds the translation for the resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="translation">The translation.</param>
        /// <exception cref="ArgumentNullException">
        /// resource
        /// or
        /// translation
        /// </exception>
        public void AddTranslation(LocalizationResource resource, LocalizationResourceTranslation translation)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (translation == null)
            {
                throw new ArgumentNullException(nameof(translation));
            }

            resource.Translations.Add(translation);

            var table = GetTable();

            table.Execute(TableOperation.InsertOrReplace(ToEntity(resource)));
        }
Beispiel #11
0
        /// <summary>
        /// Deletes the translation.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="translation">The translation.</param>
        /// <exception cref="ArgumentNullException">
        /// resource
        /// or
        /// translation
        /// </exception>
        public void DeleteTranslation(LocalizationResource resource, LocalizationResourceTranslation translation)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (translation == null)
            {
                throw new ArgumentNullException(nameof(translation));
            }

            resource.Translations.Remove(resource.Translations.FindByLanguage(translation.Language));

            var table = GetTable();

            table.Execute(TableOperation.Replace(ToEntity(resource)));
        }
Beispiel #12
0
        /// <summary>
        /// Deletes the translation.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="translation">The translation.</param>
        /// <exception cref="ArgumentNullException">
        /// resource
        /// or
        /// translation
        /// </exception>
        public void DeleteTranslation(LocalizationResource resource, LocalizationResourceTranslation translation)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }
            if (translation == null)
            {
                throw new ArgumentNullException(nameof(translation));
            }

            using (var conn = new NpgsqlConnection(Settings.DbContextConnectionString))
            {
                conn.Open();

                var cmd = new NpgsqlCommand(@"DELETE FROM public.""LocalizationResourceTranslations"" WHERE ""Id"" = @id", conn);
                cmd.Parameters.AddWithValue("id", translation.Id);

                cmd.ExecuteNonQuery();
            }
        }
        /// <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));
            }
Beispiel #15
0
        /// <summary>
        /// Updates the translation for the resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="translation">The translation.</param>
        /// <exception cref="ArgumentNullException">
        /// resource
        /// or
        /// translation
        /// </exception>
        public void UpdateTranslation(LocalizationResource resource, LocalizationResourceTranslation translation)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }
            if (translation == null)
            {
                throw new ArgumentNullException(nameof(translation));
            }

            using (var conn = new NpgsqlConnection(Settings.DbContextConnectionString))
            {
                conn.Open();

                var cmd = new NpgsqlCommand(@"UPDATE public.""LocalizationResourceTranslations"" SET ""Value"" = @translation, ""ModificationDate"" = @modificationDate WHERE ""Id"" = @id", conn);
                cmd.Parameters.AddWithValue("translation", translation.Value);
                cmd.Parameters.AddWithValue("id", translation.Id);
                cmd.Parameters.AddWithValue("modificationDate", translation.ModificationDate);

                cmd.ExecuteNonQuery();
            }
        }
        private void RegisterIfNotExist(LanguageEntities db, string resourceKey, string resourceValue, string author = "type-scanner")
        {
            var existingResource = db.LocalizationResources.Include(r => r.Translations).FirstOrDefault(r => r.ResourceKey == resourceKey);
            var defaultTranslationCulture = DetermineDefaultCulture();

            if(existingResource != null)
            {
                existingResource.FromCode = true;

                // if resource is not modified - we can sync default value from code
                if(existingResource.IsModified.HasValue && !existingResource.IsModified.Value)
                {
                    var defaultTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == defaultTranslationCulture);
                    if(defaultTranslation != null)
                    {
                        defaultTranslation.Value = resourceValue;
                    }
                }

                var fromCodeTranslation = existingResource.Translations.FirstOrDefault(t => t.Language == ConfigurationContext.CultureForTranslationsFromCode);
                if(fromCodeTranslation != null)
                {
                    fromCodeTranslation.Value = resourceValue;
                }
                else
                {
                    fromCodeTranslation = new LocalizationResourceTranslation
                    {
                        Language = ConfigurationContext.CultureForTranslationsFromCode,
                        Value = resourceValue
                    };

                    existingResource.Translations.Add(fromCodeTranslation);
                }

                existingResource.ModificationDate = DateTime.UtcNow;
            }
            else
            {
                // create new resource
                var resource = new LocalizationResource(resourceKey)
                {
                    ModificationDate = DateTime.UtcNow,
                    Author = author,
                    FromCode = true,
                    IsModified = false
                };

                resource.Translations.Add(new LocalizationResourceTranslation
                                          {
                                              Language = defaultTranslationCulture,
                                              Value = resourceValue
                                          });

                resource.Translations.Add(new LocalizationResourceTranslation
                                          {
                                              Language = ConfigurationContext.CultureForTranslationsFromCode,
                                              Value = resourceValue
                                          });
                db.LocalizationResources.Add(resource);
            }
        }