/// <summary>
 ///  Adds MVC localization to the application.
 /// </summary>
 /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
 /// <param name="format">The view format for localized views.</param>
 /// <returns>The <see cref="IMvcBuilder"/>.</returns>
 public static IMvcBuilder AddViewLocalization(
     [NotNull] this IMvcBuilder builder,
     LanguageViewLocationExpanderFormat format)
 {
     MvcLocalizationServices.AddLocalizationServices(builder.Services, format);
     return builder;
 }
        public void ExpandViewLocations_SpecificLocale(
            LanguageViewLocationExpanderFormat format,
            IEnumerable <string> viewLocations,
            IEnumerable <string> expectedViewLocations)
        {
            // Arrange
            var viewLocationExpanderContext = new ViewLocationExpanderContext(
                new ActionContext(),
                "testView",
                "test-controller",
                "",
                false);
            var languageViewLocationExpander = new LanguageViewLocationExpander(format);

            viewLocationExpanderContext.Values             = new Dictionary <string, string>();
            viewLocationExpanderContext.Values["language"] = "en-GB";

            // Act
            var expandedViewLocations = languageViewLocationExpander.ExpandViewLocations(
                viewLocationExpanderContext,
                viewLocations);

            // Assert
            Assert.Equal(expectedViewLocations, expandedViewLocations);
        }
Beispiel #3
0
 /// <summary>
 ///  Adds MVC localization to the application.
 /// </summary>
 /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
 /// <param name="format">The view format for localized views.</param>
 /// <returns>The <see cref="IMvcBuilder"/>.</returns>
 public static IMvcBuilder AddViewLocalization(
     [NotNull] this IMvcBuilder builder,
     LanguageViewLocationExpanderFormat format)
 {
     MvcLocalizationServices.AddLocalizationServices(builder.Services, format);
     return(builder);
 }
        public static void AddLocalizationServices(
            IServiceCollection services,
            LanguageViewLocationExpanderFormat format,
            Action <LocalizationOptions> setupAction)
        {
            AddMvcLocalizationServices(services, format, setupAction);

            services.AddLocalization(setupAction);
        }
        public static void AddLocalizationServices(
            IServiceCollection services,
            LanguageViewLocationExpanderFormat format,
            Action<LocalizationOptions> setupAction)
        {
            AddMvcLocalizationServices(services, format, setupAction);

            services.AddLocalization(setupAction);
        }
Beispiel #6
0
        /// <summary>
        ///  Adds MVC localization to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        /// <remarks>
        /// Adding localization also adds support for views via
        /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/> and the Razor view engine
        /// via <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
        /// </remarks>
        public static IMvcCoreBuilder AddViewLocalization(
            [NotNull] this IMvcCoreBuilder builder,
            LanguageViewLocationExpanderFormat format)
        {
            builder.AddViews();
            builder.AddRazorViewEngine();

            MvcLocalizationServices.AddLocalizationServices(builder.Services, format);
            return(builder);
        }
        /// <summary>
        ///  Adds MVC localization to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        /// <remarks>
        /// Adding localization also adds support for views via
        /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/> and the Razor view engine
        /// via <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
        /// </remarks>
        public static IMvcCoreBuilder AddViewLocalization(
            [NotNull] this IMvcCoreBuilder builder,
            LanguageViewLocationExpanderFormat format)
        {

            builder.AddViews();
            builder.AddRazorViewEngine();

            MvcLocalizationServices.AddLocalizationServices(builder.Services, format);
            return builder;
        }
        /// <summary>
        ///  Adds MVC view localization to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        public static IMvcBuilder AddViewLocalization(
            this IMvcBuilder builder,
            LanguageViewLocationExpanderFormat format)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            AddViewLocalization(builder, format, setupAction: null);
            return(builder);
        }
        /// <summary>
        ///  Adds MVC view localization to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        public static IMvcBuilder AddViewLocalization(
            this IMvcBuilder builder,
            LanguageViewLocationExpanderFormat format)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            AddViewLocalization(builder, format, setupAction: null);
            return builder;
        }
        /// <summary>
        ///  Adds MVC view localization to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <param name="setupAction">An action to configure the <see cref="LocalizationOptions"/>.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        public static IMvcBuilder AddViewLocalization(
            this IMvcBuilder builder,
            LanguageViewLocationExpanderFormat format,
            Action<LocalizationOptions> setupAction)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            MvcLocalizationServices.AddLocalizationServices(builder.Services, format, setupAction);
            return builder;
        }
        /// <summary>
        ///  Adds MVC view localization to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <param name="setupAction">An action to configure the <see cref="LocalizationOptions"/>.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        public static IMvcBuilder AddViewLocalization(
            this IMvcBuilder builder,
            LanguageViewLocationExpanderFormat format,
            Action <LocalizationOptions> setupAction)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            MvcLocalizationServices.AddLocalizationServices(builder.Services, format, setupAction);
            return(builder);
        }
    // To enable unit testing only 'MVC' specific services
    public static void AddMvcViewLocalizationServices(
        IServiceCollection services,
        LanguageViewLocationExpanderFormat format)
    {
        services.Configure <RazorViewEngineOptions>(
            options =>
        {
            options.ViewLocationExpanders.Add(new LanguageViewLocationExpander(format));
        });

        services.TryAdd(ServiceDescriptor.Singleton <IHtmlLocalizerFactory, HtmlLocalizerFactory>());
        services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer <>), typeof(HtmlLocalizer <>)));
        services.TryAdd(ServiceDescriptor.Transient <IViewLocalizer, ViewLocalizer>());
    }
