From d7b4437dd8976de3c4c6a64f545c5e903367c743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 3 May 2018 16:40:05 +0300 Subject: [PATCH] Added initial language switch implementation. --- .../ToolbarManager.cs | 2 + .../Theme/MainNavbar/Toolbar/Default.cshtml | 2 +- .../Items/LanguageSwitch/Default.cshtml | 14 ++++++ .../Items/LanguageSwitch/LanguageInfo.cs | 11 +++++ .../LanguageSwitchViewComponent.cs | 49 +++++++++++++++++++ .../LanguageSwitchViewComponentModel.cs | 16 ++++++ .../Toolbar/Items/UserMenu/Default.cshtml | 12 ++++- ...o.Abp.AspNetCore.Mvc.UI.Theme.Basic.csproj | 4 ++ .../wwwroot/views/shared/_AppLayout.css | 5 ++ .../Localization/AbpLanguagesController.cs | 19 +++++++ 10 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/Default.cshtml create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageInfo.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponent.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponentModel.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Localization/AbpLanguagesController.cs diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/ToolbarManager.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/ToolbarManager.cs index 51793c0cd7..5fefd1f243 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/ToolbarManager.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/ToolbarManager.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Views.Shared.Components.Theme.MainNavbar.Toolbar.Items.LanguageSwitch; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Views.Shared.Components.Theme.MainNavbar.Toolbar.Items.UserMenu; using Volo.Abp.DependencyInjection; @@ -12,6 +13,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars { Items = { + new ToolbarItem(typeof(LanguageSwitchViewComponent)), new ToolbarItem(typeof(UserMenuViewComponent)) } }; diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Default.cshtml b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Default.cshtml index 96b82b2f1c..bf5f7bcb66 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Default.cshtml +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Default.cshtml @@ -2,5 +2,5 @@ @model Toolbar @foreach (var toolbarItem in Model.Items) { - @(await Component.InvokeAsync(toolbarItem.ComponentType)) + @(await Component.InvokeAsync(toolbarItem.ComponentType)) } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/Default.cshtml b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/Default.cshtml new file mode 100644 index 0000000000..e3715ed337 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/Default.cshtml @@ -0,0 +1,14 @@ +@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Views.Shared.Components.Theme.MainNavbar.Toolbar.Items.LanguageSwitch +@model LanguageSwitchViewComponentModel + \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageInfo.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageInfo.cs new file mode 100644 index 0000000000..ca0d3b0f0d --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageInfo.cs @@ -0,0 +1,11 @@ +namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Views.Shared.Components.Theme.MainNavbar.Toolbar.Items.LanguageSwitch +{ + public class LanguageInfo + { + public string Name { get; set; } + + public string DisplayName { get; set; } + + public string Icon { get; set; } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponent.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponent.cs new file mode 100644 index 0000000000..97e43de185 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponent.cs @@ -0,0 +1,49 @@ +using System.Globalization; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Views.Shared.Components.Theme.MainNavbar.Toolbar.Items.LanguageSwitch +{ + public class LanguageSwitchViewComponent : AbpViewComponent + { + private readonly RequestLocalizationOptions _options; + + public LanguageSwitchViewComponent(IOptions options) + { + _options = options.Value; + } + + public IViewComponentResult Invoke() + { + //TODO: Better handle culture & uiculture separation! + + var model = new LanguageSwitchViewComponentModel + { + CurrentLanguage = new LanguageInfo + { + Name = CultureInfo.CurrentUICulture.Name, + DisplayName = CultureInfo.CurrentUICulture.DisplayName, + Icon = null //TODO! + } + }; + + foreach (var supportedUiCulture in _options.SupportedUICultures) + { + if (model.CurrentLanguage.Name == supportedUiCulture.Name) + { + continue; + } + + model.OtherLanguages.Add(new LanguageInfo + { + Name = supportedUiCulture.Name, + DisplayName = supportedUiCulture.DisplayName, + Icon = null //TODO! + }); + } + + return View("~/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/Default.cshtml", model); + } + } +} diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponentModel.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponentModel.cs new file mode 100644 index 0000000000..939e567e7c --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/LanguageSwitch/LanguageSwitchViewComponentModel.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Views.Shared.Components.Theme.MainNavbar.Toolbar.Items.LanguageSwitch +{ + public class LanguageSwitchViewComponentModel + { + public LanguageInfo CurrentLanguage { get; set; } + + public List OtherLanguages { get; } + + public LanguageSwitchViewComponentModel() + { + OtherLanguages = new List(); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/UserMenu/Default.cshtml b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/UserMenu/Default.cshtml index 1e4b10ec7c..d42b45bd5b 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/UserMenu/Default.cshtml +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/Components/Theme/MainNavbar/Toolbar/Items/UserMenu/Default.cshtml @@ -7,9 +7,17 @@ @inject IHtmlLocalizer L @if (CurrentUser.IsAuthenticated) { - @CurrentUser.UserName (@L["Logout"]) + } else { - Login + @L["Login"] } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.csproj b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.csproj index 2c358b1033..ad7365a86b 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.csproj +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.csproj @@ -19,6 +19,10 @@ + + + + diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/wwwroot/views/shared/_AppLayout.css b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/wwwroot/views/shared/_AppLayout.css index 586cc9979a..dd91bf36b9 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/wwwroot/views/shared/_AppLayout.css +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/wwwroot/views/shared/_AppLayout.css @@ -1,3 +1,8 @@ body { padding-top: 5rem; +} + +#main-navbar-tools a.dropdown-toggle { + text-decoration: none; + color: #fff; } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Localization/AbpLanguagesController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Localization/AbpLanguagesController.cs new file mode 100644 index 0000000000..79f9f17b2d --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Localization/AbpLanguagesController.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; + +namespace Volo.Abp.AspNetCore.Mvc.Localization +{ + [Area("Abp")] + [Route("Abp/Languages/[action]")] + public class AbpLanguagesController : AbpController + { + [HttpGet] + public IActionResult Switch(string culture, string uiCulture = "") //TODO: Implement return URL + { + //TODO: Check allowed languages and so on... + + Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName, $"c={culture}|uic={uiCulture ?? culture}"); + return Redirect("/"); + } + } +}