Exemple #1
0
        /// <summary>
        /// Setup embedded Json support with the given setup action.
        /// </summary>
        /// <param name="builder">The builder to setup.</param>
        /// <param name="setup">The setup action.</param>
        /// <returns>The given builder updated with the Json embedded options.</returns>
        public static JsonLocalizationOptionsBuilder UseEmbeddedJson(
            this JsonLocalizationOptionsBuilder builder,
            Action <EmbeddedJsonLocalizationOptions>?setup)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var optExt = new EmbeddedJsonLocalizationOptions();

            setup?.Invoke(optExt);

            return(builder.AddExtensionOptions(optExt));
        }
Exemple #2
0
        /// <summary>
        /// Add Json localization services.
        /// </summary>
        /// <param name="services">The service collection to setup.</param>
        /// <param name="setupAction">The action delegate to fine tune the Json localizer behavior
        /// (Use embedded Json resource files if null).</param>
        /// <param name="serviceLifetime">Service Lifetime to use to register the IStringLocalizerFactory. (Default is Scoped)</param>
        /// <returns>The given service collection updated with the Json localization services.</returns>
        public static IServiceCollection AddJsonLocalization(this IServiceCollection services, Action <JsonLocalizationOptionsBuilder> setupAction, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            services
            .AddLocalization()
            .AddSingleton <ICultureInfoService, CultureInfoService>()
            .AddSingleton <ICacheService, CacheService>()
            .AddSingleton <
                IJsonLocalizationExtensionService <EmbeddedJsonLocalizationOptions>,
                EmbeddedJsonLocalizationExtensionService>();

            switch (serviceLifetime)
            {
            case ServiceLifetime.Singleton:
                services
                .AddSingleton <IStringLocalizerFactory, JsonStringLocalizerFactory>()
                .AddSingleton <IExtensionResolverService, ExtensionResolverService>();
                break;

            case ServiceLifetime.Scoped:
                services
                .AddScoped <IStringLocalizerFactory, JsonStringLocalizerFactory>()
                .AddScoped <IExtensionResolverService, ExtensionResolverService>();
                break;

            case ServiceLifetime.Transient:
            default:
                services
                .AddTransient <IStringLocalizerFactory, JsonStringLocalizerFactory>()
                .AddTransient <IExtensionResolverService, ExtensionResolverService>();
                break;
            }

            var builder = new JsonLocalizationOptionsBuilder();

            if (setupAction != null)
            {
                setupAction.Invoke(builder);
            }
            else
            {
                builder.UseEmbeddedJson();
            }

            services.Configure <JsonLocalizationOptions>(builder.Build);

            return(services);
        }
Exemple #3
0
 /// <summary>
 /// Setup default embedded Json support.
 /// </summary>
 /// <param name="builder">The builder to setup.</param>
 /// <returns>The given builder updated with the Json embedded options.</returns>
 public static JsonLocalizationOptionsBuilder UseEmbeddedJson(
     this JsonLocalizationOptionsBuilder builder)
 => builder.UseEmbeddedJson(null);