diff --git a/Oqtane.Client/Themes/Controls/Theme/LanguageSwitcher.razor b/Oqtane.Client/Themes/Controls/Theme/LanguageSwitcher.razor index e59c9f2e8..b30486f5c 100644 --- a/Oqtane.Client/Themes/Controls/Theme/LanguageSwitcher.razor +++ b/Oqtane.Client/Themes/Controls/Theme/LanguageSwitcher.razor @@ -1,8 +1,9 @@ -@namespace Oqtane.Themes.Controls -@inherits ThemeControlBase @using System.Globalization @using Microsoft.AspNetCore.Localization +@using Microsoft.AspNetCore.Http @using Oqtane.Models +@namespace Oqtane.Themes.Controls +@inherits ThemeControlBase @inject ILanguageService LanguageService @inject NavigationManager NavigationManager @@ -15,7 +16,14 @@ @@ -23,9 +31,13 @@ @code{ private IEnumerable _supportedCultures; + private string MenuAlignment = string.Empty; + [Parameter] public string DropdownAlignment { get; set; } = string.Empty; // Empty or Left or Right - private string MenuAlignment = string.Empty; + + [CascadingParameter] + HttpContext HttpContext { get; set; } protected override void OnParametersSet() { @@ -33,16 +45,26 @@ var languages = PageState.Languages; _supportedCultures = languages.Select(l => new Culture { Name = l.Code, DisplayName = l.Name }); + + if (PageState.QueryString.ContainsKey("culture")) + { + var culture = PageState.QueryString["culture"]; + if (_supportedCultures.Any(item => item.Name == culture)) + { + var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)); + HttpContext.Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue, new CookieOptions { Path = "/", Expires = DateTimeOffset.UtcNow.AddYears(365) }); + } + NavigationManager.NavigateTo(NavigationManager.Uri.Replace($"?culture={culture}", ""), forceLoad: true); + } } private async Task SetCultureAsync(string culture) { if (culture != CultureInfo.CurrentUICulture.Name) { - var interop = new Interop(JSRuntime); var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)); + var interop = new Interop(JSRuntime); await interop.SetCookie(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue, 360); - NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); } }