diff --git a/samples/LocalizationSample/Startup.cs b/samples/LocalizationSample/Startup.cs index 8f1bb0d0e6..7b6dd5c4ec 100644 --- a/samples/LocalizationSample/Startup.cs +++ b/samples/LocalizationSample/Startup.cs @@ -23,18 +23,17 @@ namespace LocalizationSample public void Configure(IApplicationBuilder app, IStringLocalizer SR) { - var supportedCultures = new [] { "en-US", "en-AU", "en-GB", "es-ES", "ja-JP", "fr-FR", "zh", "zh-CN" }; + 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 => - //{ + // Optionally create an app-specific provider with just a delegate, e.g. look up user preference from DB. + //.AddRequestCultureProvider(new CustomRequestCultureProvider(async context => + //{ - //})); + //})); ); app.Use(async (context, next) => diff --git a/src/Microsoft.AspNetCore.Localization/RequestLocalizationOptionsExtensions.cs b/src/Microsoft.AspNetCore.Localization/RequestLocalizationOptionsExtensions.cs new file mode 100644 index 0000000000..d0bf911c74 --- /dev/null +++ b/src/Microsoft.AspNetCore.Localization/RequestLocalizationOptionsExtensions.cs @@ -0,0 +1,40 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNetCore.Localization; + +namespace Microsoft.AspNetCore.Builder +{ + /// + /// Extension methods for the . + /// + public static class RequestLocalizationOptionsExtensions + { + /// + /// Adds a new to the . + /// + /// The cultures to be added. + /// The cultures to be added. + /// The . + /// This method ensures that has priority over other instances in . + public static RequestLocalizationOptions AddInitialRequestCultureProvider( + this RequestLocalizationOptions requestLocalizationOptions, + RequestCultureProvider requestCultureProvider) + { + if (requestLocalizationOptions == null) + { + throw new ArgumentNullException(nameof(requestLocalizationOptions)); + } + + if (requestCultureProvider == null) + { + throw new ArgumentNullException(nameof(requestCultureProvider)); + } + + requestLocalizationOptions.RequestCultureProviders.Insert(0, requestCultureProvider); + + return requestLocalizationOptions; + } + } +} diff --git a/test/Microsoft.AspNetCore.Localization.Tests/RequestLocalizationOptionsExtensionsTest.cs b/test/Microsoft.AspNetCore.Localization.Tests/RequestLocalizationOptionsExtensionsTest.cs new file mode 100644 index 0000000000..1910fa95b6 --- /dev/null +++ b/test/Microsoft.AspNetCore.Localization.Tests/RequestLocalizationOptionsExtensionsTest.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Xunit; + +namespace Microsoft.AspNetCore.Localization +{ + public class RequestLocalizationOptionsExtensionsTest + { + [Fact] + public void AddInitialRequestCultureProvider_ShouldBeInsertedAtFirstPostion() + { + // Arrange + var options = new RequestLocalizationOptions(); + var provider = new CustomRequestCultureProvider(context => Task.FromResult(new ProviderCultureResult("ar-YE"))); + + // Act + options.AddInitialRequestCultureProvider(provider); + + // Assert + Assert.Same(provider, options.RequestCultureProviders[0]); + } + } +}