Add a builder API for configuring UseRequestLocalization
This commit is contained in:
parent
683ef56b76
commit
29f7e4bd62
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
|
@ -24,31 +23,19 @@ namespace LocalizationSample
|
|||
|
||||
public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR)
|
||||
{
|
||||
var supportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("en-US"),
|
||||
new CultureInfo("en-AU"),
|
||||
new CultureInfo("en-GB"),
|
||||
new CultureInfo("es-ES"),
|
||||
new CultureInfo("ja-JP"),
|
||||
new CultureInfo("fr-FR"),
|
||||
new CultureInfo("zh"),
|
||||
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 =>
|
||||
//{
|
||||
var supportedCultures = new [] { "en-US", "en-AU", "en-GB", "es-ES", "ja-JP", "fr-FR", "zh", "zh-CN" };
|
||||
app.UseRequestLocalization(options =>
|
||||
options
|
||||
.AddSupportedCultures(supportedCultures)
|
||||
.AddSupportedUICultures(supportedCultures)
|
||||
.SetDefaultCulture(supportedCultures[0])
|
||||
// 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.
|
||||
//.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
|
||||
//{
|
||||
|
||||
//}));
|
||||
|
||||
app.UseRequestLocalization(options);
|
||||
//}));
|
||||
);
|
||||
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ namespace Microsoft.AspNetCore.Builder
|
|||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
|
|
@ -50,5 +51,69 @@ namespace Microsoft.AspNetCore.Builder
|
|||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -109,5 +109,53 @@ namespace Microsoft.AspNetCore.Builder
|
|||
/// </list>
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue