Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
52 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
69 changes: 69 additions & 0 deletions
69
src/Abp.AspNetCore/AspNetCore/Localization/AbpLocalizationHeaderRequestCultureProvider.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,69 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Localization; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
|
||
namespace Abp.AspNetCore.Localization | ||
{ | ||
public class AbpLocalizationHeaderRequestCultureProvider : RequestCultureProvider | ||
{ | ||
private static readonly char[] _separator = new[] { '|' }; | ||
|
||
private static readonly string _culturePrefix = "c="; | ||
private static readonly string _uiCulturePrefix = "uic="; | ||
|
||
/// <inheritdoc /> | ||
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) | ||
{ | ||
if (httpContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(httpContext)); | ||
} | ||
|
||
var localizationHeader = httpContext.Request.Headers[CookieRequestCultureProvider.DefaultCookieName]; | ||
|
||
if (localizationHeader.Count == 0) | ||
{ | ||
return TaskCache<ProviderCultureResult>.DefaultCompletedTask; | ||
} | ||
|
||
var providerResultCulture = ParseHeaderValue(localizationHeader); | ||
return Task.FromResult(providerResultCulture); | ||
} | ||
|
||
/// <summary> | ||
/// Parses a <see cref="RequestCulture"/> from the specified cookie value. | ||
/// Returns <c>null</c> if parsing fails. | ||
/// </summary> | ||
/// <param name="value">The cookie value to parse.</param> | ||
/// <returns>The <see cref="RequestCulture"/> or <c>null</c> if parsing fails.</returns> | ||
public static ProviderCultureResult ParseHeaderValue(string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
{ | ||
return null; | ||
} | ||
|
||
var parts = value.Split(_separator, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
if (parts.Length != 2) | ||
{ | ||
return null; | ||
} | ||
|
||
var potentialCultureName = parts[0]; | ||
var potentialUiCultureName = parts[1]; | ||
|
||
if (!potentialCultureName.StartsWith(_culturePrefix) || !potentialUiCultureName.StartsWith(_uiCulturePrefix)) | ||
{ | ||
return null; | ||
} | ||
|
||
var cultureName = potentialCultureName.Substring(_culturePrefix.Length); | ||
var uiCultureName = potentialUiCultureName.Substring(_uiCulturePrefix.Length); | ||
|
||
return new ProviderCultureResult(cultureName, uiCultureName); | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
src/Abp.AspNetCore/AspNetCore/Localization/SettingRequestCultureProvider.cs
This file was deleted.
Oops, something went wrong.
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