Use cookie everywhere

This commit is contained in:
hishamco 2020-12-03 17:15:08 +03:00
parent 049ded6f7e
commit 20f1a6175f
7 changed files with 9 additions and 50 deletions

View File

@ -31,6 +31,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="5.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>

View File

@ -10,6 +10,7 @@ using System.Runtime.Loader;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using Oqtane.Modules;
@ -94,7 +95,8 @@ namespace Oqtane.Client
var host = builder.Build();
var jsRuntime = host.Services.GetRequiredService<IJSRuntime>();
var interop = new Interop(jsRuntime);
var culture = await interop.GetLocalStorage("OqtaneCulture");
var localizationCookie = await interop.GetCookie(CookieRequestCultureProvider.DefaultCookieName);
var culture = CookieRequestCultureProvider.ParseCookieValue(localizationCookie).UICultures[0].Value;
var localizationService = host.Services.GetRequiredService<ILocalizationService>();
var cultures = await localizationService.GetCulturesAsync();

View File

@ -1,6 +1,7 @@
@namespace Oqtane.Themes.Controls
@inherits ThemeControlBase
@using System.Globalization
@using Microsoft.AspNetCore.Localization;
@using Oqtane.Models
@inject ILocalizationService LocalizationService
@inject NavigationManager NavigationManager
@ -36,7 +37,8 @@
if (culture != CultureInfo.CurrentUICulture.Name)
{
var interop = new Interop(JSRuntime);
await interop.SetLocalStorage("OqtaneCulture", culture);
var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture));
await interop.SetCookie(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue, 360);
NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
}

View File

@ -1,5 +1,4 @@
using Microsoft.JSInterop;
using Oqtane.Models;
using System.Threading.Tasks;
namespace Oqtane.UI
@ -233,33 +232,5 @@ namespace Oqtane.UI
return Task.CompletedTask;
}
}
public async Task<string> GetLocalStorage(string name)
{
try
{
var value = await _jsRuntime.InvokeAsync<string>("Oqtane.Interop.getLocalStorage", name);
return value;
}
catch
{
return null;
}
}
public Task SetLocalStorage(string name, string value)
{
try
{
_jsRuntime.InvokeVoidAsync("Oqtane.Interop.setLocalStorage", name, value);
return Task.CompletedTask;
}
catch
{
return Task.CompletedTask;
}
}
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Globalization;
using System;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
@ -31,8 +30,6 @@ namespace Oqtane.Extensions
var defaultCulture = localizationManager.GetDefaultCulture();
var supportedCultures = localizationManager.GetSupportedCultures();
CultureInfo.CurrentUICulture = new CultureInfo(defaultCulture);
app.UseRequestLocalization(options => {
options.SetDefaultCulture(defaultCulture)
.AddSupportedUICultures(supportedCultures)

View File

@ -1,17 +1,9 @@
@page "/"
@namespace Oqtane.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@model Oqtane.Pages.HostModel
@{
// Set localization cookie
var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture));
HttpContext.Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue);
}
<!DOCTYPE html>
<html>
<head>

View File

@ -362,11 +362,5 @@ Oqtane.Interop = {
setInterval(function () {
window.location.href = url;
}, wait * 1000);
},
getLocalStorage: function (name) {
return window.localStorage[name];
},
setLocalStorage: function (name, value) {
window.localStorage[name] = value;
}
};