使用LazZiya ExpressLocalization Nuget包来本地化网络应用。

问题描述 投票:0回答:1

我一直试图按照这两个教程来为我的.Net Core Razor Web应用添加本地化。

http:/ziyad.infoenarticles36-Develop_Multi_Cultural_Web_Application_Using_ExpressLocalization。

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

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

我只是不能得到任何文本,如''。首页'或'myApp下面的''来改变。

当我在下拉菜单中选择一种语言时,语言是在URL中指定的(见下图),但我的文本就是不改变。

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

enter image description here

URLl:

我的包。enter image description here

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>

索引.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);
        }

    }
}

启动.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

enter image description here

resx例子

enter image description here

Resx properties

enter image description here

c# localization nuget-package resx
1个回答
1
投票

我的resx文件的Build Action属性为'Content',而不是''。嵌入式资源'.而我的LocSource.cs的Build Action属性为'Embedded Resource',而不是''。C#编译器'.

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


0
投票

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

所以,只需将本地化标签帮助程序添加到 _ViewImports.cshtml 如下图所示。

@addTagHelper *, LazZiya.ExpressLocalization

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

enter image description here

更多详情请见 ExpressLocalization wiki

© www.soinside.com 2019 - 2024. All rights reserved.