示例#1
0
        /// <summary>
        /// Adds Xaki services to the specified <see cref="IMvcBuilder"/>.
        /// </summary>
        public static IMvcBuilder AddXaki(this IMvcBuilder mvc, XakiOptions xakiOptions)
        {
            mvc.Services.AddXaki(xakiOptions);

            return(mvc.AddMvcOptions(options =>
            {
                options.ModelBinderProviders.Insert(0, new LocalizableModelBinderProvider());
            }));
        }
示例#2
0
        private static IServiceCollection AddXaki(this IServiceCollection services, XakiOptions xakiOptions)
        {
            services.AddHttpContextAccessor();

            services.Configure <RequestLocalizationOptions>(options =>
            {
                var supportedCultures = xakiOptions
                                        .SupportedLanguages
                                        .Select(i => new CultureInfo(i))
                                        .ToList();

                options.DefaultRequestCulture = new RequestCulture("en", "en");
                options.SupportedCultures     = supportedCultures;
                options.SupportedUICultures   = supportedCultures;

                // TODO support RouteDataRequestCultureProvider (https://joonasw.net/view/aspnet-core-localization-deep-dive)
            });

            services.AddScoped <IObjectLocalizer>(provider => new ObjectLocalizer
            {
                RequiredLanguages = new HashSet <string>(xakiOptions.RequiredLanguages),
                OptionalLanguages = new HashSet <string>(xakiOptions.OptionalLanguages),
                LanguageResolvers = new List <ILanguageResolver>
                {
                    new RequestLanguageResolver(provider.GetService <IHttpContextAccessor>()),
                    new CultureInfoLanguageResolver(),
                    new DefaultLanguageResolver("en")
                }
            });

            if (xakiOptions.EnablePerCallLocalizeExtensions)
            {
                var provider = services.BuildServiceProvider();
                ObjectLocalizerConfig.Set(() => provider.GetService <IObjectLocalizer>());
            }

            return(services);
        }