From 29f7e4bd6209764a0f92b19e878ea378263c0302 Mon Sep 17 00:00:00 2001 From: hishamco Date: Tue, 16 May 2017 09:12:25 +0300 Subject: [PATCH] Add a builder API for configuring UseRequestLocalization --- samples/LocalizationSample/Startup.cs | 37 ++++------- .../ApplicationBuilderExtensions.cs | 65 +++++++++++++++++++ .../RequestLocalizationOptions.cs | 48 ++++++++++++++ 3 files changed, 125 insertions(+), 25 deletions(-) diff --git a/samples/LocalizationSample/Startup.cs b/samples/LocalizationSample/Startup.cs index ef6e638b22..8f1bb0d0e6 100644 --- a/samples/LocalizationSample/Startup.cs +++ b/samples/LocalizationSample/Startup.cs @@ -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 SR) { - var supportedCultures = new List - { - 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) => { diff --git a/src/Microsoft.AspNetCore.Localization/ApplicationBuilderExtensions.cs b/src/Microsoft.AspNetCore.Localization/ApplicationBuilderExtensions.cs index 4e55497cb2..245ad7485d 100644 --- a/src/Microsoft.AspNetCore.Localization/ApplicationBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Localization/ApplicationBuilderExtensions.cs @@ -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(Options.Create(options)); } + + /// + /// Adds the to automatically set culture information for + /// requests based on information provided by the client. + /// + /// The . + /// + /// The . + public static IApplicationBuilder UseRequestLocalization( + this IApplicationBuilder app, + Action 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(Options.Create(options)); + } + + /// + /// Adds the to automatically set culture information for + /// requests based on information provided by the client. + /// + /// The . + /// The culture names to be added by the application, which is represents both supported cultures and UI cultures. + /// The . + /// + /// Note that the first culture is the default culture name. + /// + 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(Options.Create(options)); + } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Localization/RequestLocalizationOptions.cs b/src/Microsoft.AspNetCore.Localization/RequestLocalizationOptions.cs index a3a39b4fcb..ed49ce9006 100644 --- a/src/Microsoft.AspNetCore.Localization/RequestLocalizationOptions.cs +++ b/src/Microsoft.AspNetCore.Localization/RequestLocalizationOptions.cs @@ -109,5 +109,53 @@ namespace Microsoft.AspNetCore.Builder /// /// public IList RequestCultureProviders { get; set; } + + + /// + /// Adds the set of the supported cultures by the application. + /// + /// The cultures to be added. + /// The . + public RequestLocalizationOptions AddSupportedCultures(params string[] cultures) + { + var supportedCultures = new List(); + + foreach (var culture in cultures) + { + supportedCultures.Add(new CultureInfo(culture)); + } + + SupportedCultures = supportedCultures; + return this; + } + + /// + /// Adds the set of the supported UI cultures by the application. + /// + /// The UI cultures to be added. + /// The . + public RequestLocalizationOptions AddSupportedUICultures(params string[] uiCultures) + { + var supportedUICultures = new List(); + foreach (var culture in uiCultures) + { + supportedUICultures.Add(new CultureInfo(culture)); + } + + SupportedUICultures = supportedUICultures; + return this; + } + + /// + /// Set the default culture to be used by the application when a supported culture could not be determined by + /// one of the configured s. + /// + /// The default culture to be set. + /// The . + public RequestLocalizationOptions SetDefaultCulture(string defaultCulture) + { + DefaultRequestCulture = new RequestCulture(defaultCulture); + return this; + } } }