private static void LocalizeCollections <T>(T item, string language, LocalizationDepth depth) where T : class, ILocalizable
        {
            if (depth == LocalizationDepth.Shallow)
            {
                return;
            }

            if (depth == LocalizationDepth.OneLevel)
            {
                depth = LocalizationDepth.Shallow;
            }

            var collections = item.GetType()
                              .GetProperties()
                              .Select(i => i.GetValue(item) as IEnumerable)
                              .Where(i => i != null);

            foreach (var collection in collections)
            {
                foreach (var element in collection)
                {
                    Localize(element as ILocalizable, language, depth);
                }
            }
        }
Example #2
0
 /// <summary>
 /// Localizes all properties on each <see cref="ILocalizable"/> item in a collection with current language and getOriginalValueIfNotFound=true
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entities"></param>
 /// <param name="depth">Depth of localization</param>
 /// <param name="cancellationToken">A System.Threading.CancellationToken to observe while waiting for the task to complete.</param>
 /// <returns>Return localized entitis</returns>
 public static async Task <IEnumerable <TEntity> > LocalizeAsync <TEntity>(this IEnumerable <TEntity> entities,
                                                                           LocalizationDepth depth,
                                                                           CancellationToken cancellationToken = default)
     where TEntity : class, ILocalizable
 {
     using (var serviceScope = ApplicationServiceProvider.CreateServiceScope())
     {
         return(await serviceScope.ServiceProvider.GetRequiredService <IEntityLocalizer>()
                .LocalizeAsync(entities, depth, cancellationToken));
     }
 }
Example #3
0
 /// <summary>
 /// Save localized entity
 /// </summary>
 /// <typeparam name="TEntity">Type</typeparam>
 /// <param name="entity">Entity</param>
 /// <param name="languageId">Language identifier</param>
 /// <param name="depth">Depth of localization</param>
 public static async Task SaveLocalizedAsync <TEntity>(this TEntity entity,
                                                       int languageId,
                                                       LocalizationDepth depth,
                                                       CancellationToken cancellationToken = default)
     where TEntity : class, ILocalizable
 {
     using (var serviceScope = ApplicationServiceProvider.CreateServiceScope())
     {
         await serviceScope.ServiceProvider.GetRequiredService <IEntityLocalizer>()
         .SaveLocalizedAsync(entity, languageId, depth, cancellationToken);
     }
 }
