Injecting to pages
Page can be designed to use either Microsoft's service interfaces or Avalanche's localization interfaces, or both.
If Microsoft's interface is used then @inject with IStringLocalizer<T>, IViewLocalizer and IHtmlLocalizer<T>. The localization files must be supply lines to key "Assembly[.Resources].Namespace.Key".
Open Pages/_ViewImports.cshtml and add:
// Ms.Localization
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@inject IViewLocalizer ViewLocalizer
@page
@inject IStringLocalizer<IndexModel> Localizer
@inject IHtmlLocalizer<IndexModel> HtmlLocalizer
@model IndexModel
@{
ViewData["Title"] = Localizer["Home"];
}
<div class="text-center">
<h1 class="display-4">@Localizer["Welcome"]</h1>
<p>@HtmlLocalizer["Content"]</p>
</div>
If page uses Avalanche's interface then @inject with ITextLocalizer, ITextLocalizer<T>, IFileLocalizer, IFileLocalizer<T>, ILocalization interfaces.
Open Pages/_ViewImports.cshtml and add:
// Av.Localization
@using System.Globalization
@using Avalanche.Localization
@inject ITextLocalizer TextLocalizer
@inject IFileLocalizer FileLocalizer
There is extension method .Html(args?) and .Localize(args?) (Avalanche.Localization.Asp.dll) that adapt to LocalizedHtmlString and LocalizedString respective.
@{
ViewData["Title"] = TextLocalizer["samples.asp.Pages.IndexModel.Home"];
}
<div class="text-center">
<h1 class="display-4">@Localizer["samples.asp.Pages.IndexModel.Welcome"]</h1>
<p>@TextLocalizer["samples.asp.Pages.IndexModel.Content"].LocalizeHtml()</p>
</div>
Or use the model specific localizer ITextLocalizer<T>:
@page
@inject ITextLocalizer<IndexModel> IndexLocalizer
@model IndexModel
@{
ViewData["Title"] = IndexLocalizer["Home"];
}
<div class="text-center">
<h1 class="display-4">@Localizer["Welcome"]</h1>
<p>@IndexLocalizer["Content"].LocalizeHtml()</p>
</div>
Warning
TagHelper for <form> element seems to be based on ResourceTypeAttribute on model which searches for ResourceManager.
The flow doesn't go through DependencyInjection, IServiceProvider and IStringLocalizer stack and thus doesn't get localized.
<form method="post" asp-page="Index">
<div class="form-group">
<label asp-for="Hero" class="control-label"></label>
<input asp-for="Hero" class="form-control"/>
<span asp-validation-for="Hero" class="text-danger"></span>
</div>
<button type="submit">@Localizer["Submit"]</button>
</form>