public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var request = httpContext.Request;
            if (!request.Path.HasValue)
            {
                return Task.FromResult((ProviderCultureResult)null);
            }

            var cultureValue = Regex.Match(
                request.Path.Value,
                @"^/([a-z]{2})(?:$|/)",
                RegexOptions.IgnoreCase);

            if (cultureValue.Success)
            {

                var culture = CultureInfoCache.GetCultureInfo(cultureValue.Groups[1].Value, Options.SupportedCultures);
                var uiCulture = CultureInfoCache.GetCultureInfo(cultureValue.Groups[1].Value, Options.SupportedCultures);

                if (culture == null || uiCulture == null)
                {
                    return Task.FromResult((ProviderCultureResult)null);
                }

                //if (culture.Name == Options.DefaultRequestCulture.Culture.TwoLetterISOLanguageName)
                if(culture.Name == new CultureInfo("sv").TwoLetterISOLanguageName)
                {
                    if (httpContext.Request.Path.Equals(new PathString("/" + culture.TwoLetterISOLanguageName)))
                    {
                        httpContext.Response.Redirect("/", true);
                    }
                    else
                    {
                        var remaining = httpContext.Request.Path.Value.Substring(3);
                        httpContext.Response.Redirect(remaining, true);
                    }
                }

                var requestCulture = new ProviderCultureResult(culture.Name, uiCulture.Name);

                return Task.FromResult(requestCulture);
            }
            else
            {
                //var requestCulture = new ProviderCultureResult(Options.DefaultRequestCulture.Culture.TwoLetterISOLanguageName, Options.DefaultRequestCulture.UICulture.TwoLetterISOLanguageName);
                var requestCulture = new ProviderCultureResult(new CultureInfo("sv").TwoLetterISOLanguageName, new CultureInfo("sv").TwoLetterISOLanguageName);

                return Task.FromResult(requestCulture);
            }
        }
        /// <inheritdoc />
        public override Task <ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var request = httpContext.Request;

            if (!request.QueryString.HasValue)
            {
                return(TaskCache <ProviderCultureResult> .DefaultCompletedTask);
            }

            string queryCulture   = null;
            string queryUICulture = null;

            if (!string.IsNullOrWhiteSpace(QueryStringKey))
            {
                queryCulture = request.Query[QueryStringKey];
            }

            if (!string.IsNullOrWhiteSpace(UIQueryStringKey))
            {
                queryUICulture = request.Query[UIQueryStringKey];
            }

            if (queryCulture == null && queryUICulture == null)
            {
                // No values specified for either so no match
                return(TaskCache <ProviderCultureResult> .DefaultCompletedTask);
            }

            if (queryCulture != null && queryUICulture == null)
            {
                // Value for culture but not for UI culture so default to culture value for both
                queryUICulture = queryCulture;
            }

            if (queryCulture == null && queryUICulture != null)
            {
                // Value for UI culture but not for culture so default to UI culture value for both
                queryCulture = queryUICulture;
            }

            var providerResultCulture = new ProviderCultureResult(queryCulture, queryUICulture);

            return(Task.FromResult(providerResultCulture));
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public Task <ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var culture = httpContext.GetRouteValue(RouteParameterKey);

            if (culture == null)
            {
                return(NullProviderCultureResult);
            }

            var providerResultCulture = new ProviderCultureResult(culture.ToString());

            return(Task.FromResult(providerResultCulture));
        }