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

Improve usability of the request localization middleware #164 #170

Merged
merged 1 commit into from
Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion samples/LocalizationSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR)
{
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),

// Set options here to change middleware behavior
SupportedCultures = new List<CultureInfo>
{
Expand Down Expand Up @@ -58,7 +60,7 @@ public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR)

//}));

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

app.Use(async (context, next) =>
{
Expand Down
39 changes: 2 additions & 37 deletions src/Microsoft.AspNet.Localization/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,16 @@ namespace Microsoft.AspNet.Builder
/// </summary>
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>
/// Adds the <see cref="RequestLocalizationMiddleware"/> to automatically set culture information for
/// requests based on information provided by the client.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</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>
public static IApplicationBuilder UseRequestLocalization(
this IApplicationBuilder app,
RequestLocalizationOptions options,
RequestCulture defaultRequestCulture)
RequestLocalizationOptions options)
{
if (app == null)
{
Expand All @@ -63,12 +33,7 @@ public static IApplicationBuilder UseRequestLocalization(
throw new ArgumentNullException(nameof(options));
}

if (defaultRequestCulture == null)
{
throw new ArgumentNullException(nameof(defaultRequestCulture));
}

return app.UseMiddleware<RequestLocalizationMiddleware>(options, defaultRequestCulture);
return app.UseMiddleware<RequestLocalizationMiddleware>(options);
}
}
}
19 changes: 4 additions & 15 deletions src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.Extensions.Globalization;
Expand All @@ -21,20 +20,16 @@ public class RequestLocalizationMiddleware
{
private readonly RequestDelegate _next;
private readonly RequestLocalizationOptions _options;
private readonly RequestCulture _defaultRequestCulture;

/// <summary>
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
/// </summary>
/// <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
/// <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(
RequestDelegate next,
RequestLocalizationOptions options,
RequestCulture defaultRequestCulture)
RequestLocalizationOptions options)
{
if (next == null)
{
Expand All @@ -46,14 +41,8 @@ public RequestLocalizationMiddleware(
throw new ArgumentNullException(nameof(options));
}

if (defaultRequestCulture == null)
{
throw new ArgumentNullException(nameof(defaultRequestCulture));
}

_next = next;
_options = options;
_defaultRequestCulture = defaultRequestCulture;
}

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

var requestCulture = _defaultRequestCulture;
var requestCulture = _options.DefaultRequestCulture;

IRequestCultureProvider winningProvider = null;

Expand Down Expand Up @@ -100,11 +89,11 @@ public async Task Invoke(HttpContext context)
}
if (cultureInfo == null && uiCultureInfo != null)
{
cultureInfo = _defaultRequestCulture.Culture;
cultureInfo = _options.DefaultRequestCulture.Culture;
}
if (cultureInfo != null && uiCultureInfo == null)
{
uiCultureInfo = _defaultRequestCulture.UICulture;
uiCultureInfo = _options.DefaultRequestCulture.UICulture;
}

var result = new RequestCulture(cultureInfo, uiCultureInfo);
Expand Down
44 changes: 34 additions & 10 deletions src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Collections.Generic;
using System.Globalization;

Expand All @@ -11,6 +12,9 @@ namespace Microsoft.AspNet.Localization
/// </summary>
public class RequestLocalizationOptions
{
private RequestCulture _defaultRequestCulture =
new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

/// <summary>
/// Creates a new <see cref="RequestLocalizationOptions"/> with default values.
/// </summary>
Expand All @@ -25,20 +29,40 @@ public RequestLocalizationOptions()
}

/// <summary>
/// The cultures supported by the application. If this value is non-<c>null</c>, the
/// <see cref="RequestLocalizationMiddleware"/> will only set the current request culture to an entry in this
/// list. A value of <c>null</c> means all cultures are supported.
/// Defaults to <c>null</c>.
/// Gets or sets the default culture to use for requests when a supported culture could not be determined by
/// one of the configured <see cref="IRequestCultureProvider"/>s.
/// Defaults to <see cref="CultureInfo.CurrentCulture"/> and <see cref="CultureInfo.CurrentUICulture"/>.
/// </summary>
public RequestCulture DefaultRequestCulture
{
get
{
return _defaultRequestCulture;
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

_defaultRequestCulture = value;
}
}

