Updated the `Navigation.NavigateTo` method in `LanguageControls.razor` to set the `forceLoad` parameter to `true`. This ensures that the application performs a full page reload when the language is changed, improving consistency in language updates.
94 lines
2.9 KiB
Plaintext
94 lines
2.9 KiB
Plaintext
@using EnvelopeGenerator.Server.Client.Services
|
|
@using EnvelopeGenerator.Application.Resources
|
|
@using Microsoft.Extensions.Localization
|
|
@rendermode InteractiveWebAssembly
|
|
|
|
@inject LocalizationService LocalizationService
|
|
@inject NavigationManager Navigation
|
|
@inject IStringLocalizer<Resource> UiLocalizer
|
|
|
|
<div class="sf-lang-combo">
|
|
<button type="button"
|
|
class="sf-lang-trigger"
|
|
@onclick="ToggleMenu"
|
|
aria-label="@L("Language", "Language")"
|
|
aria-expanded="@_isOpen">
|
|
<span class="fi @_selected.FlagClass"></span>
|
|
</button>
|
|
|
|
@if (_isOpen)
|
|
{
|
|
<button type="button"
|
|
class="sf-lang-backdrop"
|
|
aria-label="@L("CloseLanguageMenu", "Close language menu")"
|
|
@onclick="CloseMenu">
|
|
</button>
|
|
|
|
<div class="sf-lang-menu" role="listbox" aria-label="@L("Languages", "Languages")">
|
|
@foreach (var option in _options)
|
|
{
|
|
<button type="button"
|
|
class="sf-lang-option @(_selected.Code == option.Code ? "sf-lang-option--active" : string.Empty)"
|
|
role="option"
|
|
aria-selected="@(_selected.Code == option.Code)"
|
|
@onmousedown:preventDefault="true"
|
|
@onclick="() => SelectLanguageAsync(option)">
|
|
<span class="fi @option.FlagClass"></span>
|
|
<span>@option.ShortName</span>
|
|
</button>
|
|
}
|
|
</div>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private readonly IReadOnlyList<LanguageOption> _options =
|
|
[
|
|
new("de-DE", "fi-de", "DE"),
|
|
new("en-US", "fi-us", "EN"),
|
|
new("fr-FR", "fi-fr", "FR")
|
|
];
|
|
|
|
private LanguageOption _selected = new("de-DE", "fi-de", "DE");
|
|
private bool _isOpen;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var currentLanguage = await LocalizationService.GetLanguageAsync();
|
|
_selected = _options.FirstOrDefault(x => x.Code.Equals(currentLanguage, StringComparison.OrdinalIgnoreCase))
|
|
?? _options[0];
|
|
}
|
|
|
|
private void ToggleMenu()
|
|
{
|
|
_isOpen = !_isOpen;
|
|
}
|
|
|
|
private void CloseMenu()
|
|
{
|
|
_isOpen = false;
|
|
}
|
|
|
|
private async Task SelectLanguageAsync(LanguageOption option)
|
|
{
|
|
if (_selected.Code.Equals(option.Code, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
_isOpen = false;
|
|
return;
|
|
}
|
|
|
|
_selected = option;
|
|
_isOpen = false;
|
|
await LocalizationService.SetLanguageAsync(option.Code);
|
|
Navigation.NavigateTo(Navigation.Uri, forceLoad: true);
|
|
}
|
|
|
|
private sealed record LanguageOption(string Code, string FlagClass, string ShortName);
|
|
|
|
string L(string key, string fallback)
|
|
{
|
|
var value = UiLocalizer[key];
|
|
return value.ResourceNotFound ? fallback : value.Value;
|
|
}
|
|
}
|