Use Interop

This commit is contained in:
hishamco
2020-12-02 02:10:01 +03:00
parent 75556070d6
commit 330499dda5
5 changed files with 42 additions and 11 deletions

View File

@ -16,6 +16,7 @@ using Oqtane.Modules;
using Oqtane.Providers; using Oqtane.Providers;
using Oqtane.Services; using Oqtane.Services;
using Oqtane.Shared; using Oqtane.Shared;
using Oqtane.UI;
namespace Oqtane.Client namespace Oqtane.Client
{ {
@ -92,7 +93,8 @@ namespace Oqtane.Client
var host = builder.Build(); var host = builder.Build();
var jsRuntime = host.Services.GetRequiredService<IJSRuntime>(); var jsRuntime = host.Services.GetRequiredService<IJSRuntime>();
var culture = await jsRuntime.InvokeAsync<string>("oqtaneCulture.get"); var interop = new Interop(jsRuntime);
var culture = await interop.getCulture();
if (culture != null) if (culture != null)
{ {
var cultureInfo = CultureInfo.GetCultureInfo(culture); var cultureInfo = CultureInfo.GetCultureInfo(culture);

View File

@ -25,13 +25,15 @@
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
_selectedCulture = _selectedCulture = await JSRuntime.InvokeAsync<string>("oqtaneCulture.get"); var interop = new Interop(JSRuntime);
_selectedCulture = await interop.getCulture();
_supportedCultures = await LocalizationService.GetSupportedCultures(); _supportedCultures = await LocalizationService.GetSupportedCultures();
} }
private async Task SetCultureAsync(string culture) private async Task SetCultureAsync(string culture)
{ {
await JSRuntime.InvokeVoidAsync("oqtaneCulture.set", culture); var interop = new Interop(JSRuntime);
await interop.setCulture(culture);
NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
} }

View File

@ -1,4 +1,4 @@
using Microsoft.JSInterop; using Microsoft.JSInterop;
using Oqtane.Models; using Oqtane.Models;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -234,5 +234,32 @@ namespace Oqtane.UI
} }
} }
public async Task<string> getCulture()
{
try
{
var culture = await _jsRuntime.InvokeAsync<string>("Oqtane.Interop.getCulture");
return culture;
}
catch
{
return null;
}
}
public Task setCulture(string culture)
{
try
{
_jsRuntime.InvokeVoidAsync("Oqtane.Interop.setCulture", culture);
return Task.CompletedTask;
}
catch
{
return Task.CompletedTask;
}
}
} }
} }

View File

@ -1,4 +1,4 @@
@page "/" @page "/"
@namespace Oqtane.Pages @namespace Oqtane.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using System.Globalization @using System.Globalization
@ -48,12 +48,6 @@
@if (Configuration.GetSection("Runtime").Value == "WebAssembly") @if (Configuration.GetSection("Runtime").Value == "WebAssembly")
{ {
<script src="_framework/blazor.webassembly.js"></script> <script src="_framework/blazor.webassembly.js"></script>
<script>
window.oqtaneCulture = {
get: () => window.localStorage['OqtaneCulture'],
set: (value) => window.localStorage['OqtaneCulture'] = value
};
</script>
} }
else else
{ {

View File

@ -362,5 +362,11 @@ Oqtane.Interop = {
setInterval(function () { setInterval(function () {
window.location.href = url; window.location.href = url;
}, wait * 1000); }, wait * 1000);
},
getCulture: function () {
return window.localStorage['OqtaneCulture'];
},
setCulture: function (culture) {
window.localStorage['OqtaneCulture'] = culture;
} }
}; };