Usability improvements:

- Remove the defaultRequestCulture parameter from IApplicationBuilder.UseRequestLocalization and make it a property on RequestLocalizationOptions instead
- Have just a single overload for IApplicationBuilder.UseRequestLocalization that takes the options
- #164
This commit is contained in:
damianedwards 2015-12-10 22:10:07 -08:00 committed by DamianEdwards
parent 75187b0bd7
commit 0b013ae769
11 changed files with 189 additions and 82 deletions

View File

@ -24,6 +24,8 @@ namespace LocalizationSample
{ {
var options = new RequestLocalizationOptions var options = new RequestLocalizationOptions
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
// Set options here to change middleware behavior // Set options here to change middleware behavior
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
@ -58,7 +60,7 @@ namespace LocalizationSample
//})); //}));
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Use(async (context, next) => app.Use(async (context, next) =>
{ {

View File

@ -12,46 +12,16 @@ namespace Microsoft.AspNet.Builder
/// </summary> /// </summary>
public static class ApplicationBuilderExtensions public static class ApplicationBuilderExtensions
{ {
/// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client using the default options.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="defaultRequestCulture">The default <see cref="RequestCulture"/> to use if none of the
/// requested cultures match supported cultures.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app,
RequestCulture defaultRequestCulture)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (defaultRequestCulture == null)
{
throw new ArgumentNullException(nameof(defaultRequestCulture));
}
var options = new RequestLocalizationOptions();
return UseRequestLocalization(app, options, defaultRequestCulture);
}
/// <summary> /// <summary>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for /// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client. /// requests based on information provided by the client.
/// </summary> /// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param> /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <param name="options">The options to configure the middleware with.</param> /// <param name="options">The options to configure the middleware with.</param>
/// <param name="defaultRequestCulture">The default <see cref="RequestCulture"/> to use if none of the
/// requested cultures match supported cultures.</param>
/// <returns>The <see cref="IApplicationBuilder"/>.</returns> /// <returns>The <see cref="IApplicationBuilder"/>.</returns>
public static IApplicationBuilder UseRequestLocalization( public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app, this IApplicationBuilder app,
RequestLocalizationOptions options, RequestLocalizationOptions options)
RequestCulture defaultRequestCulture)
{ {
if (app == null) if (app == null)
{ {
@ -63,12 +33,7 @@ namespace Microsoft.AspNet.Builder
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
if (defaultRequestCulture == null) return app.UseMiddleware<RequestLocalizationMiddleware>(options);
{
throw new ArgumentNullException(nameof(defaultRequestCulture));
}
return app.UseMiddleware<RequestLocalizationMiddleware>(options, defaultRequestCulture);
} }
} }
} }

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
using Microsoft.Extensions.Globalization; using Microsoft.Extensions.Globalization;
@ -21,7 +20,6 @@ namespace Microsoft.AspNet.Localization
{ {
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly RequestLocalizationOptions _options; private readonly RequestLocalizationOptions _options;
private readonly RequestCulture _defaultRequestCulture;
/// <summary> /// <summary>
/// Creates a new <see cref="RequestLocalizationMiddleware"/>. /// Creates a new <see cref="RequestLocalizationMiddleware"/>.
@ -29,12 +27,9 @@ namespace Microsoft.AspNet.Localization
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param> /// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the /// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the
/// <see cref="RequestLocalizationMiddleware"/>.</param> /// <see cref="RequestLocalizationMiddleware"/>.</param>
/// <param name="defaultRequestCulture">The default <see cref="RequestCulture"/> to use if none of the
/// requested cultures match supported cultures.</param>
public RequestLocalizationMiddleware( public RequestLocalizationMiddleware(
RequestDelegate next, RequestDelegate next,
RequestLocalizationOptions options, RequestLocalizationOptions options)
RequestCulture defaultRequestCulture)
{ {
if (next == null) if (next == null)
{ {
@ -46,14 +41,8 @@ namespace Microsoft.AspNet.Localization
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
} }
if (defaultRequestCulture == null)
{
throw new ArgumentNullException(nameof(defaultRequestCulture));
}
_next = next; _next = next;
_options = options; _options = options;
_defaultRequestCulture = defaultRequestCulture;
} }
/// <summary> /// <summary>
@ -68,7 +57,7 @@ namespace Microsoft.AspNet.Localization
throw new ArgumentNullException(nameof(context)); throw new ArgumentNullException(nameof(context));
} }
var requestCulture = _defaultRequestCulture; var requestCulture = _options.DefaultRequestCulture;
IRequestCultureProvider winningProvider = null; IRequestCultureProvider winningProvider = null;
@ -100,11 +89,11 @@ namespace Microsoft.AspNet.Localization
} }
if (cultureInfo == null && uiCultureInfo != null) if (cultureInfo == null && uiCultureInfo != null)
{ {
cultureInfo = _defaultRequestCulture.Culture; cultureInfo = _options.DefaultRequestCulture.Culture;
} }
if (cultureInfo != null && uiCultureInfo == null) if (cultureInfo != null && uiCultureInfo == null)
{ {
uiCultureInfo = _defaultRequestCulture.UICulture; uiCultureInfo = _options.DefaultRequestCulture.UICulture;
} }
var result = new RequestCulture(cultureInfo, uiCultureInfo); var result = new RequestCulture(cultureInfo, uiCultureInfo);

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -11,6 +12,9 @@ namespace Microsoft.AspNet.Localization
/// </summary> /// </summary>
public class RequestLocalizationOptions public class RequestLocalizationOptions
{ {
private RequestCulture _defaultRequestCulture =
new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
/// <summary> /// <summary>
/// Creates a new <see cref="RequestLocalizationOptions"/> with default values. /// Creates a new <see cref="RequestLocalizationOptions"/> with default values.
/// </summary> /// </summary>
@ -25,20 +29,40 @@ namespace Microsoft.AspNet.Localization
} }
/// <summary> /// <summary>
/// The cultures supported by the application. If this value is non-<c>null</c>, the /// Gets or sets the default culture to use for requests when a supported culture could not be determined by
/// <see cref="RequestLocalizationMiddleware"/> will only set the current request culture to an entry in this /// one of the configured <see cref="IRequestCultureProvider"/>s.
/// list. A value of <c>null</c> means all cultures are supported. /// Defaults to <see cref="CultureInfo.CurrentCulture"/> and <see cref="CultureInfo.CurrentUICulture"/>.
/// Defaults to <c>null</c>.
/// </summary> /// </summary>
public IList<CultureInfo> SupportedCultures { get; set; } public RequestCulture DefaultRequestCulture
{
get
{
return _defaultRequestCulture;
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_defaultRequestCulture = value;
}
}
/// <summary> /// <summary>
/// The UI cultures supported by the application. If this value is non-<c>null</c>, the /// The cultures supported by the application. The <see cref="RequestLocalizationMiddleware"/> will only set
/// <see cref="RequestLocalizationMiddleware"/> will only set the current request culture to an entry in this /// the current request culture to an entry in this list.
/// list. A value of <c>null</c> means all cultures are supported. /// Defaults to <see cref="CultureInfo.CurrentCulture"/>.
/// Defaults to <c>null</c>.
/// </summary> /// </summary>
public IList<CultureInfo> SupportedUICultures { get; set; } public IList<CultureInfo> SupportedCultures { get; set; } = new List<CultureInfo> { CultureInfo.CurrentCulture };
/// <summary>
/// The UI cultures supported by the application. The <see cref="RequestLocalizationMiddleware"/> will only set
/// the current request culture to an entry in this list.
/// Defaults to <see cref="CultureInfo.CurrentUICulture"/>.
/// </summary>
public IList<CultureInfo> SupportedUICultures { get; set; } = new List<CultureInfo> { CultureInfo.CurrentUICulture };
/// <summary> /// <summary>
/// An ordered list of providers used to determine a request's culture information. The first provider that /// An ordered list of providers used to determine a request's culture information. The first provider that

View File

@ -31,6 +31,7 @@ namespace LocalizationWebsite
var options = new RequestLocalizationOptions var options = new RequestLocalizationOptions
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>() SupportedCultures = new List<CultureInfo>()
{ {
new CultureInfo("fr-FR") new CultureInfo("fr-FR")
@ -41,7 +42,7 @@ namespace LocalizationWebsite
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
var stringLocalizer = stringLocalizerFactory.Create("Test", location: null); var stringLocalizer = stringLocalizerFactory.Create("Test", location: null);

View File

@ -31,6 +31,7 @@ namespace LocalizationWebsite
var options = new RequestLocalizationOptions var options = new RequestLocalizationOptions
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>() SupportedCultures = new List<CultureInfo>()
{ {
new CultureInfo("fr-FR") new CultureInfo("fr-FR")
@ -41,7 +42,7 @@ namespace LocalizationWebsite
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
var stringLocalizer = stringLocalizerFactory.Create("Test", location: null); var stringLocalizer = stringLocalizerFactory.Create("Test", location: null);

View File

@ -21,13 +21,14 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions var options = new RequestLocalizationOptions
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA"), new CultureInfo("ar-SA"),
new CultureInfo("en-US") new CultureInfo("en-US")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -52,13 +53,14 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions var options = new RequestLocalizationOptions
{ {
DefaultRequestCulture = new RequestCulture("fr-FR"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA"), new CultureInfo("ar-SA"),
new CultureInfo("en-US") new CultureInfo("en-US")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("fr-FR")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -82,13 +84,14 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions var options = new RequestLocalizationOptions
{ {
DefaultRequestCulture = new RequestCulture("fr-FR"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA"), new CultureInfo("ar-SA"),
new CultureInfo("af-ZA") new CultureInfo("af-ZA")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("fr-FR")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -113,6 +116,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-YE") new CultureInfo("ar-YE")
@ -122,7 +126,7 @@ namespace Microsoft.Extensions.Localization.Tests
new CultureInfo("ar-YE") new CultureInfo("ar-YE")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

View File

@ -22,6 +22,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -34,7 +35,7 @@ namespace Microsoft.Extensions.Localization.Tests
var provider = new CookieRequestCultureProvider(); var provider = new CookieRequestCultureProvider();
provider.CookieName = "Preferences"; provider.CookieName = "Preferences";
options.RequestCultureProviders.Insert(0, provider); options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -61,6 +62,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -73,7 +75,7 @@ namespace Microsoft.Extensions.Localization.Tests
var provider = new CookieRequestCultureProvider(); var provider = new CookieRequestCultureProvider();
provider.CookieName = "Preferences"; provider.CookieName = "Preferences";
options.RequestCultureProviders.Insert(0, provider); options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -96,6 +98,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -108,7 +111,7 @@ namespace Microsoft.Extensions.Localization.Tests
var provider = new CookieRequestCultureProvider(); var provider = new CookieRequestCultureProvider();
provider.CookieName = "Preferences"; provider.CookieName = "Preferences";
options.RequestCultureProviders.Insert(0, provider); options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

View File

@ -24,6 +24,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar") new CultureInfo("ar")
@ -39,7 +40,7 @@ namespace Microsoft.Extensions.Localization.Tests
var requestCulture = new ProviderCultureResult(culture); var requestCulture = new ProviderCultureResult(culture);
return Task.FromResult(requestCulture); return Task.FromResult(requestCulture);
})); }));
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

View File

@ -21,6 +21,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -31,7 +32,7 @@ namespace Microsoft.Extensions.Localization.Tests
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -52,8 +53,11 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
using (var server = TestServer.Create(app => using (var server = TestServer.Create(app =>
{ {
var options = new RequestLocalizationOptions(); var options = new RequestLocalizationOptions
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); {
DefaultRequestCulture = new RequestCulture("en-US"),
};
app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -76,6 +80,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -85,7 +90,7 @@ namespace Microsoft.Extensions.Localization.Tests
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -107,6 +112,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -116,7 +122,7 @@ namespace Microsoft.Extensions.Localization.Tests
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -138,6 +144,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -147,7 +154,7 @@ namespace Microsoft.Extensions.Localization.Tests
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -170,6 +177,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -179,7 +187,7 @@ namespace Microsoft.Extensions.Localization.Tests
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
} }
}; };
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -202,6 +210,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("ar-SA") new CultureInfo("ar-SA")
@ -215,7 +224,7 @@ namespace Microsoft.Extensions.Localization.Tests
provider.QueryStringKey = "c"; provider.QueryStringKey = "c";
provider.UIQueryStringKey = "uic"; provider.UIQueryStringKey = "uic";
options.RequestCultureProviders.Insert(0, provider); options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@ -238,6 +247,7 @@ namespace Microsoft.Extensions.Localization.Tests
{ {
var options = new RequestLocalizationOptions() var options = new RequestLocalizationOptions()
{ {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo> SupportedCultures = new List<CultureInfo>
{ {
new CultureInfo("FR") new CultureInfo("FR")
@ -251,7 +261,7 @@ namespace Microsoft.Extensions.Localization.Tests
provider.QueryStringKey = "c"; provider.QueryStringKey = "c";
provider.UIQueryStringKey = "uic"; provider.UIQueryStringKey = "uic";
options.RequestCultureProviders.Insert(0, provider); options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US")); app.UseRequestLocalization(options);
app.Run(context => app.Run(context =>
{ {
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>(); var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

View File

@ -0,0 +1,107 @@
// 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 System.Globalization;
using System.Threading;
using Xunit;
namespace Microsoft.AspNet.Localization.Tests
{
public class RequestLocalizationOptionsTest : IDisposable
{
private readonly CultureInfo _initialCulture;
private readonly CultureInfo _initialUICulture;
public RequestLocalizationOptionsTest()
{
_initialCulture = CultureInfo.CurrentCulture;
_initialUICulture = CultureInfo.CurrentUICulture;
}
[Fact]
public void DefaultRequestCulture_DefaultsToCurrentCulture()
{
// Arrange/Act
var options = new RequestLocalizationOptions();
// Assert
Assert.NotNull(options.DefaultRequestCulture);
Assert.Equal(CultureInfo.CurrentCulture, options.DefaultRequestCulture.Culture);
Assert.Equal(CultureInfo.CurrentUICulture, options.DefaultRequestCulture.UICulture);
}
[Fact]
public void DefaultRequestCulture_DefaultsToCurrentCultureWhenExplicitlySet()
{
// Arrange
var explicitCulture = new CultureInfo("fr-FR");
#if DNX451
Thread.CurrentThread.CurrentCulture = explicitCulture;
Thread.CurrentThread.CurrentUICulture = explicitCulture;
#else
CultureInfo.CurrentCulture = explicitCulture;
CultureInfo.CurrentUICulture = explicitCulture;
#endif
// Act
var options = new RequestLocalizationOptions();
// Assert
Assert.Equal(explicitCulture, options.DefaultRequestCulture.Culture);
Assert.Equal(explicitCulture, options.DefaultRequestCulture.UICulture);
}
[Fact]
public void DefaultRequestCulture_ThrowsWhenTryingToSetToNull()
{
// Arrange
var options = new RequestLocalizationOptions();
// Act/Assert
Assert.Throws(typeof(ArgumentNullException), () => options.DefaultRequestCulture = null);
}
[Fact]
public void SupportedCultures_DefaultsToCurrentCulture()
{
// Arrange/Act
var options = new RequestLocalizationOptions();
// Assert
Assert.Collection(options.SupportedCultures, item => Assert.Equal(CultureInfo.CurrentCulture, item));
Assert.Collection(options.SupportedUICultures, item => Assert.Equal(CultureInfo.CurrentUICulture, item));
}
[Fact]
public void SupportedCultures_DefaultsToCurrentCultureWhenExplicitlySet()
{
// Arrange
var explicitCulture = new CultureInfo("fr-FR");
#if DNX451
Thread.CurrentThread.CurrentCulture = explicitCulture;
Thread.CurrentThread.CurrentUICulture = explicitCulture;
#else
CultureInfo.CurrentCulture = explicitCulture;
CultureInfo.CurrentUICulture = explicitCulture;
#endif
// Act
var options = new RequestLocalizationOptions();
// Assert
Assert.Collection(options.SupportedCultures, item => Assert.Equal(explicitCulture, item));
Assert.Collection(options.SupportedUICultures, item => Assert.Equal(explicitCulture, item));
}
public void Dispose()
{
#if DNX451
Thread.CurrentThread.CurrentCulture = _initialCulture;
Thread.CurrentThread.CurrentUICulture = _initialUICulture;
#else
CultureInfo.CurrentCulture = _initialCulture;
CultureInfo.CurrentUICulture = _initialUICulture;
#endif
}
}
}