Browse Source

feat: Implement route-based culture navigation helper for improved localization handling

pull/25174/head
maliming 6 months ago
parent
commit
555959657a
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 14
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IRouteBasedCultureNavigationHelper.cs
  2. 31
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/RouteBasedCultureNavigationHelper.cs
  3. 17
      modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LanguageSwitch.razor

14
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IRouteBasedCultureNavigationHelper.cs

@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Volo.Abp.Localization;
namespace Volo.Abp.AspNetCore.Components.WebAssembly;
public interface IRouteBasedCultureNavigationHelper
{
Task NavigateToNewCultureAsync(
NavigationManager navigationManager,
LanguageInfo newLanguage,
IEnumerable<LanguageInfo> allLanguages);
}

31
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/RouteBasedCultureNavigationHelper.cs

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization;
namespace Volo.Abp.AspNetCore.Components.WebAssembly;
public class RouteBasedCultureNavigationHelper : IRouteBasedCultureNavigationHelper, ITransientDependency
{
public virtual Task NavigateToNewCultureAsync(
NavigationManager navigationManager,
LanguageInfo newLanguage,
IEnumerable<LanguageInfo> allLanguages)
{
var relativePath = navigationManager.ToBaseRelativePath(navigationManager.Uri);
var slashIndex = relativePath.IndexOf('/');
var firstSegment = slashIndex >= 0 ? relativePath.Substring(0, slashIndex) : relativePath;
var allCultures = allLanguages.Select(l => l.CultureName);
var newRelativePath = allCultures.Any(c => string.Equals(c, firstSegment, StringComparison.OrdinalIgnoreCase))
? newLanguage.CultureName + (slashIndex >= 0 ? relativePath.Substring(slashIndex) : string.Empty)
: newLanguage.CultureName + "/" + relativePath;
navigationManager.NavigateTo(navigationManager.ToAbsoluteUri(newRelativePath).ToString(), forceLoad: true);
return Task.CompletedTask;
}
}

17
modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LanguageSwitch.razor

@ -2,12 +2,14 @@
@using System.Globalization @using System.Globalization
@using System.Collections.Immutable @using System.Collections.Immutable
@using Volo.Abp.AspNetCore.Components.Web @using Volo.Abp.AspNetCore.Components.Web
@using Volo.Abp.AspNetCore.Components.WebAssembly
@using Volo.Abp.AspNetCore.Mvc.Client @using Volo.Abp.AspNetCore.Mvc.Client
@inject ILanguageProvider LanguageProvider @inject ILanguageProvider LanguageProvider
@inject IJSRuntime JsRuntime @inject IJSRuntime JsRuntime
@inject ICookieService CookieService @inject ICookieService CookieService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject ICachedApplicationConfigurationClient ConfigurationClient @inject ICachedApplicationConfigurationClient ConfigurationClient
@inject IRouteBasedCultureNavigationHelper CultureNavigationHelper
@if (_otherLanguages != null && _otherLanguages.Any()) @if (_otherLanguages != null && _otherLanguages.Any())
{ {
<BarDropdown RightAligned="true"> <BarDropdown RightAligned="true">
@ -66,19 +68,8 @@
{ {
if (_useRouteBasedCulture) if (_useRouteBasedCulture)
{ {
var relativePath = NavigationManager.ToBaseRelativePath(NavigationManager.Uri); await CultureNavigationHelper.NavigateToNewCultureAsync(
var slashIndex = relativePath.IndexOf('/'); NavigationManager, language, _otherLanguages.Append(_currentLanguage));
var firstSegment = slashIndex >= 0 ? relativePath[..slashIndex] : relativePath;
var allCultures = _otherLanguages
.Select(l => l.CultureName)
.Append(_currentLanguage.CultureName);
var newRelativePath = allCultures.Any(c => string.Equals(c, firstSegment, StringComparison.OrdinalIgnoreCase))
? language.CultureName + (slashIndex >= 0 ? relativePath[slashIndex..] : string.Empty)
: language.CultureName + "/" + relativePath;
NavigationManager.NavigateTo(NavigationManager.ToAbsoluteUri(newRelativePath).ToString(), forceLoad: true);
return; return;
} }

Loading…
Cancel
Save