Skip to content

Commit

Permalink
Add: Add language setting cache
Browse files Browse the repository at this point in the history
  • Loading branch information
whats2000 committed Aug 21, 2024
1 parent c49a616 commit 57b5bb3
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Pages/Index.razor.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
using PearlCalculatorBlazor.Localizer;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using PearlCalculatorBlazor.Localizer;

namespace PearlCalculatorBlazor.Pages
{
public partial class Index
{
[Inject] IJSRuntime JSRuntime { get; set; }

private async void OnClickChangeLanguage(string language)
{
// Load the selected language
await TransText.LoadLanguageAsync(language);

// Store the selected language in localStorage
await JSRuntime.InvokeVoidAsync("localStorage.setItem", "userLanguage", language);
}

protected override void OnInitialized()
protected override async void OnInitialized()
{
TranslateText.OnLanguageChange += RefreshPage;

// Check if a language is stored in localStorage
var storedLanguage = await JSRuntime.InvokeAsync<string>("localStorage.getItem", "userLanguage");

if (!string.IsNullOrEmpty(storedLanguage))
{
// Load the stored language
await TransText.LoadLanguageAsync(storedLanguage);
}
}

public void RefreshPage()
Expand Down

0 comments on commit 57b5bb3

Please sign in to comment.