Skip to content

Commit

Permalink
fix(identity-pages-localization): adds singleton SharedResourceLocali…
Browse files Browse the repository at this point in the history
…zer services to access IStringLocalizer from Razor identity pages for correct localization.

resolves: aspnetcorehero#34
  • Loading branch information
edusoccer1121 committed Sep 26, 2021
1 parent c5c9bca commit b9786db
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 29 deletions.
Binary file added .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@
@model AspNetCoreHero.Boilerplate.Web.Areas.Identity.Pages.Account.AuditLogModel

@{
ViewData["Title"] = localizer["Audit Log"];
ViewData["Caption"] = localizer["View your Recent 250 Logs here"];
ViewData["Title"] = localizer.GetLocalizedHtmlString("Audit Log");
ViewData["Caption"] = localizer.GetLocalizedHtmlString("View your Recent 250 Logs here");
}
@inject AspNetCoreHero.Boilerplate.Web.Services.SharedResourceLocalizer localizer;
<div class="card">
<div id="viewAll" class="card-body table-responsive">
<table class="table table-striped" id="logTable">
<thead>
<tr>
<th>
@localizer["Id"]
@localizer.GetLocalizedHtmlString("Id")
</th>
<th>
@localizer["Date"]
@localizer.GetLocalizedHtmlString("Date")
</th>
<th>
@localizer["Action"]
@localizer.GetLocalizedHtmlString("Action")
</th>
<th>
@localizer["Module"]
@localizer.GetLocalizedHtmlString("Module")
</th>
<th>
@localizer["Key"]
@localizer.GetLocalizedHtmlString("Key")
</th>
<th>
@localizer["Affected Columns"]
@localizer.GetLocalizedHtmlString("Affected Columns")
</th>
<th>
@localizer["Old Values"]
@localizer.GetLocalizedHtmlString("Old Values")
</th>
<th>
@localizer["New Values"]
@localizer.GetLocalizedHtmlString("New Values")
</th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Layout = null;
}
@using AspNetCoreHero.Boilerplate.Web.Views.Shared.Components.Culture;
@inject AspNetCoreHero.Boilerplate.Web.Services.SharedResourceLocalizer localizer;
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -26,7 +27,7 @@
<ul class="navbar-nav">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="darkSwitch">
<label class="custom-control-label" style="font-weight:400" for="darkSwitch">@localizer["Dark Mode"]</label>
<label class="custom-control-label" style="font-weight:400" for="darkSwitch">@localizer.GetLocalizedHtmlString("Dark Mode")</label>
</divv>
</ul>
</nav>
Expand All @@ -46,11 +47,11 @@
<div class="col-lg-6">
<div style="padding:10rem 3rem 10rem 3rem !important;">
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">@localizer["Login"]</h1>
<h1 class="h4 text-gray-900 mb-4">@localizer.GetLocalizedHtmlString("Login")</h1>
</div>
<form class="user" id="account" method="post">
<div class="form-group">
<input asp-for="Input.Email" type="email" class="form-control form-control-user" id="exampleInputEmail" aria-describedby="emailHelp" placeholder="Enter Email Address...">
<input asp-for="Input.Email" type="email" class="form-control form-control-user" id="exampleInputEmail" aria-describedby="emailHelp" placeholder="Enter your email...">
<small><span asp-validation-for="Input.Email" class="text-danger"></span></small>
</div>
<div class="form-group">
Expand All @@ -60,11 +61,11 @@
<div class="form-group">
<div class="custom-control custom-checkbox small">
<input asp-for="Input.RememberMe" type="checkbox" class="custom-control-input" id="customCheck">
<label class="custom-control-label" for="customCheck">@localizer["Remember Me"]</label>
<label class="custom-control-label" for="customCheck">@localizer.GetLocalizedHtmlString("Remember Me")</label>
</div>
</div>
<button type="submit" class="btn btn-primary btn-user btn-block">
@localizer["Login"]
@localizer.GetLocalizedHtmlString("Login")
</button>
@*<hr>
<a class="btn btn-google btn-user btn-block">
Expand All @@ -76,10 +77,10 @@
</form>
<hr>
<div class="text-center">
<a asp-area="Identity" asp-page="/Account/forgotpassword" class="small">@localizer["Forgot Password?"]</a>
<a asp-area="Identity" asp-page="/Account/forgotpassword" class="small">@localizer.GetLocalizedHtmlString("Forgot Password?")</a>
</div>
<div class="text-center">
<a asp-area="Identity" asp-page="/Account/register" class="small">@localizer["Create an Account!"]</a>
<a asp-area="Identity" asp-page="/Account/register" class="small">@localizer.GetLocalizedHtmlString("Create an Account!")</a>
</div>
</div>
</div>
Expand All @@ -92,7 +93,7 @@
<div class="col-xl-10 col-lg-12 col-md-9">
<div class="card">
<div class="card-header py-3">
<h6 class="m-0 text-secondary">@localizer["Default Credentials"]</h6>
<h6 class="m-0 text-secondary">@localizer.GetLocalizedHtmlString("Default Credentials")</h6>
</div>
<div class="card-body">
<div class="row no-gutters align-items-center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ViewData["Title"] = "Register";
Layout = null;
}
@inject AspNetCoreHero.Boilerplate.Web.Services.SharedResourceLocalizer localizer;
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -26,7 +27,7 @@
<ul class="navbar-nav">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="darkSwitch">
<label class="custom-control-label" style="font-weight:400" for="darkSwitch">@localizer["Dark Mode"]</label>
<label class="custom-control-label" style="font-weight:400" for="darkSwitch">@localizer.GetLocalizedHtmlString("Dark Mode")</label>
</div>
</ul>
</nav>
Expand All @@ -46,47 +47,47 @@
<div class="col-lg-7">
<div class="p-5">
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">@localizer["Register"]</h1>
<h1 class="h4 text-gray-900 mb-4">@localizer.GetLocalizedHtmlString("Register")</h1>
</div>
<form class="user" asp-route-returnUrl="@Model.ReturnUrl" method="post">
<small><div asp-validation-summary="All" class="text-danger"></div></small>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input asp-for="Input.FirstName" type="text" class="form-control form-control-user" id="exampleFirstName" placeholder="@localizer["First Name"]">
<input asp-for="Input.FirstName" type="text" class="form-control form-control-user" id="exampleFirstName" placeholder="@localizer.GetLocalizedHtmlString("First Name")">
<small><span asp-validation-for="Input.FirstName" class="text-danger"></span></small>
</div>
<div class="col-sm-6">
<input asp-for="Input.LastName" type="text" class="form-control form-control-user" id="exampleLastName" placeholder="@localizer["Last Name"]">
<input asp-for="Input.LastName" type="text" class="form-control form-control-user" id="exampleLastName" placeholder="@localizer.GetLocalizedHtmlString("Last Name")">
<small><span asp-validation-for="Input.LastName" class="text-danger"></span></small>
</div>
</div>
<div class="form-group">
<input asp-for="Input.Email" type="email" class="form-control form-control-user" id="exampleInputEmail" placeholder="@localizer["Email Address"]">
<input asp-for="Input.Email" type="email" class="form-control form-control-user" id="exampleInputEmail" placeholder="@localizer.GetLocalizedHtmlString("Email Address")">
<small><span asp-validation-for="Input.Email" class="text-danger"></span></small>
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input asp-for="Input.Password" autocomplete="off" type="password" class="form-control form-control-user" id="exampleInputPassword" placeholder="@localizer["Password"]">
<input asp-for="Input.Password" autocomplete="off" type="password" class="form-control form-control-user" id="exampleInputPassword" placeholder="@localizer.GetLocalizedHtmlString("Password")">
<small><span asp-validation-for="Input.Password" class="text-danger"></span></small>
</div>
<div class="col-sm-6">
<input asp-for="Input.ConfirmPassword" autocomplete="off" type="password" class="form-control form-control-user" id="exampleRepeatPassword" placeholder="@localizer["Repeat Password"]">
<input asp-for="Input.ConfirmPassword" autocomplete="off" type="password" class="form-control form-control-user" id="exampleRepeatPassword" placeholder="@localizer.GetLocalizedHtmlString("Repeat Password")">
<small><span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span></small>
</div>
</div>
<button type="submit" class="btn btn-primary btn-user btn-block">
@localizer["Register Account"]
@localizer.GetLocalizedHtmlString("Register Account")
</button>
</form>
<script type="text/javascript" language=javascript>
$.validator.unobtrusive.parse(document);
</script>
<hr>
<div class="text-center">
<a class="small" asp-area="Identity" asp-page="/account/forgotpassword"> @localizer["Forgot Password?"]</a>
<a class="small" asp-area="Identity" asp-page="/account/forgotpassword"> @localizer.GetLocalizedHtmlString("Forgot Password?")</a>
</div>
<div class="text-center">
<a class="small" asp-area="Identity" asp-page="/account/login"> @localizer["Already have an account? Login!"]</a>
<a class="small" asp-area="Identity" asp-page="/account/login"> @localizer.GetLocalizedHtmlString("Already have an account? Login!")</a>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class ServiceCollectionExtensions
public static void AddMultiLingualSupport(this IServiceCollection services)
{
#region Registering ResourcesPath

services.AddSingleton<SharedResourceLocalizer>();
services.AddLocalization(options => options.ResourcesPath = "Resources");

#endregion Registering ResourcesPath
Expand Down
29 changes: 29 additions & 0 deletions AspNetCoreHero.Boilerplate.Web/Services/SharedResourceLocalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Reflection;
using Microsoft.Extensions.Localization;

namespace AspNetCoreHero.Boilerplate.Web.Services
{
public class SharedResourceLocalizer
{
public readonly IStringLocalizer _localizer;

public SharedResourceLocalizer(IStringLocalizerFactory factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create("SharedResource", assemblyName.Name);
}

public LocalizedString GetLocalizedHtmlString(string key)
{
return _localizer[key];
}

public LocalizedString GetLocalizedHtmlString(string key, string parameter)
{
return _localizer[key, parameter];
}

}
}

0 comments on commit b9786db

Please sign in to comment.