/// <summary>
        /// Invokes the logic of the middleware.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/>.</param>
        /// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
        public async Task Invoke([NotNull] HttpContext context)
        {
            var requestCulture = _options.DefaultRequestCulture ??
                                 new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

            IRequestCultureProvider winningProvider = null;

            if (_options.RequestCultureProviders != null)
            {
                foreach (var provider in _options.RequestCultureProviders)
                {
                    var result = await provider.DetermineRequestCulture(context);

                    if (result != null)
                    {
                        requestCulture  = result;
                        winningProvider = provider;
                        break;
                    }
                }
            }

            context.Features.Set <IRequestCultureFeature>(new RequestCultureFeature(requestCulture, winningProvider));

            SetCurrentThreadCulture(requestCulture);

            await _next(context);
        }
Beispiel #2
0
        public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var localizationService = app.ApplicationServices.GetRequiredService <ILeaveManagementCustomLocalizerFactory>();
            var options1            = app.ApplicationServices.GetService <IOptions <MvcDataAnnotationsLocalizationOptions> >();

            HtmlHelpersExtensions.RegisterLocalizer(localizationService);

            options1.Value.DataAnnotationLocalizerProvider = (type, function) => {
                return(localizationService.Create(type));
            };


            IRequestCultureProvider[] cultureProviders = new IRequestCultureProvider[] {
                new RouteDataRequestCultureProvider(),
                new QueryStringRequestCultureProvider(),
                new CookieRequestCultureProvider(),
                new AcceptLanguageHeaderRequestCultureProvider()
            };

            app.UseRequestLocalization(new RequestLocalizationOptions {
                DefaultRequestCulture   = new RequestCulture(DefaultCulture),
                RequestCultureProviders = cultureProviders.ToList(),
                // Formatting numbers, dates, etc.
                SupportedCultures = SupportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = SupportedCultures
            });
        }
        /// <summary>
        /// Creates a new <see cref="RequestCultureFeature"/> with the specified <see cref="Localization.RequestCulture"/>.
        /// </summary>
        /// <param name="requestCulture">The <see cref="Localization.RequestCulture"/>.</param>
        /// <param name="provider">The <see cref="IRequestCultureProvider"/>.</param>
        public RequestCultureFeature(RequestCulture requestCulture, IRequestCultureProvider provider)
        {
            if (requestCulture == null)
            {
                throw new ArgumentNullException(nameof(requestCulture));
            }

            RequestCulture = requestCulture;
            Provider       = provider;
        }
        /// <summary>
        /// Creates a new <see cref="RequestCultureFeature"/> with the specified <see cref="Localization.RequestCulture"/>.
        /// </summary>
        /// <param name="requestCulture">The <see cref="Localization.RequestCulture"/>.</param>
        /// <param name="provider">The <see cref="IRequestCultureProvider"/>.</param>
        public RequestCultureFeature(RequestCulture requestCulture, IRequestCultureProvider provider)
        {
            if (requestCulture == null)
            {
                throw new ArgumentNullException(nameof(requestCulture));
            }

            RequestCulture = requestCulture;
            Provider = provider;
        }
Beispiel #5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddControllers();
            services.AddSingleton <WeatherForecastService>();

            Language[] languages =
            {
                new Language
                {
                    LangId = "en-US",
                    Title  = "English"
                },
                new Language
                {
                    LangId = "it-IT",
                    Title  = "Italiano"
                }
            };

            CultureInfo[] supportedCultures = new CultureInfo[languages.Length];

            for (int i = 0; i < languages.Length; i++)
            {
                supportedCultures[i] = new CultureInfo(languages[i].LangId);
            }

            RequestCulture requestCulture = new RequestCulture("en-US");

            IRequestCultureProvider[] requestCultureProvider = new IRequestCultureProvider[]
            {
                new CookieRequestCultureProvider()
            };

            services.Configure <RequestLocalizationOptions>(options =>
            {
                options.RequestCultureProviders = requestCultureProvider;
                options.DefaultRequestCulture   = requestCulture;
                options.SupportedCultures       = supportedCultures;
                options.SupportedUICultures     = supportedCultures;
            });
        }
 public LocalizationStringCollection(IRequestCultureProvider cultureProvider)
 {
     CultureProvider = cultureProvider;
 }
