Skip to content

Commit

Permalink
WIP: select language feature
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Aug 31, 2021
1 parent a9d4d61 commit c7473db
Show file tree
Hide file tree
Showing 16 changed files with 1,010 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CleanArchitecture.Razor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartAdmin.WebUI", "src\Sma
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{6BD2EC46-FA8F-44F3-AF33-903BBB347116}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Workflow", "src\Workflow\Workflow.csproj", "{A5A249F5-C62B-4F0E-9715-F8C65D37ACD3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Workflow", "src\Workflow\Workflow.csproj", "{A5A249F5-C62B-4F0E-9715-F8C65D37ACD3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
34 changes: 34 additions & 0 deletions src/Infrastructure/Constants/Localization/LocalizationConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CleanArchitecture.Razor.Infrastructure.Constants.Localization
{
public static class LocalizationConstants
{
public static readonly LanguageCode[] SupportedLanguages = {
new LanguageCode
{
Code = "en-US",
DisplayName= "English"
},

new LanguageCode
{
Code = "zh-CN",
DisplayName = "中文"
}
};
}

public class LanguageCode
{
public string DisplayName { get; set; }
public string Code { get; set; }
}
}
15 changes: 8 additions & 7 deletions src/SmartAdmin.WebUI/Areas/Identity/Pages/_PageHeader.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@{
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer _localizer
@{
var pageName = ViewBag.PageName ?? string.Empty;
var canLogin = new string[] { "register", "forgotpassword", "lockout", "logout" }.ToList().Contains(pageName);
}
Expand All @@ -8,17 +9,17 @@
@if (canLogin)
{
<span class="text-white opacity-50 ml-auto mr-2 hidden-sm-down">
Already a member?
@_localizer["Already a member?"]
</span>
<a class="btn-link text-white ml-auto ml-sm-0" asp-area="Identity" asp-page="/account/login">
Secure Login
@_localizer["Secure Login"]
</a>
}
else
{
<a class="btn-link text-white ml-auto" asp-area="Identity" asp-page="/account/register">
Register account
</a>
<a class="btn-link text-white ml-auto" asp-area="Identity" asp-page="/account/register">
@_localizer["Register account"]
</a>
}
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;

namespace SmartAdmin.WebUI.Extensions
{
public static class RequestLocalizationCookiesMiddlewareExtensions
{
public static IApplicationBuilder UseRequestLocalizationCookies(this IApplicationBuilder app)
{
app.UseMiddleware<RequestLocalizationCookiesMiddleware>();
return app;
}
}

public class RequestLocalizationCookiesMiddleware : IMiddleware
{
public CookieRequestCultureProvider Provider { get; }

public RequestLocalizationCookiesMiddleware(IOptions<RequestLocalizationOptions> requestLocalizationOptions)
{
Provider =
requestLocalizationOptions
.Value
.RequestCultureProviders
.Where(x => x is CookieRequestCultureProvider)
.Cast<CookieRequestCultureProvider>()
.FirstOrDefault();
}

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
if (Provider != null)
{
var feature = context.Features.Get<IRequestCultureFeature>();

if (feature != null)
{
// remember culture across request
context.Response
.Cookies
.Append(
Provider.CookieName,
CookieRequestCultureProvider.MakeCookieValue(feature.RequestCulture)
);
}
}

await next(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@model SmartAdmin.WebUI.Pages.Shared.Components.LanguageSelector.DefaultModel
<div class="dropdown-menu">
@foreach (var culture in Model.SupportedCultures)
{
<form method="GET">
<input type="hidden" name="culture" value="@culture.Name" />
<a href="javascript:void()" onclick="this.parentElement.submit();" class="dropdown-item" >@culture.DisplayName</a>
</form>
}
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Razor.Infrastructure.Constants.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace SmartAdmin.WebUI.Pages.Shared.Components.LanguageSelector
{

public class DefaultModel
{
public CultureInfo CurrentUICulture { get; set; }
public List<CultureInfo> SupportedCultures { get; set; }
}

}
53 changes: 24 additions & 29 deletions src/SmartAdmin.WebUI/Pages/Shared/_DropdownMenu.cshtml
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
<div class="dropdown-header bg-trans-gradient d-flex flex-row py-4 rounded-top">
<div class="d-flex flex-row align-items-center mt-1 mb-1 color-white">
<span class="mr-2">
<img src="@(User.GetProfilePictureDataUrl())" class="rounded-circle profile-image" alt="@(User.GetDisplayName())">
</span>
<div class="info-card-text">
<div class="fs-lg text-truncate text-truncate-lg">@(User.GetDisplayName())</div>
<span class="text-truncate text-truncate-md opacity-80">@(User.GetEmail())</span>
</div>
</div>
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer _localizer
<div class="dropdown-header bg-trans-gradient d-flex flex-row py-4 rounded-top">
<div class="d-flex flex-row align-items-center mt-1 mb-1 color-white">
<span class="mr-2">
<img src="@(User.GetProfilePictureDataUrl())" class="rounded-circle profile-image" alt="@(User.GetDisplayName())">
</span>
<div class="info-card-text">
<div class="fs-lg text-truncate text-truncate-lg">@(User.GetDisplayName())</div>
<span class="text-truncate text-truncate-md opacity-80">@(User.GetEmail())</span>
</div>
</div>
</div>
<div class="dropdown-divider m-0"></div>
<a href="#" class="dropdown-item" data-action="app-reset">
<span data-i18n="drpdwn.reset_layout">Reset Layout</span>
<span data-i18n="drpdwn.reset_layout">@_localizer["Reset Layout"]</span>
</a>
<a href="#" class="dropdown-item" data-toggle="modal" data-target=".js-modal-settings">
<span data-i18n="drpdwn.settings">Settings</span>
<span data-i18n="drpdwn.settings">@_localizer["Settings"]</span>
</a>
<div class="dropdown-divider m-0"></div>
<a href="#" class="dropdown-item" data-action="app-fullscreen">
<span data-i18n="drpdwn.fullscreen">Fullscreen</span>
<i class="float-right text-muted fw-n">F11</i>
<span data-i18n="drpdwn.fullscreen">@_localizer["Fullscreen"]</span>
<i class="float-right text-muted fw-n">F11</i>
</a>
<a href="#" class="dropdown-item" data-action="app-print">
<span data-i18n="drpdwn.print">Print</span>
<i class="float-right text-muted fw-n">Ctrl + P</i>
<span data-i18n="drpdwn.print">@_localizer["Print"]</span>
<i class="float-right text-muted fw-n">Ctrl + P</i>
</a>
<div class="dropdown-multilevel dropdown-multilevel-left">
<div class="dropdown-item">
Language
</div>
<div class="dropdown-menu">
<a href="#?lang=fr" class="dropdown-item" data-action="lang" data-lang="fr">Français</a>
<a href="#?lang=en" class="dropdown-item active" data-action="lang" data-lang="en">English (US)</a>
<a href="#?lang=es" class="dropdown-item" data-action="lang" data-lang="es">Español</a>
<a href="#?lang=ru" class="dropdown-item" data-action="lang" data-lang="ru">Русский язык</a>
<a href="#?lang=jp" class="dropdown-item" data-action="lang" data-lang="jp">日本語</a>
<a href="#?lang=ch" class="dropdown-item" data-action="lang" data-lang="ch">中文</a>
</div>
<div class="dropdown-item">
@_localizer["Language"]
</div>
<vc:language-selector />

</div>
<div class="dropdown-divider m-0"></div>
<a class="dropdown-item fw-500 pt-3 pb-3" asp-area="Identity" asp-page="/account/logout">
<span data-i18n="drpdwn.page-logout">Logout</span>
<span class="float-right fw-n">@(User.Identity.Name)</span>
<span data-i18n="drpdwn.page-logout">@_localizer["Logout"]</span>
<span class="float-right fw-n">@(User.Identity.Name)</span>
</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Already a member?" xml:space="preserve">
<value>Already a member?</value>
</data>
<data name="Secure Login" xml:space="preserve">
<value>Secure Login</value>
</data>
<data name="Register account" xml:space="preserve">
<value>Register account</value>
</data>
</root>
Loading

0 comments on commit c7473db

Please sign in to comment.