Adding AddRequestCultureProvider as extension method (#458)

* Adding AddRequestCultureProvider as extension method

Fixes aspnet/AspNetCore#3336
This commit is contained in:
Hisham Bin Ateya 2018-11-15 23:23:10 +03:00 committed by Pranav K
parent 9ecc9abd23
commit b8747c8b6b
3 changed files with 71 additions and 6 deletions

View File

@ -23,18 +23,17 @@ namespace LocalizationSample
public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> 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) =>

View File

@ -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
{
/// <summary>
/// Extension methods for the <see cref="RequestLocalizationOptions"/>.
/// </summary>
public static class RequestLocalizationOptionsExtensions
{
/// <summary>
/// Adds a new <see cref="RequestCultureProvider"/> to the <see cref="RequestLocalizationOptions.RequestCultureProviders"/>.
/// </summary>
/// <param name="requestLocalizationOptions">The cultures to be added.</param>
/// <param name="requestCultureProvider">The cultures to be added.</param>
/// <returns>The <see cref="RequestLocalizationOptions"/>.</returns>
/// <remarks>This method ensures that <paramref name="requestCultureProvider"/> has priority over other <see cref="RequestCultureProvider"/> instances in <see cref="RequestLocalizationOptions.RequestCultureProviders"/>.</remarks>
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;
}
}
}

View File

@ -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]);
}
}
}