Beispiel #7
0
        /// <summary>
        /// Invokes the logic of the middleware.
        /// </summary>
        /// <param name="context">The <see cref="IOwinContext"/>.</param>
        /// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
        public override async Task Invoke(IOwinContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var requestCulture = _options.DefaultRequestCulture;

            IRequestCultureProvider winningProvider = null;

            if (_options.RequestCultureProviders != null)
            {
                foreach (var provider in _options.RequestCultureProviders)
                {
                    var providerResultCulture = await provider.DetermineProviderCultureResult(context);

                    if (providerResultCulture == null)
                    {
                        continue;
                    }
                    var cultures   = providerResultCulture.Cultures;
                    var uiCultures = providerResultCulture.UICultures;

                    CultureInfo cultureInfo   = null;
                    CultureInfo uiCultureInfo = null;
                    if (_options.SupportedCultures != null)
                    {
                        cultureInfo = GetCultureInfo(
                            cultures,
                            _options.SupportedCultures,
                            _options.FallBackToParentCultures);

                        if (cultureInfo == null)
                        {
                            System.Diagnostics.Trace.WriteLine($"{provider.GetType().Name} returned the following unsupported cultures '{string.Join(", ", cultures)}'");
                        }
                    }

                    if (_options.SupportedUICultures != null)
                    {
                        uiCultureInfo = GetCultureInfo(
                            uiCultures,
                            _options.SupportedUICultures,
                            _options.FallBackToParentUICultures);

                        if (uiCultureInfo == null)
                        {
                            System.Diagnostics.Trace.WriteLine($"{provider.GetType().Name} returned the following unsupported UI cultures '{string.Join(", ", uiCultures)}'");
                        }
                    }

                    if (cultureInfo == null && uiCultureInfo == null)
                    {
                        continue;
                    }

                    if (cultureInfo == null && uiCultureInfo != null)
                    {
                        cultureInfo = _options.DefaultRequestCulture.Culture;
                    }
                    else if (cultureInfo != null && uiCultureInfo == null)
                    {
                        uiCultureInfo = _options.DefaultRequestCulture.UICulture;
                    }

                    var result = new RequestCulture(cultureInfo, uiCultureInfo);

                    if (result != null)
                    {
                        requestCulture  = result;
                        winningProvider = provider;
                        break;
                    }
                }
            }

            // set the request culture feature on the owin context
            // this can be used to restore the culture if the synchronization context changes such as an AspNet event (ie MVC pipeline)
            context.Set <IRequestCultureFeature>(RequestCultureFeature.RequestCultureFeatureKey, new RequestCultureFeature(requestCulture, winningProvider));

            // set the current thread culture
            SetCurrentThreadCulture(requestCulture);

            if (_options.ApplyCurrentCultureToResponseHeaders)
            {
                context.Response.Headers.Add("Content-Language", new[] { requestCulture.UICulture.Name });
            }

            await this.Next.Invoke(context);
        }
        protected internal virtual string GetRequestCultureProviderHint(string label, IRequestCultureProvider requestCultureProvider)
        {
            switch (requestCultureProvider)
            {
            case null:
                return("default-settings");

            case AcceptLanguageHeaderRequestCultureProvider _:
                return("header");

            case CookieRequestCultureProvider _:
                return("cookie");

            case QueryStringRequestCultureProvider _:
                return(this.HttpContext.Request.Query.Keys.Contains(label, StringComparer.OrdinalIgnoreCase) ? "query-string" : "query-string/default-settings");

            case RouteDataRequestCultureProvider _:
                return("url");

            default:
                return(null);
            }
        }
        protected virtual async Task <ProviderCultureResult> GetResultOrNull([NotNull] HttpContext httpContext, [CanBeNull] IRequestCultureProvider provider)
        {
            if (provider == null)
            {
                return(null);
            }

            return(await provider.DetermineProviderCultureResult(httpContext));
        }
Beispiel #10
0
 public EFCollection(ILocalizationDbContext <TKey> DbContext, IRequestCultureProvider cultureProvider) : base(cultureProvider)
 {
     _DbContext = DbContext;
     Refresh();
 }
Beispiel #11
0
 public EFLocalizationManager(ILocalizationStringCollection collection, IRequestCultureProvider provider)
 {
     _localizationStringCollection = collection as EFCollection <TKey>;
     _cultureProvider = provider;
 }
