使用 LazZiya ExpressLocalization Nuget 包本地化网络应用程序

Using LazZiya ExpressLocalization Nuget package to Localize web app

我一直在尝试按照这两个教程将本地化添加到我的 .Net Core Razor Web 应用程序。

http://ziyad.info/en/articles/36-Develop_Multi_Cultural_Web_Application_Using_ExpressLocalization

https://medium.com/swlh/step-by-step-tutorial-to-build-multi-cultural-asp-net-core-web-app-3fac9a960c43

我尝试过从头开始创建项目。我尝试添加到我现有的项目中。我尝试使用 LocalizeTagHelperSharedCultureLocalizer 选项但没有成功。

我无法更改任何文本,例如下面的“Home”或“myApp”。

当我在下拉列表中 select 语言时,该语言在 URL 中指定(见下文),但我的文本不会改变。

下拉组件和主页文本 x 2:

Url:

我的包裹:

Index.cshtml

@page
@model IndexModel
@using LazZiya.ExpressLocalization
@inject ISharedCultureLocalizer _loc

@{
    ViewData["Title"] = @_loc["myApp"];
}

    <body>
        <h1 class="display-4" localize-content>Home</h1>
        <header>
            <div class="bg-img">
                <div class="container-title">
                    <div class="block-title block-title1">
                        <language-nav cookie-handler-url="@Url.Page("/Index", "SetCultureCookie", new { area="", cltr="{0}", returnUrl="{1}" })"></language-nav>
                        <br>
                    </div>
                    <div class="block-title block-title2 d-none d-md-block d-lg-block d-xl-block"><img src="/image/title_image.png" class="img-fluid"></div>
                </div>
            </div>
        </header>
        <main>
            <div class="row_primary">
            </div>
        </main>
    </body>

Index.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace myApp.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }

        public IActionResult OnGetSetCultureCookie(string cltr, string returnUrl)
        {
            Response.Cookies.Append(
                CookieRequestCultureProvider.DefaultCookieName,
                CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(cltr)),
                new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
            );

            return LocalRedirect(returnUrl);
        }

    }
}

Startup.cs

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using LazZiya.ExpressLocalization;
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using myApp.wwwroot.LocalizationResources;

namespace myApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();

            var cultures = new[]
            {
                new CultureInfo("de"),
                new CultureInfo("fr"),
                new CultureInfo("en"),
            };
            services.AddRazorPages().AddExpressLocalization<ExpressLocalizationResource, ViewLocalizationResource >( ops =>
                {
                    ops.ResourcesPath = "LocalizationResources";
                    ops.RequestLocalizationOptions = o =>
                    {
                        o.SupportedCultures = cultures;
                        o.SupportedUICultures = cultures;
                        o.DefaultRequestCulture = new RequestCulture("en");
                    };
                });

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

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

            app.UseRouting();

            app.UseAuthorization();

            app.UseRequestLocalization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });

        }
    }
}

wwwroot

resx 示例

resx 属性

如果您正在使用 V4.0.0,则不需要 LazZiya.TagHelpers.Localization,只需卸载它,因为 LocalizeTagHelper 在最新版本中已移至 LazZiya.ExpressLocalization

因此,只需将本地化标签助手添加到 _ViewImports.cshtml,如下所示:

@addTagHelper *, LazZiya.ExpressLocalization

并将资源文件中字符串的访问修饰符改为不生成代码:

可以在ExpressLocalization wiki

中找到更多详细信息

我的 resx 文件的构建操作 属性 为 'Content' 而不是“嵌入式资源”。 我的 LocSource.cs 的构建操作 属性 为 'Embedded Resource' 而不是“C# 编译器”。

真的很期待现在使用这个包,看起来它会让生活变得更轻松。