Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
- Rename `FromLanguage` to `FromRequest`
- Re-order the methods in the concern to follow the lifecycle and visibility
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 9, 2024
1 parent 5584062 commit fa3fbf5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
25 changes: 13 additions & 12 deletions Modules/GenHTTP.Modules.I18n/Provider/LocalizationConcern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,7 @@ AsyncOrSyncSetter[] cultureSetters

#region Functionality

private async ValueTask<CultureInfo?> ResolveCultureInfoAsync(IRequest request)
{
await foreach (var candidate in _cultureSelector(request))
{
if (await _cultureFilter(request, candidate))
{
return candidate;
}
}
return null;
}
public ValueTask PrepareAsync() => Content.PrepareAsync();

public async ValueTask<IResponse?> HandleAsync(IRequest request)
{
Expand All @@ -69,7 +59,18 @@ AsyncOrSyncSetter[] cultureSetters
return await Content.HandleAsync(request);
}

public ValueTask PrepareAsync() => Content.PrepareAsync();
private async ValueTask<CultureInfo?> ResolveCultureInfoAsync(IRequest request)
{
await foreach (var candidate in _cultureSelector(request))
{
if (await _cultureFilter(request, candidate))
{
return candidate;
}
}
return null;
}

#endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public sealed class LocalizationConcernBuilder : IConcernBuilder
/// <param name="cookieName">The name of the cookie to extract the language from.</param>
/// <returns></returns>
public LocalizationConcernBuilder FromCookie(string cookieName = "lang")
=> FromLanguage(request =>
=> FromRequest(request =>
{
request.Cookies.TryGetValue(cookieName, out var languageCookie);
return languageCookie.Value;
Expand All @@ -42,7 +42,7 @@ public LocalizationConcernBuilder FromCookie(string cookieName = "lang")
/// <param name="queryName">The name of the query parameter to extract the language from.</param>
/// <returns></returns>
public LocalizationConcernBuilder FromQuery(string queryName = "lang")
=> FromLanguage(request =>
=> FromRequest(request =>
{
request.Query.TryGetValue(queryName, out var language);
return language;
Expand All @@ -54,7 +54,7 @@ public LocalizationConcernBuilder FromQuery(string queryName = "lang")
/// <param name="headerName">The name of the header to extract the language from.</param>
/// <returns></returns>
public LocalizationConcernBuilder FromHeader(string headerName = "Accept-Language")
=> FromLanguage(request =>
=> FromRequest(request =>
{
request.Headers.TryGetValue(headerName, out var language);
return language;
Expand All @@ -65,15 +65,15 @@ public LocalizationConcernBuilder FromHeader(string headerName = "Accept-Languag
/// </summary>
/// <param name="languageSelector">The selector to extract the language from.</param>
/// <returns></returns>
public LocalizationConcernBuilder FromLanguage(Func<IRequest, string?> languageSelector)
=> FromLanguage(request => ValueTask.FromResult(languageSelector(request)));
public LocalizationConcernBuilder FromRequest(Func<IRequest, string?> languageSelector)
=> FromRequest(request => ValueTask.FromResult(languageSelector(request)));

/// <summary>
/// Extracts the language from a custom async selector.
/// </summary>
/// <param name="languageSelector">The async selector to extract the language from.</param>
/// <returns></returns>
public LocalizationConcernBuilder FromLanguage(Func<IRequest, ValueTask<string?>> languageSelector)
public LocalizationConcernBuilder FromRequest(Func<IRequest, ValueTask<string?>> languageSelector)
=> FromRequest(async request =>
{
var language = await languageSelector(request);
Expand All @@ -85,7 +85,7 @@ public LocalizationConcernBuilder FromLanguage(Func<IRequest, ValueTask<string?>
/// </summary>
/// <param name="cultureSelector">The selector to extract the culture from.</param>
/// <returns></returns>
public LocalizationConcernBuilder FromRequest(CultureSelectorDelegate cultureSelector)
public LocalizationConcernBuilder FromRequest(CultureSelectorDelegate cultureSelector)
=> FromRequest(request => ValueTask.FromResult(cultureSelector(request)));

/// <summary>
Expand Down Expand Up @@ -260,7 +260,7 @@ [.. _cultureSetters]
#region Composite functions

private async IAsyncEnumerable<CultureInfo> CultureSelector(IRequest request)
{
{
foreach (var selector in _cultureSelectors)
{
var cultures = await selector(request);
Expand Down
8 changes: 4 additions & 4 deletions Testing/Acceptance/Modules/I18n/LocalizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public async Task TestSupportsList(TestEngine engine)
{
var localization = Localization
.Create()
.FromLanguage(_ => "en,de,cs,fr")
.FromRequest(_ => "en,de,cs,fr")
.Supports([CultureInfo.CreateSpecificCulture("fr"), CultureInfo.CreateSpecificCulture("cs")]);

await TestLocalization(engine, localization,
Expand All @@ -128,7 +128,7 @@ public async Task TestSupportsPredicate(TestEngine engine)
{
var localization = Localization
.Create()
.FromLanguage(_ => "en,de,cs,fr")
.FromRequest(_ => "en,de,cs,fr")
.Supports(culture => culture.Equals(CultureInfo.CreateSpecificCulture("fr")));

await TestLocalization(engine, localization,
Expand Down Expand Up @@ -159,7 +159,7 @@ public async Task TestSetterCustom(TestEngine engine)
{
var localization = Localization
.Create()
.FromLanguage(_ => "de")
.FromRequest(_ => "de")
.Setter((request, culture) => request.Properties["culture"] = culture);

await TestLocalization(engine, localization, request =>
Expand All @@ -177,7 +177,7 @@ public async Task TestMultipleMixed(TestEngine engine)
.FromQuery()
.FromCookie()
.FromHeader()
.FromLanguage(_ => "de")
.FromRequest(_ => "de")
.Supports([CultureInfo.CreateSpecificCulture("de")])
.Setter(currentCulture: true)
.Setter((request, culture) => request.Properties["culture"] = culture);
Expand Down

0 comments on commit fa3fbf5

Please sign in to comment.