Beispiel #12
0
    /// <summary>
    /// Invokes the logic of the middleware.
    /// </summary>
    /// <param name="context">The <see cref="HttpContext"/>.</param>
    /// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
    public async Task InvokeAsync(HttpContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var requestCulture = _options.DefaultRequestCulture;

        IRequestCultureProvider winningProvider = null;

        var supportedCultures = await context.RequestServices.GetService <ISupportedCulturesResolver>().GetSupportedCulturesAsync(context);

        if (_options.RequestCultureProviders != null)
        {
            foreach (var provider in _options.RequestCultureProviders)
            {
                var providerResultCulture = await provider.DetermineProviderCultureResult(context);

                if (providerResultCulture == null)
                {
                    continue;
                }
                var cultures   = providerResultCulture.Cultures;
                var uiCultures = providerResultCulture.UICultures;

                CultureInfo cultureInfo   = null;
                CultureInfo uiCultureInfo = null;

                if (supportedCultures != null)
                {
                    cultureInfo = GetCultureInfo(
                        cultures,
                        supportedCultures,
                        _options.FallBackToParentCultures);

                    if (cultureInfo == null)
                    {
                        EnsureLogger(context);
                        _logger?.LogDebug("Unsupported Cultures: {Provider} => {Cultures}", provider.GetType().Name, cultures);
                    }
                }

                if (supportedCultures != null)
                {
                    uiCultureInfo = GetCultureInfo(
                        uiCultures,
                        supportedCultures,
                        _options.FallBackToParentUICultures);

                    if (uiCultureInfo == null)
                    {
                        EnsureLogger(context);
                        _logger?.LogDebug("Unsupported UI Cultures: {Provider} => {Cultures}", provider.GetType().Name, uiCultures);
                    }
                }

                if (cultureInfo == null && uiCultureInfo == null)
                {
                    continue;
                }

                if (cultureInfo == null)
                {
                    cultureInfo = _options.DefaultRequestCulture.Culture;
                }

                if (cultureInfo != null && uiCultureInfo == null)
                {
                    uiCultureInfo = _options.DefaultRequestCulture.UICulture;
                }

                var result = new RequestCulture(cultureInfo, uiCultureInfo);

                requestCulture  = result;
                winningProvider = provider;
                break;
            }
        }

        context.Features.Set <IRequestCultureFeature>(new RequestCultureFeature(requestCulture, winningProvider));

        SetCurrentThreadCulture(requestCulture);

        await _next(context);
    }
Beispiel #13
0
 /// <summary>
 /// Creates a new <see cref="RequestCultureFeature"/> with the specified <see cref="Localization.RequestCulture"/>.
 /// </summary>
 /// <param name="requestCulture">The <see cref="Localization.RequestCulture"/>.</param>
 /// <param name="provider">The <see cref="IRequestCultureProvider"/>.</param>
 public RequestCultureFeature([NotNull] RequestCulture requestCulture, IRequestCultureProvider provider)
 {
     RequestCulture = requestCulture;
     Provider       = provider;
 }
 public DefaultLocalizationStringCollection(IList<CultureInfo> collection, IRequestCultureProvider cultureProvider) : base(cultureProvider)
 {
     _collection = collection;
 }
 public LocalizationStringCollection(IRequestCultureProvider cultureProvider)
 {
     CultureProvider = cultureProvider;
 }
        /// <summary>
        /// Invokes the logic of the middleware.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/>.</param>
        /// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
        public async Task Invoke(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var requestCulture = _options.DefaultRequestCulture;

            IRequestCultureProvider winningProvider = null;

            if (_options.RequestCultureProviders != null)
            {
                foreach (var provider in _options.RequestCultureProviders)
                {
                    var providerResultCulture = await provider.DetermineProviderCultureResult(context);

                    if (providerResultCulture == null)
                    {
                        continue;
                    }
                    var cultures   = providerResultCulture.Cultures;
                    var uiCultures = providerResultCulture.UICultures;

                    CultureInfo cultureInfo   = null;
                    CultureInfo uiCultureInfo = null;
                    if (_options.SupportedCultures != null)
                    {
                        cultureInfo = GetCultureInfo(
                            cultures,
                            _options.SupportedCultures,
                            _options.FallBackToParentCultures);

                        if (cultureInfo == null)
                        {
                            _logger.UnsupportedCultures(provider.GetType().Name, cultures);
                        }
                    }

                    if (_options.SupportedUICultures != null)
                    {
                        uiCultureInfo = GetCultureInfo(
                            uiCultures,
                            _options.SupportedUICultures,
                            _options.FallBackToParentUICultures);

                        if (uiCultureInfo == null)
                        {
                            _logger.UnsupportedUICultures(provider.GetType().Name, uiCultures);
                        }
                    }

                    if (cultureInfo == null && uiCultureInfo == null)
                    {
                        continue;
                    }

                    if (cultureInfo == null && uiCultureInfo != null)
                    {
                        cultureInfo = _options.DefaultRequestCulture.Culture;
                    }

                    if (cultureInfo != null && uiCultureInfo == null)
                    {
                        uiCultureInfo = _options.DefaultRequestCulture.UICulture;
                    }

                    var result = new RequestCulture(cultureInfo, uiCultureInfo);

                    if (result != null)
                    {
                        requestCulture  = result;
                        winningProvider = provider;
                        break;
                    }
                }
            }

            context.Features.Set <IRequestCultureFeature>(new RequestCultureFeature(requestCulture, winningProvider));

            SetCurrentThreadCulture(requestCulture);

            if (_options.ApplyCurrentCultureToResponseHeaders)
            {
                context.Response.Headers.Add(HeaderNames.ContentLanguage, requestCulture.UICulture.Name);
            }

            await _next(context);
        }