Beispiel #13
0
        // To enable unit testing only 'MVC' specific services
        public static void AddMvcLocalizationServices(
            IServiceCollection services,
            LanguageViewLocationExpanderFormat format,
            Action<LocalizationOptions> setupAction)
        {
            services.Configure<RazorViewEngineOptions>(
                options =>
                {
                    options.ViewLocationExpanders.Add(new LanguageViewLocationExpander(format));
                });

            services.TryAdd(ServiceDescriptor.Singleton<IHtmlLocalizerFactory, HtmlLocalizerFactory>());
            services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer<>), typeof(HtmlLocalizer<>)));
            services.TryAdd(ServiceDescriptor.Transient<IViewLocalizer, ViewLocalizer>());
        }
        /// <summary>
        ///  Adds MVC localization to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        /// <remarks>
        /// Adding localization also adds support for views via
        /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/> and the Razor view engine
        /// via <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
        /// </remarks>
        public static IMvcCoreBuilder AddViewLocalization(
            this IMvcCoreBuilder builder,
            LanguageViewLocationExpanderFormat format)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddViews();
            builder.AddRazorViewEngine();

            MvcLocalizationServices.AddLocalizationServices(builder.Services, format, setupAction: null);
            return builder;
        }
Beispiel #15
0
    /// <summary>
    ///  Adds MVC view localization services to the application.
    /// </summary>
    /// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
    /// <param name="format">The view format for localized views.</param>
    /// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
    /// <remarks>
    /// Adding localization also adds support for views via
    /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/> and the Razor view engine
    /// via <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
    /// </remarks>
    public static IMvcCoreBuilder AddViewLocalization(
        this IMvcCoreBuilder builder,
        LanguageViewLocationExpanderFormat format)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        builder.AddViews();
        builder.AddRazorViewEngine();

        MvcLocalizationServices.AddLocalizationServices(builder.Services, format, setupAction: null);
        return(builder);
    }
        /// <summary>
        /// Adds MVC view and data annotations localization services to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="localizationOptionsSetupAction">An action to configure the <see cref="LocalizationOptions"/>.
        /// Can be <c>null</c>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <param name="dataAnnotationsLocalizationOptionsSetupAction">An action to configure
        /// the <see cref="MvcDataAnnotationsLocalizationOptions"/>. Can be <c>null</c>.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        /// <remarks>
        /// Adding localization also adds support for views via
        /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/> and the Razor view engine
        /// via <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
        /// </remarks>
        public static IMvcBuilder AddMvcLocalization(
            this IMvcBuilder builder,
            Action <LocalizationOptions> localizationOptionsSetupAction,
            LanguageViewLocationExpanderFormat format,
            Action <MvcDataAnnotationsLocalizationOptions> dataAnnotationsLocalizationOptionsSetupAction)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder
                   .AddViewLocalization(format, localizationOptionsSetupAction)
                   .AddDataAnnotationsLocalization(dataAnnotationsLocalizationOptionsSetupAction));
        }
        /// <summary>
        /// Adds MVC view and data annotations localization services to the application.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
        /// <param name="format">The view format for localized views.</param>
        /// <returns>The <see cref="IMvcBuilder"/>.</returns>
        /// <remarks>
        /// Adding localization also adds support for views via
        /// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/> and the Razor view engine
        /// via <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
        /// </remarks>
        public static IMvcBuilder AddMvcLocalization(
            this IMvcBuilder builder,
            LanguageViewLocationExpanderFormat format)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(AddMvcLocalization(
                       builder,
                       localizationOptionsSetupAction: null,
                       format: format,
                       dataAnnotationsLocalizationOptionsSetupAction: null));
        }
    public static void AddLocalizationServices(
        IServiceCollection services,
        LanguageViewLocationExpanderFormat format,
        Action <LocalizationOptions>?setupAction)
    {
        AddMvcViewLocalizationServices(services, format);

        if (setupAction == null)
        {
            services.AddLocalization();
        }
        else
        {
            services.AddLocalization(setupAction);
        }
    }
        /// <summary>
        ///  Adds Mvc localization to the application.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
        /// <param name="option">The view format for localized views.</param>
        /// <returns>The <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddMvcLocalization(
            [NotNull] this IServiceCollection services,
            LanguageViewLocationExpanderFormat format)
        {
            services.Configure<RazorViewEngineOptions>(
                options =>
                {
                    options.ViewLocationExpanders.Add(new LanguageViewLocationExpander(format));
                },
                DefaultOrder.DefaultFrameworkSortOrder);

            services.TryAdd(ServiceDescriptor.Singleton<IHtmlLocalizerFactory, HtmlLocalizerFactory>());
            services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer<>), typeof(HtmlLocalizer<>))); 
            services.TryAdd(ServiceDescriptor.Transient<IViewLocalizer, ViewLocalizer>());
            if (!services.Any(sd => sd.ServiceType == typeof(IHtmlEncoder)))
            {
                services.TryAdd(ServiceDescriptor.Instance<IHtmlEncoder>(HtmlEncoder.Default));
            }
            return services.AddLocalization();
        }
