Make topnav structure refresh on language change (#1006)

This commit is contained in:
Leendert de Borst
2025-07-15 15:35:49 +02:00
committed by Leendert de Borst
parent 95739f6758
commit a2b962bb44
3 changed files with 38 additions and 0 deletions

View File

@@ -4,6 +4,7 @@
@inject IJSRuntime JSRuntime
@inject CredentialService CredentialService
@inject AliasVault.Client.Services.QuickCreateStateService QuickCreateStateService
@inject LanguageService LanguageService
@implements IAsyncDisposable
<button @ref="buttonRef" @onclick="TogglePopup" id="quickIdentityButton" class="px-4 py-2 text-sm font-medium text-white bg-gradient-to-r from-primary-500 to-primary-600 hover:from-primary-600 hover:to-primary-700 focus:outline-none dark:from-primary-400 dark:to-primary-500 dark:hover:from-primary-500 dark:hover:to-primary-600 rounded-md shadow-sm transition duration-150 ease-in-out transform hover:scale-105 active:scale-95 focus:shadow-outline">
@@ -55,6 +56,7 @@
async ValueTask IAsyncDisposable.DisposeAsync()
{
await KeyboardShortcutService.UnregisterShortcutAsync("gc");
LanguageService.LanguageChanged -= OnLanguageChanged;
if (Module is not null)
{
await Module.DisposeAsync();
@@ -67,10 +69,20 @@
if (firstRender)
{
await KeyboardShortcutService.RegisterShortcutAsync("gc", ShowPopup);
LanguageService.LanguageChanged += OnLanguageChanged;
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./js/modules/newIdentityWidget.js");
}
}
/// <summary>
/// Handles language change events and triggers component refresh.
/// </summary>
/// <param name="languageCode">The new language code.</param>
private void OnLanguageChanged(string languageCode)
{
InvokeAsync(StateHasChanged);
}
/// <summary>
/// When the URL input is focused, place cursor at the end of the default URL to allow for easy typing.
/// </summary>

View File

@@ -3,6 +3,7 @@
@inject KeyboardShortcutService KeyboardShortcutService
@inject JsInteropService JsInteropService
@inject IStringLocalizerFactory LocalizerFactory
@inject LanguageService LanguageService
@implements IAsyncDisposable
@using Microsoft.Extensions.Localization
@using System.Timers
@@ -98,6 +99,7 @@
await KeyboardShortcutService.UnregisterShortcutAsync("gs");
await KeyboardShortcutService.UnregisterShortcutAsync("gf");
NavigationManager.LocationChanged -= ResetSearchField;
LanguageService.LanguageChanged -= OnLanguageChanged;
_searchTimer?.Dispose();
}
@@ -111,6 +113,7 @@
await KeyboardShortcutService.RegisterShortcutAsync("gs", FocusSearchField);
await KeyboardShortcutService.RegisterShortcutAsync("gf", FocusSearchField);
NavigationManager.LocationChanged += ResetSearchField;
LanguageService.LanguageChanged += OnLanguageChanged;
// Initialize search timer
_searchTimer = new Timer(300); // 300ms debounce
@@ -119,6 +122,17 @@
}
}
/// <summary>
/// Handles language change events and triggers component refresh.
/// </summary>
/// <param name="languageCode">The new language code.</param>
private void OnLanguageChanged(string languageCode)
{
// Reset the localizer to force it to use the new language
_localizer = null;
InvokeAsync(StateHasChanged);
}
private void OnFocus()
{
ShowHelpText = true;

View File

@@ -1,6 +1,7 @@
@inherits AliasVault.Client.Main.Pages.MainBase
@using Microsoft.Extensions.Localization
@implements IDisposable
@inject LanguageService LanguageService
<header>
<nav class="fixed z-30 w-full bg-white border-b border-gray-200 dark:bg-gray-800 dark:border-gray-700 py-3 px-4">
@@ -132,6 +133,7 @@
public void Dispose()
{
NavigationManager.LocationChanged -= LocationChanged;
LanguageService.LanguageChanged -= OnLanguageChanged;
}
/// <inheritdoc />
@@ -140,6 +142,7 @@
await base.OnInitializedAsync();
Username = await GetUsernameAsync();
NavigationManager.LocationChanged += LocationChanged;
LanguageService.LanguageChanged += OnLanguageChanged;
}
/// <inheritdoc />
@@ -155,6 +158,15 @@
}
}
/// <summary>
/// Handles language change events and triggers component refresh.
/// </summary>
/// <param name="languageCode">The new language code.</param>
private void OnLanguageChanged(string languageCode)
{
InvokeAsync(StateHasChanged);
}
private void LocationChanged(object? sender, LocationChangedEventArgs e)
{
bool hadChanges = false;