Merge pull request #967 from hishamco/language-switcher
Add Language switcher
This commit is contained in:
@ -198,6 +198,8 @@
|
||||
</div>
|
||||
}
|
||||
|
||||
<LanguageSwitcher />
|
||||
|
||||
@if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.Permissions) || (PageState.Page.IsPersonalizable && PageState.User != null))
|
||||
{
|
||||
if (PageState.EditMode)
|
||||
|
46
Oqtane.Client/Themes/Controls/LanguageSwitcher.razor
Normal file
46
Oqtane.Client/Themes/Controls/LanguageSwitcher.razor
Normal file
@ -0,0 +1,46 @@
|
||||
@namespace Oqtane.Themes.Controls
|
||||
@inherits ThemeControlBase
|
||||
@using System.Globalization
|
||||
@using Microsoft.AspNetCore.Localization;
|
||||
@using Oqtane.Models
|
||||
@inject ILocalizationService LocalizationService
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
@if (_supportedCultures != null && Visible)
|
||||
{
|
||||
<div class="btn-group" role="group">
|
||||
<button id="btnCultures" type="button" class="btn btn-outline-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||
<span class="oi oi-globe"></span>
|
||||
</button>
|
||||
<div class="dropdown-menu" aria-labelledby="btnCultures">
|
||||
@foreach (var culture in _supportedCultures)
|
||||
{
|
||||
<a class="dropdown-item @(CultureInfo.CurrentUICulture.Name == culture.Name ? "active" : String.Empty)" href="#" @onclick="@(async e => await SetCultureAsync(culture.Name))">@culture.DisplayName</a>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@code{
|
||||
private IEnumerable<Culture> _supportedCultures;
|
||||
|
||||
[Parameter]
|
||||
public bool Visible { get; set; } = true;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_supportedCultures = await LocalizationService.GetCulturesAsync();
|
||||
}
|
||||
|
||||
private async Task SetCultureAsync(string culture)
|
||||
{
|
||||
if (culture != CultureInfo.CurrentUICulture.Name)
|
||||
{
|
||||
var interop = new Interop(JSRuntime);
|
||||
var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture));
|
||||
await interop.SetCookie(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue, 360);
|
||||
|
||||
NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user