Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Commit 0b013ae

Browse files
committed
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
1 parent 75187b0 commit 0b013ae

11 files changed

+189
-82
lines changed

samples/LocalizationSample/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR)
2424
{
2525
var options = new RequestLocalizationOptions
2626
{
27+
DefaultRequestCulture = new RequestCulture("en-US"),
28+
2729
// Set options here to change middleware behavior
2830
SupportedCultures = new List<CultureInfo>
2931
{
@@ -58,7 +60,7 @@ public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR)
5860

5961
//}));
6062

61-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
63+
app.UseRequestLocalization(options);
6264

6365
app.Use(async (context, next) =>
6466
{

src/Microsoft.AspNet.Localization/ApplicationBuilderExtensions.cs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,16 @@ namespace Microsoft.AspNet.Builder
1212
/// </summary>
1313
public static class ApplicationBuilderExtensions
1414
{
15-
/// <summary>
16-
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
17-
/// requests based on information provided by the client using the default options.
18-
/// </summary>
19-
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
20-
/// <param name="defaultRequestCulture">The default <see cref="RequestCulture"/> to use if none of the
21-
/// requested cultures match supported cultures.</param>
22-
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
23-
public static IApplicationBuilder UseRequestLocalization(
24-
this IApplicationBuilder app,
25-
RequestCulture defaultRequestCulture)
26-
{
27-
if (app == null)
28-
{
29-
throw new ArgumentNullException(nameof(app));
30-
}
31-
32-
if (defaultRequestCulture == null)
33-
{
34-
throw new ArgumentNullException(nameof(defaultRequestCulture));
35-
}
36-
37-
var options = new RequestLocalizationOptions();
38-
39-
return UseRequestLocalization(app, options, defaultRequestCulture);
40-
}
41-
4215
/// <summary>
4316
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
4417
/// requests based on information provided by the client.
4518
/// </summary>
4619
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
4720
/// <param name="options">The options to configure the middleware with.</param>
48-
/// <param name="defaultRequestCulture">The default <see cref="RequestCulture"/> to use if none of the
49-
/// requested cultures match supported cultures.</param>
5021
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
5122
public static IApplicationBuilder UseRequestLocalization(
5223
this IApplicationBuilder app,
53-
RequestLocalizationOptions options,
54-
RequestCulture defaultRequestCulture)
24+
RequestLocalizationOptions options)
5525
{
5626
if (app == null)
5727
{
@@ -63,12 +33,7 @@ public static IApplicationBuilder UseRequestLocalization(
6333
throw new ArgumentNullException(nameof(options));
6434
}
6535

66-
if (defaultRequestCulture == null)
67-
{
68-
throw new ArgumentNullException(nameof(defaultRequestCulture));
69-
}
70-
71-
return app.UseMiddleware<RequestLocalizationMiddleware>(options, defaultRequestCulture);
36+
return app.UseMiddleware<RequestLocalizationMiddleware>(options);
7237
}
7338
}
7439
}

src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Globalization;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Microsoft.AspNet.Builder;
109
using Microsoft.AspNet.Http;
1110
using Microsoft.AspNet.Http.Features;
1211
using Microsoft.Extensions.Globalization;
@@ -21,20 +20,16 @@ public class RequestLocalizationMiddleware
2120
{
2221
private readonly RequestDelegate _next;
2322
private readonly RequestLocalizationOptions _options;
24-
private readonly RequestCulture _defaultRequestCulture;
2523

2624
/// <summary>
2725
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
2826
/// </summary>
2927
/// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param>
3028
/// <param name="options">The <see cref="RequestLocalizationOptions"/> representing the options for the
3129
/// <see cref="RequestLocalizationMiddleware"/>.</param>
32-
/// <param name="defaultRequestCulture">The default <see cref="RequestCulture"/> to use if none of the
33-
/// requested cultures match supported cultures.</param>
3430
public RequestLocalizationMiddleware(
3531
RequestDelegate next,
36-
RequestLocalizationOptions options,
37-
RequestCulture defaultRequestCulture)
32+
RequestLocalizationOptions options)
3833
{
3934
if (next == null)
4035
{
@@ -46,14 +41,8 @@ public RequestLocalizationMiddleware(
4641
throw new ArgumentNullException(nameof(options));
4742
}
4843

49-
if (defaultRequestCulture == null)
50-
{
51-
throw new ArgumentNullException(nameof(defaultRequestCulture));
52-
}
53-
5444
_next = next;
5545
_options = options;
56-
_defaultRequestCulture = defaultRequestCulture;
5746
}
5847

5948
/// <summary>
@@ -68,7 +57,7 @@ public async Task Invoke(HttpContext context)
6857
throw new ArgumentNullException(nameof(context));
6958
}
7059

71-
var requestCulture = _defaultRequestCulture;
60+
var requestCulture = _options.DefaultRequestCulture;
7261

7362
IRequestCultureProvider winningProvider = null;
7463

@@ -100,11 +89,11 @@ public async Task Invoke(HttpContext context)
10089
}
10190
if (cultureInfo == null && uiCultureInfo != null)
10291
{
103-
cultureInfo = _defaultRequestCulture.Culture;
92+
cultureInfo = _options.DefaultRequestCulture.Culture;
10493
}
10594
if (cultureInfo != null && uiCultureInfo == null)
10695
{
107-
uiCultureInfo = _defaultRequestCulture.UICulture;
96+
uiCultureInfo = _options.DefaultRequestCulture.UICulture;
10897
}
10998

11099
var result = new RequestCulture(cultureInfo, uiCultureInfo);

src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Globalization;
67

@@ -11,6 +12,9 @@ namespace Microsoft.AspNet.Localization
1112
/// </summary>
1213
public class RequestLocalizationOptions
1314
{
15+
private RequestCulture _defaultRequestCulture =
16+
new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
17+
1418
/// <summary>
1519
/// Creates a new <see cref="RequestLocalizationOptions"/> with default values.
1620
/// </summary>
@@ -25,20 +29,40 @@ public RequestLocalizationOptions()
2529
}
2630

2731
/// <summary>
28-
/// The cultures supported by the application. If this value is non-<c>null</c>, the
29-
/// <see cref="RequestLocalizationMiddleware"/> will only set the current request culture to an entry in this
30-
/// list. A value of <c>null</c> means all cultures are supported.
31-
/// Defaults to <c>null</c>.
32+
/// Gets or sets the default culture to use for requests when a supported culture could not be determined by
33+
/// one of the configured <see cref="IRequestCultureProvider"/>s.
34+
/// Defaults to <see cref="CultureInfo.CurrentCulture"/> and <see cref="CultureInfo.CurrentUICulture"/>.
35+
/// </summary>
36+
public RequestCulture DefaultRequestCulture
37+
{
38+
get
39+
{
40+
return _defaultRequestCulture;
41+
}
42+
set
43+
{
44+
if (value == null)
45+
{
46+
throw new ArgumentNullException(nameof(value));
47+
}
48+
49+
_defaultRequestCulture = value;
50+
}
51+
}
52+
53+
/// <summary>
54+
/// The cultures supported by the application. The <see cref="RequestLocalizationMiddleware"/> will only set
55+
/// the current request culture to an entry in this list.
56+
/// Defaults to <see cref="CultureInfo.CurrentCulture"/>.
3257
/// </summary>
33-
public IList<CultureInfo> SupportedCultures { get; set; }
58+
public IList<CultureInfo> SupportedCultures { get; set; } = new List<CultureInfo> { CultureInfo.CurrentCulture };
3459

3560
/// <summary>
36-
/// The UI cultures supported by the application. If this value is non-<c>null</c>, the
37-
/// <see cref="RequestLocalizationMiddleware"/> will only set the current request culture to an entry in this
38-
/// list. A value of <c>null</c> means all cultures are supported.
39-
/// Defaults to <c>null</c>.
61+
/// The UI cultures supported by the application. The <see cref="RequestLocalizationMiddleware"/> will only set
62+
/// the current request culture to an entry in this list.
63+
/// Defaults to <see cref="CultureInfo.CurrentUICulture"/>.
4064
/// </summary>
41-
public IList<CultureInfo> SupportedUICultures { get; set; }
65+
public IList<CultureInfo> SupportedUICultures { get; set; } = new List<CultureInfo> { CultureInfo.CurrentUICulture };
4266

4367
/// <summary>
4468
/// An ordered list of providers used to determine a request's culture information. The first provider that

test/LocalizationWebsite/StartupResourcesAtRootFolder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void Configure(
3131

3232
var options = new RequestLocalizationOptions
3333
{
34+
DefaultRequestCulture = new RequestCulture("en-US"),
3435
SupportedCultures = new List<CultureInfo>()
3536
{
3637
new CultureInfo("fr-FR")
@@ -41,7 +42,7 @@ public void Configure(
4142
}
4243
};
4344

44-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
45+
app.UseRequestLocalization(options);
4546

4647
var stringLocalizer = stringLocalizerFactory.Create("Test", location: null);
4748

test/LocalizationWebsite/StartupResourcesInFolder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void Configure(
3131

3232
var options = new RequestLocalizationOptions
3333
{
34+
DefaultRequestCulture = new RequestCulture("en-US"),
3435
SupportedCultures = new List<CultureInfo>()
3536
{
3637
new CultureInfo("fr-FR")
@@ -41,7 +42,7 @@ public void Configure(
4142
}
4243
};
4344

44-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
45+
app.UseRequestLocalization(options);
4546

4647
var stringLocalizer = stringLocalizerFactory.Create("Test", location: null);
4748

test/Microsoft.AspNet.Localization.Tests/AcceptLanguageHeaderRequestCultureProviderTest.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ public async void GetFallbackLanguage_ReturnsFirstNonNullCultureFromSupportedCul
2121
{
2222
var options = new RequestLocalizationOptions
2323
{
24+
DefaultRequestCulture = new RequestCulture("en-US"),
2425
SupportedCultures = new List<CultureInfo>
2526
{
2627
new CultureInfo("ar-SA"),
2728
new CultureInfo("en-US")
2829
}
2930
};
30-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
31+
app.UseRequestLocalization(options);
3132
app.Run(context =>
3233
{
3334
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@@ -52,13 +53,14 @@ public async void GetFallbackLanguage_ReturnsFromSupportedCulture_AcceptLanguage
5253
{
5354
var options = new RequestLocalizationOptions
5455
{
56+
DefaultRequestCulture = new RequestCulture("fr-FR"),
5557
SupportedCultures = new List<CultureInfo>
5658
{
5759
new CultureInfo("ar-SA"),
5860
new CultureInfo("en-US")
5961
}
6062
};
61-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("fr-FR"));
63+
app.UseRequestLocalization(options);
6264
app.Run(context =>
6365
{
6466
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@@ -82,13 +84,14 @@ public async void GetFallbackLanguage_ReturnsDefault_AcceptLanguageListDoesnotCo
8284
{
8385
var options = new RequestLocalizationOptions
8486
{
87+
DefaultRequestCulture = new RequestCulture("fr-FR"),
8588
SupportedCultures = new List<CultureInfo>
8689
{
8790
new CultureInfo("ar-SA"),
8891
new CultureInfo("af-ZA")
8992
}
9093
};
91-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("fr-FR"));
94+
app.UseRequestLocalization(options);
9295
app.Run(context =>
9396
{
9497
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@@ -113,6 +116,7 @@ public async void OmitDefaultRequestCultureShouldNotThrowNullReferenceException_
113116
{
114117
var options = new RequestLocalizationOptions()
115118
{
119+
DefaultRequestCulture = new RequestCulture("en-US"),
116120
SupportedCultures = new List<CultureInfo>
117121
{
118122
new CultureInfo("ar-YE")
@@ -122,7 +126,7 @@ public async void OmitDefaultRequestCultureShouldNotThrowNullReferenceException_
122126
new CultureInfo("ar-YE")
123127
}
124128
};
125-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
129+
app.UseRequestLocalization(options);
126130
app.Run(context =>
127131
{
128132
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

test/Microsoft.AspNet.Localization.Tests/CookieRequestCultureProviderTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public async void GetCultureInfoFromPersistentCookie()
2222
{
2323
var options = new RequestLocalizationOptions()
2424
{
25+
DefaultRequestCulture = new RequestCulture("en-US"),
2526
SupportedCultures = new List<CultureInfo>
2627
{
2728
new CultureInfo("ar-SA")
@@ -34,7 +35,7 @@ public async void GetCultureInfoFromPersistentCookie()
3435
var provider = new CookieRequestCultureProvider();
3536
provider.CookieName = "Preferences";
3637
options.RequestCultureProviders.Insert(0, provider);
37-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
38+
app.UseRequestLocalization(options);
3839
app.Run(context =>
3940
{
4041
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@@ -61,6 +62,7 @@ public async void GetDefaultCultureInfoIfCultureKeysAreMissingOrInvalid()
6162
{
6263
var options = new RequestLocalizationOptions()
6364
{
65+
DefaultRequestCulture = new RequestCulture("en-US"),
6466
SupportedCultures = new List<CultureInfo>
6567
{
6668
new CultureInfo("ar-SA")
@@ -73,7 +75,7 @@ public async void GetDefaultCultureInfoIfCultureKeysAreMissingOrInvalid()
7375
var provider = new CookieRequestCultureProvider();
7476
provider.CookieName = "Preferences";
7577
options.RequestCultureProviders.Insert(0, provider);
76-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
78+
app.UseRequestLocalization(options);
7779
app.Run(context =>
7880
{
7981
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
@@ -96,6 +98,7 @@ public async void GetDefaultCultureInfoIfCookieDoesNotExist()
9698
{
9799
var options = new RequestLocalizationOptions()
98100
{
101+
DefaultRequestCulture = new RequestCulture("en-US"),
99102
SupportedCultures = new List<CultureInfo>
100103
{
101104
new CultureInfo("ar-SA")
@@ -108,7 +111,7 @@ public async void GetDefaultCultureInfoIfCookieDoesNotExist()
108111
var provider = new CookieRequestCultureProvider();
109112
provider.CookieName = "Preferences";
110113
options.RequestCultureProviders.Insert(0, provider);
111-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
114+
app.UseRequestLocalization(options);
112115
app.Run(context =>
113116
{
114117
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

test/Microsoft.AspNet.Localization.Tests/CustomRequestCultureProviderTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public async void CustomRequestCultureProviderThatGetsCultureInfoFromUrl()
2424
{
2525
var options = new RequestLocalizationOptions()
2626
{
27+
DefaultRequestCulture = new RequestCulture("en-US"),
2728
SupportedCultures = new List<CultureInfo>
2829
{
2930
new CultureInfo("ar")
@@ -39,7 +40,7 @@ public async void CustomRequestCultureProviderThatGetsCultureInfoFromUrl()
3940
var requestCulture = new ProviderCultureResult(culture);
4041
return Task.FromResult(requestCulture);
4142
}));
42-
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
43+
app.UseRequestLocalization(options);
4344
app.Run(context =>
4445
{
4546
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();

0 commit comments

Comments
 (0)