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:
parent
75187b0bd7
commit
0b013ae769
|
|
@ -24,6 +24,8 @@ namespace LocalizationSample
|
|||
{
|
||||
var options = new RequestLocalizationOptions
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
|
||||
// Set options here to change middleware behavior
|
||||
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) =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -63,12 +33,7 @@ namespace Microsoft.AspNet.Builder
|
|||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
if (defaultRequestCulture == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(defaultRequestCulture));
|
||||
}
|
||||
|
||||
return app.UseMiddleware<RequestLocalizationMiddleware>(options, defaultRequestCulture);
|
||||
return app.UseMiddleware<RequestLocalizationMiddleware>(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,6 @@ using System.Collections.Generic;
|
|||
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;
|
||||
|
|
@ -21,7 +20,6 @@ namespace Microsoft.AspNet.Localization
|
|||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly RequestLocalizationOptions _options;
|
||||
private readonly RequestCulture _defaultRequestCulture;
|
||||
|
||||
/// <summary>
|
||||
/// 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="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)
|
||||
{
|
||||
|
|
@ -46,14 +41,8 @@ namespace Microsoft.AspNet.Localization
|
|||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
if (defaultRequestCulture == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(defaultRequestCulture));
|
||||
}
|
||||
|
||||
_next = next;
|
||||
_options = options;
|
||||
_defaultRequestCulture = defaultRequestCulture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -68,7 +57,7 @@ namespace Microsoft.AspNet.Localization
|
|||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var requestCulture = _defaultRequestCulture;
|
||||
var requestCulture = _options.DefaultRequestCulture;
|
||||
|
||||
IRequestCultureProvider winningProvider = null;
|
||||
|
||||
|
|
@ -100,11 +89,11 @@ namespace Microsoft.AspNet.Localization
|
|||
}
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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>
|
||||
|
|
@ -25,20 +29,40 @@ namespace Microsoft.AspNet.Localization
|
|||
}
|
||||
|
||||
/// <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 IList<CultureInfo> SupportedCultures { get; set; }
|
||||
public RequestCulture DefaultRequestCulture
|
||||
{
|
||||
get
|
||||
{
|
||||
return _defaultRequestCulture;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
_defaultRequestCulture = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 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> 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>
|
||||
/// An ordered list of providers used to determine a request's culture information. The first provider that
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ namespace LocalizationWebsite
|
|||
|
||||
var options = new RequestLocalizationOptions
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>()
|
||||
{
|
||||
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ namespace LocalizationWebsite
|
|||
|
||||
var options = new RequestLocalizationOptions
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>()
|
||||
{
|
||||
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -21,13 +21,14 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
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>();
|
||||
|
|
@ -52,13 +53,14 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
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>();
|
||||
|
|
@ -82,13 +84,14 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
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>();
|
||||
|
|
@ -113,6 +116,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-YE")
|
||||
|
|
@ -122,7 +126,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
new CultureInfo("ar-YE")
|
||||
}
|
||||
};
|
||||
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -34,7 +35,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
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>();
|
||||
|
|
@ -61,6 +62,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -73,7 +75,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
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>();
|
||||
|
|
@ -96,6 +98,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -108,7 +111,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
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>();
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar")
|
||||
|
|
@ -39,7 +40,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
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>();
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
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 =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
|
|
@ -52,8 +53,11 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
using (var server = TestServer.Create(app =>
|
||||
{
|
||||
var options = new RequestLocalizationOptions();
|
||||
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
|
||||
var options = new RequestLocalizationOptions
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
};
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
|
|
@ -76,6 +80,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -85,7 +90,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
new CultureInfo("ar-SA")
|
||||
}
|
||||
};
|
||||
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
|
|
@ -107,6 +112,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -116,7 +122,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
new CultureInfo("ar-SA")
|
||||
}
|
||||
};
|
||||
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
|
|
@ -138,6 +144,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -147,7 +154,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
new CultureInfo("ar-SA")
|
||||
}
|
||||
};
|
||||
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
|
|
@ -170,6 +177,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -179,7 +187,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
new CultureInfo("ar-SA")
|
||||
}
|
||||
};
|
||||
app.UseRequestLocalization(options, defaultRequestCulture: new RequestCulture("en-US"));
|
||||
app.UseRequestLocalization(options);
|
||||
app.Run(context =>
|
||||
{
|
||||
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
|
||||
|
|
@ -202,6 +210,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("ar-SA")
|
||||
|
|
@ -215,7 +224,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
provider.QueryStringKey = "c";
|
||||
provider.UIQueryStringKey = "uic";
|
||||
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>();
|
||||
|
|
@ -238,6 +247,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
{
|
||||
var options = new RequestLocalizationOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture("en-US"),
|
||||
SupportedCultures = new List<CultureInfo>
|
||||
{
|
||||
new CultureInfo("FR")
|
||||
|
|
@ -251,7 +261,7 @@ namespace Microsoft.Extensions.Localization.Tests
|
|||
provider.QueryStringKey = "c";
|
||||
provider.UIQueryStringKey = "uic";
|
||||
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>();
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue