-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from Lombiq/issue/TDEAL-4
TDEAL-4: CookieCultureScopeMiddleware and GetUrlHelper
- Loading branch information
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Lombiq.HelpfulLibraries.OrchardCore/Middlewares/CookieCultureScopeMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Localization; | ||
using System.Globalization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lombiq.HelpfulLibraries.OrchardCore.Middlewares; | ||
|
||
/// <summary> | ||
/// A middleware that looks for the "culture" query string argument or cookie and uses it to initialize a <see | ||
/// cref="CultureScope"/>. | ||
/// </summary> | ||
public class CookieCultureScopeMiddleware | ||
{ | ||
public const string CultureKeyName = "culture"; | ||
|
||
private readonly RequestDelegate _next; | ||
|
||
public CookieCultureScopeMiddleware(RequestDelegate next) => _next = next; | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (!TryGetCultureInfo(context, out var culture)) | ||
{ | ||
culture = await context | ||
.RequestServices | ||
.GetRequiredService<ILocalizationService>() | ||
.GetDefaultCultureAsync() ?? "en-US"; | ||
} | ||
|
||
using var scope = CultureScope.Create(culture, culture, ignoreSystemSettings: true); | ||
context.SetCookieForever(CultureKeyName, culture); | ||
|
||
await _next(context); | ||
} | ||
|
||
private static bool TryGetCultureInfo(HttpContext context, out string culture) | ||
{ | ||
if (context.Request.Query.TryGetValue(CultureKeyName, out var queryCulture)) | ||
{ | ||
culture = queryCulture[0]; | ||
} | ||
else | ||
{ | ||
context.Request.Cookies.TryGetValue(CultureKeyName, out culture); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(culture)) return false; | ||
|
||
try | ||
{ | ||
culture = new CultureInfo(culture).Name; | ||
return true; | ||
} | ||
catch | ||
{ | ||
culture = null; | ||
return false; | ||
} | ||
} | ||
} |