ASP.NET Core Razor页面中的本地化-文化

哈br!目前,OTUS正在开设一门新课程“ C#ASP.NET Core Developer”的套件在这方面,我们通常会与您分享有用的翻译,并邀请您注册一个开放日,在此期间,您可以详细了解该课程,并询问您感兴趣的专家问题。


这是有关ASP.NET Core Razor Pages应用程序中本地化的系列文章中的第一篇。在本文中,我们将研究为站点进行内容本地化(换句话说,为站点全球化)准备所需的配置。在以后的文章中,我将讨论如何创建本地化内容以及如何将其呈现给最终用户。

ASP.NET Core中的全球化

全球化正在根据用户的喜好准备一个支持不同语言的应用程序。本地化是使网站内容适应不同国家,地区或文化的过程。全球化Web应用程序的起点是为每个请求定义语言或区域性的能力。接下来,我们需要一种根据当前请求的文化来选择内容的机制。在本文中,我们将研究类CultureInfo在本地化中所扮演的角色,以及如何实现视图组件,以便用户可以为查询选择自己喜欢的区域性。

Razor Pages , - ASP.NET Core 3.0 . «Localisation». , , .

1. Startup.cs using :

using System.Globalization;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;

2. - . . ConfigureServices, AddLocalization, . RequestLocalizationOptions .

services.Configure<RequestLocalizationOptions>(options =>
{
   var supportedCultures = new[]
    {
        new CultureInfo("en"),
        new CultureInfo("de"),
        new CultureInfo("fr"),
        new CultureInfo("es"),
        new CultureInfo("ru"),
        new CultureInfo("ja"),
        new CultureInfo("ar"),
        new CultureInfo("zh"),
        new CultureInfo("en-GB")
    };
    options.DefaultRequestCulture = new RequestCulture("en-GB");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

, . .NET CultureInfo, , , , , . CultureInfo, , , (). - ISO 639-1, (, «en» ), ISO 3166, (, «en-GB» «en-ZA» ). , - , .

3. , RequestLocalizationOptions , , Configure app.UseRouting():

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(localizationOptions);

, RequestCultureProviders. :

  • QueryStringRequestCultureProvider,

  • CookieRequestCultureProvider, cookie

, . , . , , .

1. - Models CultureSwitcherModel.cs.

using System.Collections.Generic;
using System.Globalization;
 
namespace Localisation.Models
{
    public class CultureSwitcherModel
    {
        public CultureInfo CurrentUICulture { get; set; }
        public List<CultureInfo> SupportedCultures { get; set; }
    }
}

2. ViewComponents C# CultureSwitcherViewcomponent.cs. :

using Localisation.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Linq;
 
namespace Localisation.ViewComponents
{
    public class CultureSwitcherViewComponent : ViewComponent
    {
        private readonly IOptions<RequestLocalizationOptions> localizationOptions;
        public CultureSwitcherViewComponent(IOptions<RequestLocalizationOptions> localizationOptions) =>
            this.localizationOptions = localizationOptions;
 
        public IViewComponentResult Invoke()
        {
            var cultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
            var model = new CultureSwitcherModel
            {
                SupportedCultures = localizationOptions.Value.SupportedUICultures.ToList(),
                CurrentUICulture = cultureFeature.RequestCulture.UICulture
            };
            return View(model);
        }
    }
}

3. Pages Components. CultureSwitcher. Razor View default.cshtml :

@model CultureSwitcherModel
 
<div>
    <form id="culture-switcher">
        <select name="culture" id="culture-options">
            <option></option>
            @foreach (var culture in Model.SupportedCultures)
            {
                <option value="@culture.Name" selected="@(Model.CurrentUICulture.Name == culture.Name)">@culture.DisplayName</option>
            }
        </select>
    </form>
</div>
 
 
<script>
    document.getElementById("culture-options").addEventListener("change", () => {
        document.getElementById("culture-switcher").submit();
    });
</script>

- select , , Startup. , , get , , culture. QueryStringRequestCultureProvider culture (/ ui-culture).

CurrentCulture . CurrentUICulture , , . , . CurrentCulture CurrentUICulture , , . (, ), .

4. , , -   _ViewImports.cshtml using, , , tag- :

@using Localisation
@using Localisation.Models
@using System.Globalization
@namespace Localisation.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Localisation

5. , tag-, .

<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
    <ul class="navbar-nav flex-grow-1">
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
        </li>
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
        </li>
    </ul>
</div>
<vc:culture-switcher/>

6. Index.cshtml, HTML- , :

@page
@using Microsoft.AspNetCore.Localization
@model IndexModel
@{
    ViewData["Title"] = "Home page";
    var requestCultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
    var requestCulture = requestCultureFeature.RequestCulture;
}
 
<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
 
    <table class="table culture-table">
        <tr>
            <td style="width:50%;">Culture</td>
            <td>@requestCulture.Culture.DisplayName {@requestCulture.Culture.Name}</td>
        </tr>
        <tr>
            <td>UI Culture</td>
            <td>@requestCulture.UICulture.Name</td>
        </tr>
        <tr>
            <td>UICulture Parent</td>
            <td>@requestCulture.UICulture.Parent</td>
        </tr>
        <tr>
            <td>Date</td>
            <td>@DateTime.Now.ToLongDateString()</td>
        </tr>
        <tr>
            <td>Currency</td>
            <td>
                @(12345.00.ToString("c"))
            </td>
        </tr>
        <tr>
            <td>Number</td>
            <td>
                @(123.45m.ToString("F2"))
            </td>
        </tr>
    </table>
</div>

AcceptHeadersCultureRequestProvider. , QueryStringCultureRequestProvider. ui-culture culture (, https://localhost:xxxxx/?culture=es&ui-culture=de), , .

Razor Pages. , , , . , . , .

, , , , . , (.resx) .


.


:




All Articles