diff --git a/Masa.Blazor.Pro/Global/Config/GlobalConfig.cs b/Masa.Blazor.Pro/Global/Config/GlobalConfig.cs index 1262227..45cb967 100644 --- a/Masa.Blazor.Pro/Global/Config/GlobalConfig.cs +++ b/Masa.Blazor.Pro/Global/Config/GlobalConfig.cs @@ -1,27 +1,32 @@ -namespace Masa.Blazor.Pro.Global; +using System.Globalization; + +namespace Masa.Blazor.Pro.Global; public class GlobalConfig { - private readonly CookieStorage? _cookieStorage; + private readonly I18n _i18n; + private readonly CookieStorage _cookieStorage; private string? _pageMode; private bool _expandOnHover; private string? _favorite; private string? _navigationStyle; - public GlobalConfig(CookieStorage cookieStorage, IHttpContextAccessor httpContextAccessor) + public GlobalConfig(CookieStorage cookieStorage, I18n i18n) { + _i18n = i18n; _cookieStorage = cookieStorage; - if (httpContextAccessor.HttpContext is not null) Initialization(httpContextAccessor.HttpContext.Request.Cookies); } - public static string PageModeKey { get; set; } = "GlobalConfig_PageMode"; + public static string PageModeKey => "GlobalConfig_PageMode"; + + public static string NavigationStyleKey => "GlobalConfig_NavigationStyle"; - public static string NavigationStyleKey { get; set; } = "GlobalConfig_NavigationStyle"; + public static string ExpandOnHoverCookieKey => "GlobalConfig_ExpandOnHover"; - public static string ExpandOnHoverCookieKey { get; set; } = "GlobalConfig_ExpandOnHover"; + public static string FavoriteCookieKey => "GlobalConfig_Favorite"; - public static string FavoriteCookieKey { get; set; } = "GlobalConfig_Favorite"; + public static string LangCookieKey => "GlobalConfig_Lang"; public EventHandler? NavigationStyleChanged { get; set; } @@ -31,7 +36,7 @@ public string PageMode set { _pageMode = value; - _cookieStorage?.SetItemAsync(PageModeKey, value); + _cookieStorage.SetAsync(PageModeKey, value); } } @@ -42,7 +47,7 @@ public string NavigationStyle { _navigationStyle = value; NavigationStyleChanged?.Invoke(this, EventArgs.Empty); - _cookieStorage?.SetItemAsync(NavigationStyleKey, value); + _cookieStorage.SetAsync(NavigationStyleKey, value); } } @@ -52,7 +57,7 @@ public bool ExpandOnHover set { _expandOnHover = value; - _cookieStorage?.SetItemAsync(ExpandOnHoverCookieKey, value); + _cookieStorage.SetAsync(ExpandOnHoverCookieKey, value); } } @@ -62,15 +67,31 @@ public string? Favorite set { _favorite = value; - _cookieStorage?.SetItemAsync(FavoriteCookieKey, value); + _cookieStorage.SetAsync(FavoriteCookieKey, value); } } - public void Initialization(IRequestCookieCollection cookies) + public CultureInfo Culture { - _pageMode = cookies[PageModeKey]; - _navigationStyle = cookies[NavigationStyleKey]; - _expandOnHover = Convert.ToBoolean(cookies[ExpandOnHoverCookieKey]); - _favorite = cookies[FavoriteCookieKey]; + get => _i18n.Culture; + set + { + _cookieStorage.SetAsync(LangCookieKey, value.Name); + _i18n.SetCulture(value); + } + } + + public async Task InitFromStorage() + { + _pageMode = await _cookieStorage.GetAsync(PageModeKey); + _navigationStyle = await _cookieStorage.GetAsync(NavigationStyleKey); + _expandOnHover = Convert.ToBoolean(await _cookieStorage.GetAsync(ExpandOnHoverCookieKey)); + _favorite = await _cookieStorage.GetAsync(FavoriteCookieKey); + + var lang = await _cookieStorage.GetAsync(LangCookieKey); + if (!string.IsNullOrWhiteSpace(lang)) + { + _i18n.SetCulture(new CultureInfo(lang)); + } } } diff --git a/Masa.Blazor.Pro/Global/CookieStorage.cs b/Masa.Blazor.Pro/Global/CookieStorage.cs new file mode 100644 index 0000000..1c47cf6 --- /dev/null +++ b/Masa.Blazor.Pro/Global/CookieStorage.cs @@ -0,0 +1,30 @@ +using Microsoft.JSInterop; + +namespace Masa.Blazor.Pro; + +public class CookieStorage +{ + private readonly IJSRuntime _jsRuntime; + + public CookieStorage(IJSRuntime jsRuntime) + { + _jsRuntime = jsRuntime; + } + + public async Task GetAsync(string key) + { + return await _jsRuntime.InvokeAsync(JsInteropConstants.GetCookie, key); + } + + public async void SetAsync(string key, T? value) + { + try + { + await _jsRuntime.InvokeVoidAsync(JsInteropConstants.SetCookie, key, value?.ToString()); + } + catch + { + // ignored + } + } +} diff --git a/Masa.Blazor.Pro/Global/ServiceCollectionExtensions.cs b/Masa.Blazor.Pro/Global/ServiceCollectionExtensions.cs index 4d34c6f..8b9e9d8 100644 --- a/Masa.Blazor.Pro/Global/ServiceCollectionExtensions.cs +++ b/Masa.Blazor.Pro/Global/ServiceCollectionExtensions.cs @@ -6,6 +6,7 @@ public static IServiceCollection AddGlobalForServer(this IServiceCollection serv { var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new Exception("Get the assembly root directory exception!"); services.AddNav(Path.Combine(basePath, $"wwwroot/nav/nav.json")); + services.AddScoped(); services.AddScoped(); return services; @@ -16,6 +17,7 @@ public static async Task AddGlobalForWasmAsync(this IService using var httpclient = new HttpClient(); var navList = await httpclient.GetFromJsonAsync>(Path.Combine(baseUri, $"nav/nav.json")) ?? throw new Exception("please configure the Navigation!"); services.AddNav(navList); + services.AddScoped(); services.AddScoped(); return services; diff --git a/Masa.Blazor.Pro/Masa.Blazor.Pro.csproj b/Masa.Blazor.Pro/Masa.Blazor.Pro.csproj index 2b2f2b7..9f6ce5c 100644 --- a/Masa.Blazor.Pro/Masa.Blazor.Pro.csproj +++ b/Masa.Blazor.Pro/Masa.Blazor.Pro.csproj @@ -12,7 +12,7 @@ - + diff --git a/Masa.Blazor.Pro/Pages/Authentication/Components/Login.razor.cs b/Masa.Blazor.Pro/Pages/Authentication/Components/Login.razor.cs index caa7c84..68f10d3 100644 --- a/Masa.Blazor.Pro/Pages/Authentication/Components/Login.razor.cs +++ b/Masa.Blazor.Pro/Pages/Authentication/Components/Login.razor.cs @@ -1,4 +1,6 @@ -namespace Masa.Blazor.Pro.Pages.Authentication.Components; +using Microsoft.AspNetCore.Components.Web; + +namespace Masa.Blazor.Pro.Pages.Authentication.Components; public partial class Login { diff --git a/Masa.Blazor.Pro/Shared/MainLayout.razor b/Masa.Blazor.Pro/Shared/MainLayout.razor index b28a1fa..7f3c0c0 100644 --- a/Masa.Blazor.Pro/Shared/MainLayout.razor +++ b/Masa.Blazor.Pro/Shared/MainLayout.razor @@ -1,9 +1,9 @@ @inherits LayoutComponentBase -@inject I18n I18n +@inject GlobalConfig GlobalConfig - +
@@ -62,9 +62,19 @@ private string PageModeClass => _pageTab == PageModes.PageTab ? "page-mode--tab" : "page-mode--breadcrumb"; + protected override async Task OnAfterRenderAsync(bool firstRender) + { + await base.OnAfterRenderAsync(firstRender); + + if (firstRender) + { + await GlobalConfig.InitFromStorage(); + } + } + void OnLanguageChanged(CultureInfo culture) { - I18n.SetCulture(culture); + GlobalConfig.Culture = culture; } } diff --git a/Masa.Blazor.Pro/_Imports.cs b/Masa.Blazor.Pro/_Imports.cs index 1fedde5..f4a8efc 100644 --- a/Masa.Blazor.Pro/_Imports.cs +++ b/Masa.Blazor.Pro/_Imports.cs @@ -1,5 +1,6 @@ global using BlazorComponent; global using BlazorComponent.I18n; +global using Masa.Blazor.Pro; global using Masa.Blazor.Pro.Data.App.ECommerce; global using Masa.Blazor.Pro.Data.App.ECommerce.Dto; global using Masa.Blazor.Pro.Data.App.Invoice; @@ -14,13 +15,13 @@ global using Masa.Blazor.Pro.Data.Dashboard.Analytics; global using Masa.Blazor.Pro.Data.Others.AccountSettings; global using Masa.Blazor.Pro.Data.Others.AccountSettings.Dto; -global using Masa.Blazor.Pro.Data.Shared.Favorite; global using Masa.Blazor.Pro.Global; global using Masa.Blazor.Pro.Global.Config; global using Masa.Blazor.Pro.Global.Nav.Model; +global using Masa.Blazor.Pro.Data.Shared.Favorite; global using Microsoft.AspNetCore.Components; -global using Microsoft.AspNetCore.Components.Web; global using System.ComponentModel; global using System.ComponentModel.DataAnnotations; global using System.Reflection; global using System.Text.Json; +