remove Microsoft.AspNetCore.Localization
This commit is contained in:
67
Oqtane.Shared/Models/RequestCulture.cs
Normal file
67
Oqtane.Shared/Models/RequestCulture.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System.Globalization;
|
||||
using System;
|
||||
|
||||
namespace Oqtane.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Culture information describing a Culture
|
||||
/// </summary>
|
||||
public class RequestCulture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RequestCulture"/> object with its <see cref="Culture"/> and <see cref="UICulture"/>
|
||||
/// properties set to the same <see cref="CultureInfo"/> value.
|
||||
/// </summary>
|
||||
/// <param name="culture">The <see cref="CultureInfo"/> for the request.</param>
|
||||
public RequestCulture(CultureInfo culture)
|
||||
: this(culture, culture)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RequestCulture"/> object with its <see cref="Culture"/> and <see cref="UICulture"/>
|
||||
/// properties set to the same <see cref="CultureInfo"/> value.
|
||||
/// </summary>
|
||||
/// <param name="culture">The culture for the request.</param>
|
||||
public RequestCulture(string culture)
|
||||
: this(culture, culture)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RequestCulture"/> object with its <see cref="Culture"/> and <see cref="UICulture"/>
|
||||
/// properties set to the respective <see cref="CultureInfo"/> values provided.
|
||||
/// </summary>
|
||||
/// <param name="culture">The culture for the request to be used for formatting.</param>
|
||||
/// <param name="uiCulture">The culture for the request to be used for text, i.e. language.</param>
|
||||
public RequestCulture(string culture, string uiCulture)
|
||||
: this(new CultureInfo(culture), new CultureInfo(uiCulture))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RequestCulture"/> object with its <see cref="Culture"/> and <see cref="UICulture"/>
|
||||
/// properties set to the respective <see cref="CultureInfo"/> values provided.
|
||||
/// </summary>
|
||||
/// <param name="culture">The <see cref="CultureInfo"/> for the request to be used for formatting.</param>
|
||||
/// <param name="uiCulture">The <see cref="CultureInfo"/> for the request to be used for text, i.e. language.</param>
|
||||
public RequestCulture(CultureInfo culture, CultureInfo uiCulture)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(culture);
|
||||
ArgumentNullException.ThrowIfNull(uiCulture);
|
||||
|
||||
Culture = culture;
|
||||
UICulture = uiCulture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="CultureInfo"/> for the request to be used for formatting.
|
||||
/// </summary>
|
||||
public CultureInfo Culture { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="CultureInfo"/> for the request to be used for text, i.e. language;
|
||||
/// </summary>
|
||||
public CultureInfo UICulture { get; }
|
||||
}
|
||||
}
|
89
Oqtane.Shared/Shared/CookieRequestCultureProvider.cs
Normal file
89
Oqtane.Shared/Shared/CookieRequestCultureProvider.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Oqtane.Models;
|
||||
|
||||
namespace Oqtane.Shared
|
||||
{
|
||||
public class CookieRequestCultureProvider
|
||||
{
|
||||
private const char _cookieSeparator = '|';
|
||||
private const string _culturePrefix = "c=";
|
||||
private const string _uiCulturePrefix = "uic=";
|
||||
|
||||
/// <summary>
|
||||
/// Represent the default cookie name used to track the user's preferred culture information, which is ".AspNetCore.Culture".
|
||||
/// </summary>
|
||||
public static readonly string DefaultCookieName = ".AspNetCore.Culture";
|
||||
|
||||
/// <summary>
|
||||
/// The name of the cookie that contains the user's preferred culture information.
|
||||
/// Defaults to <see cref="DefaultCookieName"/>.
|
||||
/// </summary>
|
||||
public string CookieName { get; set; } = DefaultCookieName;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a string representation of a <see cref="RequestCulture"/> for placement in a cookie.
|
||||
/// </summary>
|
||||
/// <param name="requestCulture">The <see cref="RequestCulture"/>.</param>
|
||||
/// <returns>The cookie value.</returns>
|
||||
public static string MakeCookieValue(RequestCulture requestCulture)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(requestCulture);
|
||||
|
||||
return string.Join(_cookieSeparator,
|
||||
$"{_culturePrefix}{requestCulture.Culture.Name}",
|
||||
$"{_uiCulturePrefix}{requestCulture.UICulture.Name}");
|
||||
}
|
||||
|
||||
/// <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 RequestCulture ParseCookieValue(string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Span<Range> parts = stackalloc Range[3];
|
||||
var valueSpan = value.AsSpan();
|
||||
if (valueSpan.Split(parts, _cookieSeparator, StringSplitOptions.RemoveEmptyEntries) != 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var potentialCultureName = valueSpan[parts[0]];
|
||||
var potentialUICultureName = valueSpan[parts[1]];
|
||||
|
||||
if (!potentialCultureName.StartsWith(_culturePrefix, StringComparison.Ordinal) || !
|
||||
potentialUICultureName.StartsWith(_uiCulturePrefix, StringComparison.Ordinal))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var cultureName = potentialCultureName.Slice(_culturePrefix.Length);
|
||||
var uiCultureName = potentialUICultureName.Slice(_uiCulturePrefix.Length);
|
||||
|
||||
if (cultureName.IsEmpty && uiCultureName.IsEmpty)
|
||||
{
|
||||
// No values specified for either so no match
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!cultureName.IsEmpty && uiCultureName.IsEmpty)
|
||||
{
|
||||
// Value for culture but not for UI culture so default to culture value for both
|
||||
uiCultureName = cultureName;
|
||||
}
|
||||
else if (cultureName.IsEmpty && !uiCultureName.IsEmpty)
|
||||
{
|
||||
// Value for UI culture but not for culture so default to UI culture value for both
|
||||
cultureName = uiCultureName;
|
||||
}
|
||||
|
||||
return new RequestCulture(cultureName.ToString(), uiCultureName.ToString());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user