remove Microsoft.AspNetCore.Localization

This commit is contained in:
sbwalker 2024-10-24 12:24:46 -04:00
parent 6719d242bd
commit a967332f89
9 changed files with 163 additions and 12 deletions

View File

@ -1,7 +1,6 @@
@namespace Oqtane.Modules.Admin.Languages
@inherits ModuleBase
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@inject NavigationManager NavigationManager
@inject ILocalizationService LocalizationService
@inject ILanguageService LanguageService

View File

@ -1,7 +1,6 @@
@namespace Oqtane.Modules.Admin.Languages
@inherits ModuleBase
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@inject NavigationManager NavigationManager
@inject ILocalizationService LocalizationService
@inject ILanguageService LanguageService

View File

@ -1,7 +1,6 @@
@namespace Oqtane.Modules.Admin.ModuleDefinitions
@inherits ModuleBase
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@inject IModuleDefinitionService ModuleDefinitionService
@inject IPackageService PackageService
@inject ILanguageService LanguageService

View File

@ -26,7 +26,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.0-rc.2.24474.3" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="9.0.0-rc.2.24474.3" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0-rc.2.24473.5" />
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
</ItemGroup>
<ItemGroup>

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
@ -13,13 +12,13 @@ using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using Oqtane.Documentation;
using Oqtane.Models;
using Oqtane.Modules;
using Oqtane.Services;
using Oqtane.Shared;
using Oqtane.UI;
namespace Oqtane.Client
@ -258,7 +257,7 @@ namespace Oqtane.Client
var jsRuntime = serviceProvider.GetRequiredService<IJSRuntime>();
var interop = new Interop(jsRuntime);
var localizationCookie = await interop.GetCookie(CookieRequestCultureProvider.DefaultCookieName);
var culture = CookieRequestCultureProvider.ParseCookieValue(localizationCookie)?.UICultures?[0].Value;
var culture = CookieRequestCultureProvider.ParseCookieValue(localizationCookie)?.UICulture.Name;
var localizationService = serviceProvider.GetRequiredService<ILocalizationService>();
var cultures = await localizationService.GetCulturesAsync(false);

View File

@ -1,7 +1,6 @@
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Http
@using Oqtane.Models
@using Microsoft.AspNetCore.Http
@namespace Oqtane.Themes.Controls
@inherits ThemeControlBase
@inject ILanguageService LanguageService

View File

@ -196,7 +196,7 @@
_bodyResources += ParseScripts(site.BodyContent);
// set culture if not specified
string culture = Context.Request.Cookies[CookieRequestCultureProvider.DefaultCookieName];
string culture = Context.Request.Cookies[Shared.CookieRequestCultureProvider.DefaultCookieName];
if (culture == null)
{
// get default language for site
@ -613,8 +613,8 @@
};
Context.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
Shared.CookieRequestCultureProvider.DefaultCookieName,
Shared.CookieRequestCultureProvider.MakeCookieValue(new Models.RequestCulture(culture)),
cookieOptions
);
}

View 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; }
}
}

View 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());
}
}
}