Merge pull request #967 from hishamco/language-switcher
Add Language switcher
This commit is contained in:
32
Oqtane.Server/Controllers/LocalizationController.cs
Normal file
32
Oqtane.Server/Controllers/LocalizationController.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Oqtane.Infrastructure;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Controllers
|
||||
{
|
||||
[Route(ControllerRoutes.Default)]
|
||||
public class LocalizationController : Controller
|
||||
{
|
||||
private readonly ILocalizationManager _localizationManager;
|
||||
|
||||
public LocalizationController(ILocalizationManager localizationManager)
|
||||
{
|
||||
_localizationManager = localizationManager;
|
||||
}
|
||||
|
||||
// GET: api/localization
|
||||
[HttpGet()]
|
||||
public IEnumerable<Culture> Get()
|
||||
=> _localizationManager.GetSupportedCultures().Select(c => new Culture {
|
||||
Name = CultureInfo.GetCultureInfo(c).Name,
|
||||
DisplayName = CultureInfo.GetCultureInfo(c).DisplayName,
|
||||
IsDefault = _localizationManager.GetDefaultCulture()
|
||||
.Equals(CultureInfo.GetCultureInfo(c).Name, StringComparison.OrdinalIgnoreCase)
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@ -31,11 +30,9 @@ namespace Oqtane.Extensions
|
||||
var defaultCulture = localizationManager.GetDefaultCulture();
|
||||
var supportedCultures = localizationManager.GetSupportedCultures();
|
||||
|
||||
CultureInfo.CurrentUICulture = new CultureInfo(defaultCulture);
|
||||
|
||||
app.UseRequestLocalization(options => {
|
||||
options.SetDefaultCulture(defaultCulture)
|
||||
.AddSupportedUICultures(supportedCultures)
|
||||
.AddSupportedCultures(supportedCultures)
|
||||
.AddSupportedUICultures(supportedCultures);
|
||||
});
|
||||
|
||||
|
@ -1,17 +1,9 @@
|
||||
@page "/"
|
||||
@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>
|
||||
|
@ -127,6 +127,7 @@ namespace Oqtane
|
||||
services.AddScoped<ISiteTemplateService, SiteTemplateService>();
|
||||
services.AddScoped<ISqlService, SqlService>();
|
||||
services.AddScoped<ISystemService, SystemService>();
|
||||
services.AddScoped<ILocalizationService, LocalizationService>();
|
||||
|
||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
|
||||
|
Reference in New Issue
Block a user