/// <summary> /// Creates a string representation of a <see cref="RequestCulture"/> for placement in a cookie. /// </summary> /// <param name="requestCulture">The <see cref="RequestCulture"/>.</param> /// <returns>The cookie value.</returns> public static string MakeCookieValue(RequestCulture requestCulture) { if (requestCulture == null) { throw new ArgumentNullException(nameof(requestCulture)); } return(string.Join(_cookieSeparator.ToString(), $"{_culturePrefix}{requestCulture.Culture.Name}", $"{_uiCulturePrefix}{requestCulture.UICulture.Name}")); }
/// <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); }
private static void SetCurrentThreadCulture(RequestCulture requestCulture) { CultureInfo.CurrentCulture = requestCulture.Culture; CultureInfo.CurrentUICulture = requestCulture.UICulture; }
/// <summary> /// Set the default culture which is used by the application when a supported culture could not be determined by /// one of the configured <see cref="IRequestCultureProvider"/>s. /// </summary> /// <param name="defaultCulture">The default culture to be set.</param> /// <returns>The <see cref="RequestLocalizationOptions"/>.</returns> public RequestLocalizationOptions SetDefaultCulture(string defaultCulture) { DefaultRequestCulture = new RequestCulture(defaultCulture); return(this); }