Add a builder API for configuring UseRequestLocalization

This commit is contained in:
hishamco 2017-05-16 09:12:25 +03:00
parent 683ef56b76
commit 29f7e4bd62
3 changed files with 125 additions and 25 deletions

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -24,31 +23,19 @@ namespace LocalizationSample
public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR) public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR)
{ {
var supportedCultures = new List<CultureInfo> var supportedCultures = new [] { "en-US", "en-AU", "en-GB", "es-ES", "ja-JP", "fr-FR", "zh", "zh-CN" };
{ app.UseRequestLocalization(options =>
new CultureInfo("en-US"), options
new CultureInfo("en-AU"), .AddSupportedCultures(supportedCultures)
new CultureInfo("en-GB"), .AddSupportedUICultures(supportedCultures)
new CultureInfo("es-ES"), .SetDefaultCulture(supportedCultures[0])
new CultureInfo("ja-JP"), // Optionally create an app-specific provider with just a delegate, e.g. look up user preference from DB.
new CultureInfo("fr-FR"), // Inserting it as position 0 ensures it has priority over any of the default providers.
new CultureInfo("zh"), //.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
new CultureInfo("zh-CN") //{
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
// Optionally create an app-specific provider with just a delegate, e.g. look up user preference from DB.
// Inserting it as position 0 ensures it has priority over any of the default providers.
//options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
//{
//})); //}));
);
app.UseRequestLocalization(options);
app.Use(async (context, next) => app.Use(async (context, next) =>
{ {

View File

@ -43,6 +43,7 @@ namespace Microsoft.AspNetCore.Builder
{ {
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
if (options == null) if (options == null)
{ {
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
@ -50,5 +51,69 @@ namespace Microsoft.AspNetCore.Builder
return app.UseMiddleware<RequestLocalizationMiddleware>(Options.Create(options)); return app.UseMiddleware<RequestLocalizationMiddleware>(Options.Create(options));
} }
/// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="optionsAction"></param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app,
Action<RequestLocalizationOptions> optionsAction)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (optionsAction == null)
{
throw new ArgumentNullException(nameof(optionsAction));
}
var options = new RequestLocalizationOptions();
optionsAction.Invoke(options);
return app.UseMiddleware<RequestLocalizationMiddleware>(Options.Create(options));
}
/// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="cultures">The culture names to be added by the application, which is represents both supported cultures and UI cultures.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
/// <remarks>
/// Note that the first culture is the default culture name.
/// </remarks>
public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app,
params string[] cultures)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (cultures == null)
{
throw new ArgumentNullException(nameof(cultures));
}
if (cultures.Length == 0)
{
throw new ArgumentException(nameof(cultures));
}
var options = new RequestLocalizationOptions()
.AddSupportedCultures(cultures)
.AddSupportedUICultures(cultures)
.SetDefaultCulture(cultures[0]);
return app.UseMiddleware<RequestLocalizationMiddleware>(Options.Create(options));
}
} }
} }

View File

@ -109,5 +109,53 @@ namespace Microsoft.AspNetCore.Builder
/// </list> /// </list>
/// </summary> /// </summary>
public IList<IRequestCultureProvider> RequestCultureProviders { get; set; } public IList<IRequestCultureProvider> RequestCultureProviders { get; set; }
/// <summary>
/// Adds the set of the supported cultures by the application.
/// </summary>
/// <param name="cultures">The cultures to be added.</param>
/// <returns>The <see cref="RequestLocalizationOptions"/>.</returns>
public RequestLocalizationOptions AddSupportedCultures(params string[] cultures)
{
var supportedCultures = new List<CultureInfo>();
foreach (var culture in cultures)
{
supportedCultures.Add(new CultureInfo(culture));
}
SupportedCultures = supportedCultures;
return this;
}
/// <summary>
/// Adds the set of the supported UI cultures by the application.
/// </summary>
/// <param name="uiCultures">The UI cultures to be added.</param>
/// <returns>The <see cref="RequestLocalizationOptions"/>.</returns>
public RequestLocalizationOptions AddSupportedUICultures(params string[] uiCultures)
{
var supportedUICultures = new List<CultureInfo>();
foreach (var culture in uiCultures)
{
supportedUICultures.Add(new CultureInfo(culture));
}
SupportedUICultures = supportedUICultures;
return this;
}
/// <summary>
/// Set the default culture to be 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;
}
} }
} }