/// <summary>
/// The 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.CurrentCulture"/>.
/// </summary>
public IList<CultureInfo> SupportedCultures { get; set; }
public IList<CultureInfo> SupportedCultures { get; set; } = new List<CultureInfo> { CultureInfo.CurrentCulture };

/// <summary>
/// The UI cultures supported by the application. If this value is non-<c>null</c>, the
/// <see cref="RequestLocalizationMiddleware"/> will only set the current request culture to an entry in this
/// list. A value of <c>null</c> means all cultures are supported.
/// Defaults to <c>null</c>.
/// 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; }
public IList<CultureInfo> SupportedUICultures { get; set; } = new List<CultureInfo> { CultureInfo.CurrentUICulture };

/// <summary>
/// An ordered list of providers used to determine a request's culture information. The first provider that
Expand Down
3 changes: 2 additions & 1 deletion test/LocalizationWebsite/StartupResourcesAtRootFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void Configure(

var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>()
{
new CultureInfo("fr-FR")
Expand All @@ -41,7 +42,7 @@ public void Configure(
}
};

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

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

Expand Down
3 changes: 2 additions & 1 deletion test/LocalizationWebsite/StartupResourcesInFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void Configure(

var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>()
{
new CultureInfo("fr-FR")
Expand All @@ -41,7 +42,7 @@ public void Configure(
}
};

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ public async void GetFallbackLanguage_ReturnsFirstNonNullCultureFromSupportedCul
{
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA"),
new CultureInfo("en-US")
}
};
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand All @@ -52,13 +53,14 @@ public async void GetFallbackLanguage_ReturnsFromSupportedCulture_AcceptLanguage
{
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr-FR"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA"),
new CultureInfo("en-US")
}
};
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("fr-FR"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand All @@ -82,13 +84,14 @@ public async void GetFallbackLanguage_ReturnsDefault_AcceptLanguageListDoesnotCo
{
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr-FR"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA"),
new CultureInfo("af-ZA")
}
};
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("fr-FR"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand All @@ -113,6 +116,7 @@ public async void OmitDefaultRequestCultureShouldNotThrowNullReferenceException_
{
var options = new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-YE")
Expand All @@ -122,7 +126,7 @@ public async void OmitDefaultRequestCultureShouldNotThrowNullReferenceException_
new CultureInfo("ar-YE")
}
};
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public async void GetCultureInfoFromPersistentCookie()
{
var options = new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA")
Expand All @@ -34,7 +35,7 @@ public async void GetCultureInfoFromPersistentCookie()
var provider = new CookieRequestCultureProvider();
provider.CookieName = "Preferences";
options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand All @@ -61,6 +62,7 @@ public async void GetDefaultCultureInfoIfCultureKeysAreMissingOrInvalid()
{
var options = new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA")
Expand All @@ -73,7 +75,7 @@ public async void GetDefaultCultureInfoIfCultureKeysAreMissingOrInvalid()
var provider = new CookieRequestCultureProvider();
provider.CookieName = "Preferences";
options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand All @@ -96,6 +98,7 @@ public async void GetDefaultCultureInfoIfCookieDoesNotExist()
{
var options = new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA")
Expand All @@ -108,7 +111,7 @@ public async void GetDefaultCultureInfoIfCookieDoesNotExist()
var provider = new CookieRequestCultureProvider();
provider.CookieName = "Preferences";
options.RequestCultureProviders.Insert(0, provider);
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async void CustomRequestCultureProviderThatGetsCultureInfoFromUrl()
{
var options = new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar")
Expand All @@ -39,7 +40,7 @@ public async void CustomRequestCultureProviderThatGetsCultureInfoFromUrl()
var requestCulture = new ProviderCultureResult(culture);
return Task.FromResult(requestCulture);
}));
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
app.UseRequestLocalization(options);
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
Expand Down
Loading