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

Commit

Permalink
Change "Strategy" to "Provider"
Browse files Browse the repository at this point in the history
- #19
  • Loading branch information
DamianEdwards committed May 20, 2015
1 parent 31f36a8 commit 9fc8497
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 45 deletions.
12 changes: 6 additions & 6 deletions samples/LocalizationSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public void Configure(IApplicationBuilder app, IStringLocalizer<Startup> SR)
//}
};

// 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 =>
// Optionally create an app-specific provider 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 providers.
//options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
//{

//}));
Expand Down Expand Up @@ -74,13 +74,13 @@ await context.Response.WriteAsync(
function useCookie() {{
var culture = document.getElementById('culture');
var uiCulture = document.getElementById('uiCulture');
var cookieValue = '{CookieRequestCultureStrategy.DefaultCookieName}=c='+culture.options[culture.selectedIndex].value+'|uic='+uiCulture.options[uiCulture.selectedIndex].value;
var cookieValue = '{CookieRequestCultureProvider.DefaultCookieName}=c='+culture.options[culture.selectedIndex].value+'|uic='+uiCulture.options[uiCulture.selectedIndex].value;
document.cookie = cookieValue;
window.location = window.location.href.split('?')[0];
}}
function clearCookie() {{
document.cookie='{CookieRequestCultureStrategy.DefaultCookieName}=""""';
document.cookie='{CookieRequestCultureProvider.DefaultCookieName}=""""';
}}
</script>
</head>
Expand All @@ -101,7 +101,7 @@ function clearCookie() {{
await context.Response.WriteAsync("</form>");
await context.Response.WriteAsync("<br />");
await context.Response.WriteAsync("<table><tbody>");
await context.Response.WriteAsync($"<tr><th>Winning strategy:</th><td>{requestCultureFeature.Strategy.GetType().Name}</td></tr>");
await context.Response.WriteAsync($"<tr><th>Winning provider:</th><td>{requestCultureFeature.Provider.GetType().Name}</td></tr>");
await context.Response.WriteAsync($"<tr><th>{SR["Current request culture:"]}</th><td>{requestCulture.Culture.DisplayName} ({requestCulture.Culture})</td></tr>");
await context.Response.WriteAsync($"<tr><th>{SR["Current request UI culture:"]}</th><td>{requestCulture.UICulture.DisplayName} ({requestCulture.UICulture})</td></tr>");
await context.Response.WriteAsync($"<tr><th>{SR["Current thread culture:"]}</th><td>{CultureInfo.CurrentCulture.DisplayName} ({CultureInfo.CurrentCulture})</td></tr>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.AspNet.Localization
/// <summary>
/// Determines the culture information for a request via the value of the Accept-Language header.
/// </summary>
public class AcceptLanguageHeaderRequestCultureStrategy : RequestCultureStrategy
public class AcceptLanguageHeaderRequestCultureProvider : RequestCultureProvider
{
/// <summary>
/// The maximum number of values in the Accept-Language header to attempt to create a <see cref="System.Globalization.CultureInfo"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Localization
/// <summary>
/// Determines the culture information for a request via the value of a cookie.
/// </summary>
public class CookieRequestCultureStrategy : RequestCultureStrategy
public class CookieRequestCultureProvider : RequestCultureProvider
{
private static readonly char[] _cookieSeparator = new[] { '|' };
private static readonly string _culturePrefix = "c=";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ namespace Microsoft.AspNet.Localization
/// <summary>
/// Determines the culture information for a request via the configured delegate.
/// </summary>
public class CustomRequestCultureStrategy : RequestCultureStrategy
public class CustomRequestCultureProvider : RequestCultureProvider
{
private readonly Func<HttpContext, Task<RequestCulture>> _strategy;
private readonly Func<HttpContext, Task<RequestCulture>> _provider;

/// <summary>
/// Creates a new <see cref="CustomRequestCultureStrategy"/> using the specified delegate.
/// Creates a new <see cref="CustomRequestCultureProvider"/> using the specified delegate.
/// </summary>
/// <param name="strategy">The strategy delegate.</param>
public CustomRequestCultureStrategy([NotNull] Func<HttpContext, Task<RequestCulture>> strategy)
/// <param name="provider">The provider delegate.</param>
public CustomRequestCultureProvider([NotNull] Func<HttpContext, Task<RequestCulture>> provider)
{
_strategy = strategy;
_provider = provider;
}

/// <inheritdoc />
public override Task<RequestCulture> DetermineRequestCulture([NotNull] HttpContext httpContext)
{
return _strategy(httpContext);
return _provider(httpContext);
}
}
}
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Localization/IRequestCultureFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public interface IRequestCultureFeature
RequestCulture RequestCulture { get; }

/// <summary>
/// The <see cref="IRequestCultureStrategy"/> that determined the request's culture information.
/// If the value is <c>null</c> then no strategy was used and the request's culture was set to the value of
/// The <see cref="IRequestCultureProvider"/> that determined the request's culture information.
/// If the value is <c>null</c> then no provider was used and the request's culture was set to the value of
/// <see cref="RequestLocalizationOptions.DefaultRequestCulture"/>.
/// </summary>
IRequestCultureStrategy Strategy { get; }
IRequestCultureProvider Provider { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
namespace Microsoft.AspNet.Localization
{
/// <summary>
/// Represents a strategy for determining the culture information of an <see cref="HttpRequest"/>.
/// Represents a provider for determining the culture information of an <see cref="HttpRequest"/>.
/// </summary>
public interface IRequestCultureStrategy
public interface IRequestCultureProvider
{
/// <summary>
/// Implements the strategy to determine the culture of the given request.
/// Implements the provider to determine the culture of the given request.
/// </summary>
/// <param name="httpContext">The <see cref="HttpContext"/> for the request.</param>
/// <returns>
/// The determined <see cref="RequestCulture"/>.
/// Returns <c>null</c> if the strategy couldn't determine a <see cref="RequestCulture"/>.
/// Returns <c>null</c> if the provider couldn't determine a <see cref="RequestCulture"/>.
/// </returns>
Task<RequestCulture> DetermineRequestCulture(HttpContext httpContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Localization
/// <summary>
/// Determines the culture information for a request via values in the query string.
/// </summary>
public class QueryStringRequestCultureStrategy : RequestCultureStrategy
public class QueryStringRequestCultureProvider : RequestCultureProvider
{
/// <summary>
/// The key that contains the culture name.
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Localization/RequestCultureFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public class RequestCultureFeature : IRequestCultureFeature
/// Creates a new <see cref="RequestCultureFeature"/> with the specified <see cref="Localization.RequestCulture"/>.
/// </summary>
/// <param name="requestCulture">The <see cref="Localization.RequestCulture"/>.</param>
public RequestCultureFeature([NotNull] RequestCulture requestCulture, IRequestCultureStrategy strategy)
public RequestCultureFeature([NotNull] RequestCulture requestCulture, IRequestCultureProvider provider)
{
RequestCulture = requestCulture;
Strategy = strategy;
Provider = provider;
}

/// <inheritdoc />
public RequestCulture RequestCulture { get; }

/// <inheritdoc />
public IRequestCultureStrategy Strategy { get; }
public IRequestCultureProvider Provider { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;

namespace Microsoft.AspNet.Localization
{
/// <summary>
/// An abstract base class strategy for determining the culture information of an <see cref="HttpRequest"/>.
/// An abstract base class provider for determining the culture information of an <see cref="HttpRequest"/>.
/// </summary>
public abstract class RequestCultureStrategy : IRequestCultureStrategy
public abstract class RequestCultureProvider : IRequestCultureProvider
{
/// <summary>
/// The current options for the <see cref="RequestLocalizationMiddleware"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ public async Task Invoke([NotNull] HttpContext context)
var requestCulture = _options.DefaultRequestCulture ??
new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

IRequestCultureStrategy winningStrategy = null;
IRequestCultureProvider winningProvider = null;

if (_options.RequestCultureStrategies != null)
if (_options.RequestCultureProviders != null)
{
foreach (var strategy in _options.RequestCultureStrategies)
foreach (var provider in _options.RequestCultureProviders)
{
var result = await strategy.DetermineRequestCulture(context);
var result = await provider.DetermineRequestCulture(context);
if (result != null)
{
requestCulture = result;
winningStrategy = strategy;
winningProvider = provider;
break;
}
}
}

context.SetFeature<IRequestCultureFeature>(new RequestCultureFeature(requestCulture, winningStrategy));
context.SetFeature<IRequestCultureFeature>(new RequestCultureFeature(requestCulture, winningProvider));

SetCurrentThreadCulture(requestCulture);

Expand Down
20 changes: 10 additions & 10 deletions src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

RequestCultureStrategies = new List<IRequestCultureStrategy>
RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureStrategy { Options = this },
new CookieRequestCultureStrategy { Options = this },
new AcceptLanguageHeaderRequestCultureStrategy { Options = this }
new QueryStringRequestCultureProvider { Options = this },
new CookieRequestCultureProvider { Options = this },
new AcceptLanguageHeaderRequestCultureProvider { Options = this }
};
}

/// <summary>
/// The default <see cref="RequestCulture"/> to use. This value will be used if none of the configured
/// <see cref="IRequestCultureStrategy"/> options result in a non-<c>null</c> result.
/// <see cref="IRequestCultureProvider"/> options result in a non-<c>null</c> result.
/// Defaults to <see cref="RequestCulture.Culture"/> set to <see cref="CultureInfo.DefaultThreadCurrentCulture"/>
/// and <see cref="RequestCulture.UICulture"/> set to <see cref="CultureInfo.DefaultThreadCurrentUICulture"/>.
/// </summary>
Expand All @@ -54,16 +54,16 @@ public RequestLocalizationOptions()
public IList<CultureInfo> SupportedUICultures { get; set; }

/// <summary>
/// An ordered list of strategies used to determine a request's culture information. The first strategy that
/// An ordered list of providers used to determine a request's culture information. The first provider that
/// returns a non-<c>null</c> result for a given request will be used.
/// Defaults to the following:
/// <list type="number">
/// <item><description><see cref="QueryStringRequestCultureStrategy"/></description></item>
/// <item><description><see cref="CookieRequestCultureStrategy"/></description></item>
/// <item><description><see cref="AcceptLanguageHeaderRequestCultureStrategy"/></description></item>
/// <item><description><see cref="QueryStringRequestCultureProvider"/></description></item>
/// <item><description><see cref="CookieRequestCultureProvider"/></description></item>
/// <item><description><see cref="AcceptLanguageHeaderRequestCultureProvider"/></description></item>
/// </list>
/// </summary>
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "Improves usability")]
public IList<IRequestCultureStrategy> RequestCultureStrategies { get; set; }
public IList<IRequestCultureProvider> RequestCultureProviders { get; set; }
}
}

0 comments on commit 9fc8497

Please sign in to comment.