Example #4
0
 /// <summary>
 /// Localizes all properties on an <see cref="ILocalizable"/> item with current language
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entity"></param>
 /// <param name="getOriginalValueIfNotFound">A value indicating whether to return default value (if localized is not found)</param>
 /// <param name="depth">Depth of localization</param>
 /// <param name="cancellationToken">A System.Threading.CancellationToken to observe while waiting for the task to complete.</param>
 /// <returns>Return localized entity</returns>
 public static async Task <TEntity> LocalizeAsync <TEntity>(this TEntity entity,
                                                            bool getOriginalValueIfNotFound,
                                                            LocalizationDepth depth,
                                                            CancellationToken cancellationToken = default)
     where TEntity : class, ILocalizable
 {
     using (var serviceScope = ApplicationServiceProvider.CreateServiceScope())
     {
         return(await serviceScope.ServiceProvider.GetRequiredService <IEntityLocalizer>()
                .LocalizeAsync(entity, getOriginalValueIfNotFound, depth, cancellationToken));
     }
 }
 /// <summary>
 /// Localizes all properties on an <see cref="ILocalizedModel{TModel, TEntity}"/> item with current language
 /// and getOriginalValueIfNotFound=true
 /// </summary>
 /// <typeparam name="TModel"></typeparam>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="model"></param>
 /// <param name="depth">Depth of localization</param>
 /// <param name="cancellationToken">A System.Threading.CancellationToken to observe while waiting for the task to complete.</param>
 /// <returns>Return localized model</returns>
 public static async Task <TModel> LocalizeAsync <TModel, TEntity>(this ILocalizedModel <TModel, TEntity> model,
                                                                   LocalizationDepth depth,
                                                                   CancellationToken cancellationToken = default)
     where TModel : class, ILocalizedModel <TModel, TEntity>
     where TEntity : class, ILocalizable
 {
     using (var serviceScope = ApplicationServiceProvider.CreateServiceScope())
     {
         return(await serviceScope.ServiceProvider.GetRequiredService <IModelLocalizer>()
                .LocalizeAsync(model, depth, cancellationToken));
     }
 }
        /// <summary>
        /// Returns an item with one or more localized properties deserialized and set to specified language.
        /// </summary>
        /// <param name="item">The items to localize.</param>
        /// <param name="language">The language to use for localization.</param>
        /// <param name="depth">The depth to which child properties should be localized.</param>
        /// <remarks>If the specified language is not found the default language or first in list is used.</remarks>
        public static T Localize <T>(this T item, string language, LocalizationDepth depth = LocalizationDepth.Shallow) where T : class, ILocalizable
        {
            if (item == null)
            {
                return(null);
            }

            LocalizeProperties(item, language);
            LocalizeChildren(item, language, depth);
            LocalizeCollections(item, language, depth);

            return(item);
        }
 /// <summary>
 /// Save all localized properties of each LocalizedModel item in Locales collection property of model
 /// </summary>
 /// <typeparam name="TModel"></typeparam>
 /// <typeparam name="TEntity"></typeparam>
 /// <typeparam name="TLocalizedModel"></typeparam>
 /// <param name="entity"></param>
 /// <param name="model"></param>
 /// <param name="depth">Depth of localization</param>
 /// <param name="cancellationToken">A System.Threading.CancellationToken to observe while waiting for the task to complete.</param>
 /// <returns></returns>
 public static async Task SaveLocalesAsync <TModel, TEntity, TLocalizedModel>(this TEntity entity,
                                                                              ILocalizedModelForEdit <TModel, TEntity, TLocalizedModel> model,
                                                                              LocalizationDepth depth,
                                                                              CancellationToken cancellationToken = default)
     where TModel : class, ILocalizedModelForEdit <TModel, TEntity, TLocalizedModel>
     where TEntity : class, ILocalizable
     where TLocalizedModel : class, ILocalizedLocaleModel
 {
     using (var serviceScope = ApplicationServiceProvider.CreateServiceScope())
     {
         await serviceScope.ServiceProvider.GetRequiredService <IModelLocalizer>()
         .SaveLocalesAsync(entity, model, depth, cancellationToken);
     }
 }
        private static void LocalizeChildren <T>(T item, string language, LocalizationDepth depth) where T : class, ILocalizable
        {
            if (depth == LocalizationDepth.Shallow)
            {
                return;
            }

            if (depth == LocalizationDepth.OneLevel)
            {
                depth = LocalizationDepth.Shallow;
            }

            var children = item.GetType()
                           .GetProperties()
                           .Select(i => i.GetValue(item, null) as ILocalizable)
                           .Where(i => i != null);

            foreach (var child in children)
            {
                child.Localize(language, depth);
            }
        }
        /// <summary>
        /// Returns a collection with one or more localized properties deserialized and set to specified language.
        /// </summary>
        /// <param name="items">The collection of items to localize.</param>
        /// <param name="language">The language to use for localization.</param>
        /// <param name="depth">The depth to which child properties should be localized.</param>
        /// <remarks>If the specified language is not found the default language or first in list is used.</remarks>
        public static IEnumerable <T> Localize <T>(this IEnumerable <T> items, string language, LocalizationDepth depth = LocalizationDepth.Shallow) where T : class, ILocalizable
        {
            foreach (var item in items)
            {
                LocalizeProperties(item, language);
                LocalizeChildren(item, language, depth);
                LocalizeCollections(item, language, depth);

                yield return(item);
            }
        }
 /// <summary>
 /// Localizes all properties on each <see cref="ILocalizedModel{TModel, TEntity}"/> item in a collection with specified language id
 /// </summary>
 /// <typeparam name="TModel"></typeparam>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="models"></param>
 /// <param name="languageId">Language identifier</param>
 /// <param name="getOriginalValueIfNotFound">A value indicating whether to return default value (if localized is not found)</param>
 /// <param name="depth">Depth of localization</param>
 /// <param name="cancellationToken">A System.Threading.CancellationToken to observe while waiting for the task to complete.</param>
 /// <returns>Return localized models</returns>
 public static async Task <IEnumerable <TModel> > LocalizeAsync <TModel, TEntity>(this IEnumerable <ILocalizedModel <TModel, TEntity> > models,
                                                                                  int languageId,
                                                                                  bool getOriginalValueIfNotFound,
                                                                                  LocalizationDepth depth,
                                                                                  CancellationToken cancellationToken = default)
     where TModel : class, ILocalizedModel <TModel, TEntity>
     where TEntity : class, ILocalizable
 {
     using (var serviceScope = ApplicationServiceProvider.CreateServiceScope())
     {
         return(await serviceScope.ServiceProvider.GetRequiredService <IModelLocalizer>()
                .LocalizeAsync(models, languageId, getOriginalValueIfNotFound, depth, cancellationToken));
     }
 }
Example #11
0
 public static T Localize <T>(this T item, IObjectLocalizer localizer, LocalizationDepth depth = LocalizationDepth.Shallow) where T : class, ILocalizable
 {
     return(localizer.Localize(item, depth));
 }
Example #12
0
 public static IEnumerable <T> Localize <T>(this IEnumerable <T> items, string languageCode, LocalizationDepth depth = LocalizationDepth.Shallow) where T : class, ILocalizable
 {
     return(ObjectLocalizerConfig.Get().Localize(items, languageCode, depth));
 }
Example #13
0
 public static T Localize <T>(this T item, LocalizationDepth depth = LocalizationDepth.Shallow) where T : class, ILocalizable
 {
     return(ObjectLocalizerConfig.Get().Localize(item, depth));
 }
Example #14
0
 public static IEnumerable <T> Localize <T>(this IEnumerable <T> items, IObjectLocalizer localizer, LocalizationDepth depth = LocalizationDepth.Shallow) where T : class, ILocalizable
 {
     return(localizer.Localize(items, depth));
 }