@using Blazored.LocalStorage @using Microsoft.Extensions.Options @using TaikoWebUI.Settings @inject NavigationManager NavigationManager @inject IJSRuntime JsRuntime @inject IOptions Settings @inject ILocalStorageService LocalStorage @Localizer["UI"] @foreach (var culture in supportedCultures) { @culture.Value } @Localizer["Song List"] @foreach (var culture in supportedCultures) { @culture.Value } @code { private Dictionary supportedCultures = new(); private Dictionary> cultureStatuses = new(); protected override void OnInitialized() { base.OnInitialized(); foreach (var language in Settings.Value.SupportedLanguages) { supportedCultures.Add(new CultureInfo(language.CultureCode), language.DisplayName); } if (supportedCultures.Count == 0) { supportedCultures.Add(new CultureInfo("en-US"), "English"); } } protected override async Task OnInitializedAsync() { var currentSongNameLanguage = await LocalStorage.GetItemAsync("songNameLanguage"); if (string.IsNullOrEmpty(currentSongNameLanguage)) { await LocalStorage.SetItemAsync("songNameLanguage", "en-US"); } foreach (var culture in supportedCultures.Keys) { var uiStatus = GetActiveUiStatus(culture); var songNameStatus = await GetActiveSongNameStatus(culture); if (!cultureStatuses.ContainsKey("UI")) { cultureStatuses["UI"] = new Dictionary(); } if (!cultureStatuses.ContainsKey("SongName")) { cultureStatuses["SongName"] = new Dictionary(); } cultureStatuses["UI"][culture] = uiStatus; cultureStatuses["SongName"][culture] = songNameStatus; } } private void RequestUiCultureChange(CultureInfo newCulture) { if (Equals(CultureInfo.CurrentCulture, newCulture)) { return; } var js = (IJSInProcessRuntime)JsRuntime; js.InvokeVoid("blazorCulture.set", newCulture.Name); NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); } private async Task RequestSongNameCultureChange(CultureInfo newCulture) { var currentSongNameLanguage = await LocalStorage.GetItemAsync("songNameLanguage"); if (currentSongNameLanguage == newCulture.Name) { return; } await LocalStorage.SetItemAsync("songNameLanguage", newCulture.Name); NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); } private async Task GetActiveStatusAsync(string type, CultureInfo culture) { switch (type) { case "UI": return GetActiveUiStatus(culture); case "SongName": return await GetActiveSongNameStatus(culture); default: return string.Empty; } } private string GetActiveUiStatus(CultureInfo culture) { var currentUiLanguage = CultureInfo.CurrentCulture; return Equals(currentUiLanguage, culture) ? Icons.Material.Filled.Check : string.Empty; } private async Task GetActiveSongNameStatus(CultureInfo culture) { var currentSongNameLanguage = await LocalStorage.GetItemAsync("songNameLanguage"); return currentSongNameLanguage == culture.Name ? Icons.Material.Filled.Check : string.Empty; } }