Beispiel #20
0
        /// <summary>
        ///  Adds Mvc localization to the application.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
        /// <param name="option">The view format for localized views.</param>
        /// <returns>The <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddMvcLocalization(
            [NotNull] this IServiceCollection services,
            LanguageViewLocationExpanderFormat format)
        {
            services.Configure <RazorViewEngineOptions>(
                options =>
            {
                options.ViewLocationExpanders.Add(new LanguageViewLocationExpander(format));
            },
                DefaultOrder.DefaultFrameworkSortOrder);

            services.TryAdd(ServiceDescriptor.Singleton <IHtmlLocalizerFactory, HtmlLocalizerFactory>());
            services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer <>), typeof(HtmlLocalizer <>)));
            services.TryAdd(ServiceDescriptor.Transient <IViewLocalizer, ViewLocalizer>());
            if (!services.Any(sd => sd.ServiceType == typeof(IHtmlEncoder)))
            {
                services.TryAdd(ServiceDescriptor.Instance <IHtmlEncoder>(HtmlEncoder.Default));
            }
            return(services.AddLocalization());
        }
Beispiel #21
0
        public static void AddLocalizationServices(
            IServiceCollection services,
            LanguageViewLocationExpanderFormat format,
            Action <LocalizationOptions> setupAction)
        {
            services.Configure <RazorViewEngineOptions>(
                options =>
            {
                options.ViewLocationExpanders.Add(new LanguageViewLocationExpander(format));
            });

            services.TryAdd(ServiceDescriptor.Singleton <IHtmlLocalizerFactory, HtmlLocalizerFactory>());
            services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer <>), typeof(HtmlLocalizer <>)));
            services.TryAdd(ServiceDescriptor.Transient <IViewLocalizer, ViewLocalizer>());
            if (!services.Any(sd => sd.ServiceType == typeof(IHtmlEncoder)))
            {
                services.TryAdd(ServiceDescriptor.Instance <IHtmlEncoder>(HtmlEncoder.Default));
            }

            services.AddLocalization(setupAction);
        }
        public static void AddLocalizationServices(
            IServiceCollection services,
            LanguageViewLocationExpanderFormat format,
            Action<LocalizationOptions> setupAction)
        {
            services.Configure<RazorViewEngineOptions>(
                options =>
                {
                    options.ViewLocationExpanders.Add(new LanguageViewLocationExpander(format));
                });

            services.TryAdd(ServiceDescriptor.Singleton<IHtmlLocalizerFactory, HtmlLocalizerFactory>());
            services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer<>), typeof(HtmlLocalizer<>)));
            services.TryAdd(ServiceDescriptor.Transient<IViewLocalizer, ViewLocalizer>());
            if (!services.Any(sd => sd.ServiceType == typeof(IHtmlEncoder)))
            {
                services.TryAdd(ServiceDescriptor.Instance<IHtmlEncoder>(HtmlEncoder.Default));
            }

            services.AddLocalization(setupAction);
        }
 /// <summary>
 /// Instantiates a new <see cref="Internal.DefaultTagHelperActivator"/> instance.
 /// </summary>
 /// <param name="format">The <see cref="LanguageViewLocationExpanderFormat"/>.</param>
 public LanguageViewLocationExpander(LanguageViewLocationExpanderFormat format)
 {
     _format = format;
 }
        public void ExpandViewLocations_SpecificLocale(
            LanguageViewLocationExpanderFormat format,
            IEnumerable<string> viewLocations,
            IEnumerable<string> expectedViewLocations)
        {
            // Arrange
            var viewLocationExpanderContext = new ViewLocationExpanderContext(new ActionContext(),"testView", false);
            var languageViewLocationExpander = new LanguageViewLocationExpander(format);
            viewLocationExpanderContext.Values = new Dictionary<string, string>();
            viewLocationExpanderContext.Values["language"] = "en-GB";

            // Act
            var expandedViewLocations = languageViewLocationExpander.ExpandViewLocations(
                viewLocationExpanderContext,
                viewLocations);

            // Assert
            Assert.Equal(expectedViewLocations, expandedViewLocations);
        }
 /// <summary>
 /// Instantiates a new <see cref="DefaultTagHelperActivator"/> instance.
 /// </summary>
 /// <param name="format">The <see cref="LanguageViewLocationExpanderFormat"/>.</param>
 public LanguageViewLocationExpander(LanguageViewLocationExpanderFormat format)
 {
     _format = format;
 }