Beispiel #17
0
 public JsonCollection(string resourcesPath, IRequestCultureProvider cultureProvider, IApplicationEnvironment env) : base(cultureProvider)
 {
     _Collection    = new List <CultureInfo>();
     _ResourcesPath = env.ApplicationBasePath + resourcesPath;
     Refresh();
 }
Beispiel #18
0
 protected internal virtual string GetRequestCultureProviderHint(string label, IRequestCultureProvider requestCultureProvider)
 {
     return(requestCultureProvider switch
     {
         null => this.Localizer.GetString("default-settings"),
         AcceptLanguageHeaderRequestCultureProvider => this.Localizer.GetString("header"),
         CookieRequestCultureProvider => this.Localizer.GetString("cookie"),
         QueryStringRequestCultureProvider => this.Localizer.GetString("query-string") + (this.HttpContext.Request.Query.Keys.Contains(label, StringComparer.OrdinalIgnoreCase) ? null : "/" + this.Localizer.GetString("default-settings")),
         RouteDataRequestCultureProvider => this.Localizer.GetString("url"),
         _ => null,
     });
 /// <summary>
 /// Creates a new <see cref="RequestCultureFeature"/> with the specified <see cref="Localization.RequestCulture"/>.
 /// </summary>
 /// <param name="requestCulture">The <see cref="Localization.RequestCulture"/>.</param>
 /// <param name="provider">The <see cref="IRequestCultureProvider"/>.</param>
 public RequestCultureFeature([NotNull] RequestCulture requestCulture, IRequestCultureProvider provider)
 {
     RequestCulture = requestCulture;
     Provider = provider;
 }
        /// <summary>
        /// Invokes the logic of the middleware.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/>.</param>
        /// <returns>A <see cref="Task"/> that completes when the middleware has completed processing.</returns>
        public async Task Invoke(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var requestCulture = _defaultRequestCulture;

            IRequestCultureProvider winningProvider = null;

            if (_options.RequestCultureProviders != null)
            {
                foreach (var provider in _options.RequestCultureProviders)
                {
                    var providerResultCulture = await provider.DetermineProviderCultureResult(context);

                    if (providerResultCulture != null)
                    {
                        var cultures   = providerResultCulture.Cultures;
                        var uiCultures = providerResultCulture.UICultures;

                        CultureInfo cultureInfo   = null;
                        CultureInfo uiCultureInfo = null;
                        if (_options.SupportedCultures != null)
                        {
                            cultureInfo = GetCultureInfo(cultures, _options.SupportedCultures);
                        }

                        if (_options.SupportedUICultures != null)
                        {
                            uiCultureInfo = GetCultureInfo(uiCultures, _options.SupportedUICultures);
                        }

                        if (cultureInfo == null && uiCultureInfo == null)
                        {
                            continue;
                        }
                        if (cultureInfo == null && uiCultureInfo != null)
                        {
                            cultureInfo = _defaultRequestCulture.Culture;
                        }
                        if (cultureInfo != null && uiCultureInfo == null)
                        {
                            uiCultureInfo = _defaultRequestCulture.UICulture;
                        }

                        var result = new RequestCulture(cultureInfo, uiCultureInfo);

                        if (result != null)
                        {
                            requestCulture  = result;
                            winningProvider = provider;
                            break;
                        }
                    }
                }
            }

            context.Features.Set <IRequestCultureFeature>(new RequestCultureFeature(requestCulture, winningProvider));

            SetCurrentThreadCulture(requestCulture);

            await _next(context);
        }
 public DefaultLocalizationStringCollection(IList<CultureInfo> collection, IRequestCultureProvider cultureProvider)
     : base(cultureProvider)
 {
     _collection = collection;
 }