Skip to content

Commit

Permalink
Adjusted per PR recommendations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matasx committed Dec 9, 2024
1 parent c02587d commit 07d312a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Modules/GenHTTP.Modules.I18n/GenHTTP.Modules.I18n.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://genhttp.org/</PackageProjectUrl>

<Description>Allows to handle requests in a functional manner.</Description>
<PackageTags>HTTP Webserver C# Module Functional Inline Request Response</PackageTags>
<Description>Allows to handle requests in a culture specific manner.</Description>
<PackageTags>HTTP Webserver C# Module i18n</PackageTags>

<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
1 change: 1 addition & 0 deletions Modules/GenHTTP.Modules.I18n/Localization.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using GenHTTP.Modules.I18n.Provider;

namespace GenHTTP.Modules.I18n;

Expand Down
3 changes: 1 addition & 2 deletions Modules/GenHTTP.Modules.I18n/Parsers/CultureInfoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public static CultureInfo[] ParseFromLanguage(string? language)
public static CultureInfo[] Parse(IEnumerable<string> languages)
=> languages
.Select(Parse)
.Where(culture => culture != null)
.Select(culture => culture!)
.OfType<CultureInfo>()
.ToArray();

private static CultureInfo? Parse(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using GenHTTP.Api.Protocol;
using System.Globalization;

namespace GenHTTP.Modules.I18n;
namespace GenHTTP.Modules.I18n.Provider;

public sealed class LocalizationConcern : IConcern
{
Expand All @@ -11,9 +11,9 @@ public sealed class LocalizationConcern : IConcern
public IHandler Content { get; }

private readonly CultureInfo _defaultCulture;
private readonly CultureSelector_Delegate _cultureSelector;
private readonly CultureFilter_Delegate _cultureFilter;
private readonly CultureSetter_Delegate _cultureSetter;
private readonly CultureSelectorDelegate _cultureSelector;
private readonly CultureFilterDelegate _cultureFilter;
private readonly CultureSetterDelegate _cultureSetter;

#endregion

Expand All @@ -22,15 +22,15 @@ public sealed class LocalizationConcern : IConcern
public LocalizationConcern(
IHandler content,
CultureInfo defaultCulture,
CultureSelector_Delegate cultureSelector,
CultureFilter_Delegate cultureFilter,
CultureSetter_Delegate cultureSetter
CultureSelectorDelegate cultureSelector,
CultureFilterDelegate cultureFilter,
CultureSetterDelegate cultureSetter
)
{
Content = content;

_defaultCulture = defaultCulture;
_cultureSelector = cultureSelector;
_cultureSelector = cultureSelector;
_cultureFilter = cultureFilter;

_cultureSetter = cultureSetter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using GenHTTP.Modules.I18n.Parsers;
using System.Globalization;

namespace GenHTTP.Modules.I18n;
namespace GenHTTP.Modules.I18n.Provider;

/// <summary>
/// Builder class to configure and create an instance of <see cref="LocalizationConcern"/>.
Expand All @@ -14,45 +14,45 @@ public sealed class LocalizationConcernBuilder : IConcernBuilder

private CultureInfo _defaultCulture = CultureInfo.CurrentCulture;

private readonly List<CultureSelector_Delegate> _cultureSelectors = [];
private CultureFilter_Delegate _cultureFilter = (_, _) => true;
private readonly List<CultureSetter_Delegate> _cultureSetters = [];
private readonly List<CultureSelectorDelegate> _cultureSelectors = [];
private CultureFilterDelegate _cultureFilter = (_, _) => true;
private readonly List<CultureSetterDelegate> _cultureSetters = [];

#endregion

#region Functionality

#region Selectors

public LocalizationConcernBuilder AddFromCookie(string cookieName = "lang")
=> AddFromLanguage(request =>
public LocalizationConcernBuilder FromCookie(string cookieName = "lang")
=> FromLanguage(request =>
{
request.Cookies.TryGetValue(cookieName, out var languageCookie);
return languageCookie.Value;
});

public LocalizationConcernBuilder AddFromQuery(string queryName = "lang")
=> AddFromLanguage(request =>
public LocalizationConcernBuilder FromQuery(string queryName = "lang")
=> FromLanguage(request =>
{
request.Query.TryGetValue(queryName, out var language);
return language;
});

public LocalizationConcernBuilder AddFromHeader(string headerName = "Accept-Language")
=> AddFromLanguage(request =>
public LocalizationConcernBuilder FromHeader(string headerName = "Accept-Language")
=> FromLanguage(request =>
{
request.Headers.TryGetValue(headerName, out var language);
return language;
});

public LocalizationConcernBuilder AddFromLanguage(Func<IRequest, string?> languageSelector)
=> AddFromRequest(request =>
public LocalizationConcernBuilder FromLanguage(Func<IRequest, string?> languageSelector)
=> FromRequest(request =>
{
var language = languageSelector(request);
return CultureInfoParser.ParseFromLanguage(language);
});

public LocalizationConcernBuilder AddFromRequest(CultureSelector_Delegate cultureSelector)
public LocalizationConcernBuilder FromRequest(CultureSelectorDelegate cultureSelector)
{
_cultureSelectors.Add(cultureSelector);
return this;
Expand All @@ -71,7 +71,7 @@ public LocalizationConcernBuilder Supports(params CultureInfo[] supportedCulture
public LocalizationConcernBuilder Supports(Predicate<CultureInfo> culturePredicate)
=> Supports((_, culture) => culturePredicate(culture));

public LocalizationConcernBuilder Supports(CultureFilter_Delegate cultureFilter)
public LocalizationConcernBuilder Supports(CultureFilterDelegate cultureFilter)
{
_cultureFilter = cultureFilter;
return this;
Expand All @@ -81,24 +81,24 @@ public LocalizationConcernBuilder Supports(CultureFilter_Delegate cultureFilter)

#region Setters

public LocalizationConcernBuilder AddSet(bool currentCulture = false, bool currentUICulture = true)
public LocalizationConcernBuilder Setter(bool currentCulture = false, bool currentUICulture = true)
{
//Note: this is a minor optimization so that the flags are not evaluated for each request
if (currentCulture)
{
AddSet(culture => CultureInfo.CurrentCulture = culture);
Setter(culture => CultureInfo.CurrentCulture = culture);
}
if (currentUICulture)
{
AddSet(culture => CultureInfo.CurrentUICulture = culture);
Setter(culture => CultureInfo.CurrentUICulture = culture);
}
return this;
}

public LocalizationConcernBuilder AddSet(Action<CultureInfo> cultureSetter)
=> AddSet((_, culture) => cultureSetter(culture));
public LocalizationConcernBuilder Setter(Action<CultureInfo> cultureSetter)
=> Setter((_, culture) => cultureSetter(culture));

public LocalizationConcernBuilder AddSet(CultureSetter_Delegate cultureSetter)
public LocalizationConcernBuilder Setter(CultureSetterDelegate cultureSetter)
{
_cultureSetters.Add(cultureSetter);
return this;
Expand All @@ -120,19 +120,19 @@ public IConcern Build(IHandler content)
{
if (_cultureSelectors.Count == 0)
{
AddFromHeader();
FromHeader();
}

if (_cultureSetters.Count == 0)
{
AddSet();
Setter();
}

return new LocalizationConcern(
content,
content,
_defaultCulture,
CultureSelector,
_cultureFilter,
_cultureFilter,
CultureSetter
);
}
Expand All @@ -142,7 +142,7 @@ public IConcern Build(IHandler content)
#region Composite functions

private IEnumerable<CultureInfo> CultureSelector(IRequest request)
=> _cultureSelectors.SelectMany(selector => selector(request));
=> _cultureSelectors.SelectMany(selector => selector(request));

private void CultureSetter(IRequest request, CultureInfo cultureInfo)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using GenHTTP.Api.Protocol;
using System.Globalization;

namespace GenHTTP.Modules.I18n;
namespace GenHTTP.Modules.I18n.Provider;

/// <summary>
/// Delegate to select the cultures for a given request.
/// Delegate to extract the cultures for a given request.
/// </summary>
/// <param name="request">The request to select cultures for.</param>
/// <returns>An enumerable of CultureInfo objects representing the selected cultures.</returns>
public delegate IEnumerable<CultureInfo> CultureSelector_Delegate(IRequest request);
/// <param name="request">The request to extract cultures for.</param>
/// <returns>An enumerable of CultureInfo objects representing the extracted cultures.</returns>
public delegate IEnumerable<CultureInfo> CultureSelectorDelegate(IRequest request);

/// <summary>
/// Delegate to set the culture for a given request.
/// </summary>
/// <param name="request">The request to set the culture for.</param>
/// <param name="cultureInfo">The CultureInfo object representing the culture to be set.</param>
public delegate void CultureSetter_Delegate(IRequest request, CultureInfo cultureInfo);
public delegate void CultureSetterDelegate(IRequest request, CultureInfo cultureInfo);

/// <summary>
/// Delegate to filter the cultures for a given request.
/// </summary>
/// <param name="request">The request to filter cultures for.</param>
/// <param name="cultureInfo">The CultureInfo object representing the culture to be filtered.</param>
/// <returns>True if the culture is valid for the request, otherwise false.</returns>
public delegate bool CultureFilter_Delegate(IRequest request, CultureInfo cultureInfo);
public delegate bool CultureFilterDelegate(IRequest request, CultureInfo cultureInfo);

0 comments on commit 07d312a

Please sign in to comment.