Made IRequestCultureStrategy async to support things like user profile lookup
This commit is contained in:
parent
d22adcbef0
commit
21dc2909a1
|
|
@ -35,6 +35,14 @@ namespace LocalizationSample
|
|||
// new CultureInfo("en-AU")
|
||||
//}
|
||||
};
|
||||
|
||||
// Optionally create an app-specific strategy 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 strategies.
|
||||
//options.RequestCultureStrategies.Insert(0, new CustomRequestCultureStrategy(async context =>
|
||||
//{
|
||||
|
||||
//}));
|
||||
|
||||
app.UseRequestLocalization(options);
|
||||
|
||||
app.Use(async (context, next) =>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Localization.Internal;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
|
@ -22,13 +23,13 @@ namespace Microsoft.AspNet.Localization
|
|||
public int MaximumAcceptLanguageHeaderValuesToTry { get; set; } = 3;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override RequestCulture DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
{
|
||||
var acceptLanguageHeader = httpContext.Request.GetTypedHeaders().AcceptLanguage;
|
||||
|
||||
if (acceptLanguageHeader == null || acceptLanguageHeader.Count == 0)
|
||||
{
|
||||
return null;
|
||||
return Task.FromResult((RequestCulture)null);
|
||||
}
|
||||
|
||||
var languages = acceptLanguageHeader.AsEnumerable();
|
||||
|
|
@ -58,13 +59,13 @@ namespace Microsoft.AspNet.Localization
|
|||
|
||||
if (requestCulture != null)
|
||||
{
|
||||
return requestCulture;
|
||||
return Task.FromResult(requestCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return Task.FromResult((RequestCulture)null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Localization.Internal;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
|
@ -24,20 +25,20 @@ namespace Microsoft.AspNet.Localization
|
|||
public string CookieName { get; set; } = DefaultCookieName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override RequestCulture DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
{
|
||||
var cookie = httpContext.Request.Cookies[CookieName];
|
||||
|
||||
if (cookie == null)
|
||||
{
|
||||
return null;
|
||||
return Task.FromResult((RequestCulture)null);
|
||||
}
|
||||
|
||||
var requestCulture = ParseCookieValue(cookie);
|
||||
|
||||
requestCulture = ValidateRequestCulture(requestCulture);
|
||||
|
||||
return requestCulture;
|
||||
return Task.FromResult(requestCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
|
|
@ -12,19 +13,19 @@ namespace Microsoft.AspNet.Localization
|
|||
/// </summary>
|
||||
public class CustomRequestCultureStrategy : RequestCultureStrategy
|
||||
{
|
||||
private readonly Func<HttpContext, RequestCulture> _strategy;
|
||||
private readonly Func<HttpContext, Task<RequestCulture>> _strategy;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="CustomRequestCultureStrategy"/> using the specified delegate.
|
||||
/// </summary>
|
||||
/// <param name="strategy">The strategy delegate.</param>
|
||||
public CustomRequestCultureStrategy([NotNull] Func<HttpContext, RequestCulture> strategy)
|
||||
public CustomRequestCultureStrategy([NotNull] Func<HttpContext, Task<RequestCulture>> strategy)
|
||||
{
|
||||
_strategy = strategy;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override RequestCulture DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
{
|
||||
return _strategy(httpContext);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
|
||||
namespace Microsoft.AspNet.Localization
|
||||
|
|
@ -18,6 +19,6 @@ namespace Microsoft.AspNet.Localization
|
|||
/// The determined <see cref="RequestCulture"/>.
|
||||
/// Returns <c>null</c> if the strategy couldn't determine a <see cref="RequestCulture"/>.
|
||||
/// </returns>
|
||||
RequestCulture DetermineRequestCulture(HttpContext httpContext);
|
||||
Task<RequestCulture> DetermineRequestCulture(HttpContext httpContext);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Localization.Internal;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
|
@ -26,12 +27,12 @@ namespace Microsoft.AspNet.Localization
|
|||
public string UIQueryStringKey { get; set; } = "ui-culture";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override RequestCulture DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
|
||||
{
|
||||
var request = httpContext.Request;
|
||||
if (!request.QueryString.HasValue)
|
||||
{
|
||||
return null;
|
||||
return Task.FromResult((RequestCulture)null);
|
||||
}
|
||||
|
||||
string queryCulture = null;
|
||||
|
|
@ -50,7 +51,7 @@ namespace Microsoft.AspNet.Localization
|
|||
if (queryCulture == null && queryUICulture == null)
|
||||
{
|
||||
// No values specified for either so no match
|
||||
return null;
|
||||
return Task.FromResult((RequestCulture)null);
|
||||
}
|
||||
|
||||
if (queryCulture != null && queryUICulture == null)
|
||||
|
|
@ -64,14 +65,14 @@ namespace Microsoft.AspNet.Localization
|
|||
|
||||
if (culture == null || uiCulture == null)
|
||||
{
|
||||
return null;
|
||||
return Task.FromResult((RequestCulture)null);
|
||||
}
|
||||
|
||||
var requestCulture = new RequestCulture(culture, uiCulture);
|
||||
|
||||
requestCulture = ValidateRequestCulture(requestCulture);
|
||||
|
||||
return requestCulture;
|
||||
return Task.FromResult(requestCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
|
|
@ -17,7 +18,7 @@ namespace Microsoft.AspNet.Localization
|
|||
public RequestLocalizationOptions Options { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract RequestCulture DetermineRequestCulture(HttpContext httpContext);
|
||||
public abstract Task<RequestCulture> DetermineRequestCulture(HttpContext httpContext);
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the given <see cref="RequestCulture"/> is valid according to the currently configured.
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ namespace Microsoft.AspNet.Localization
|
|||
{
|
||||
foreach (var strategy in _options.RequestCultureStrategies)
|
||||
{
|
||||
var result = strategy.DetermineRequestCulture(context);
|
||||
var result = await strategy.DetermineRequestCulture(context);
|
||||
if (result != null)
|
||||
{
|
||||
requestCulture = result;
|
||||
|
|
|
|||
Loading…
Reference in New Issue