0

我一直在尝试按照这两个教程将本地化添加到我的 .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-culture-asp-net-core-web-app-3fac9a960c43

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

我只是无法更改下面的“ Home ”或“ myApp ”等任何文本。

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

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

在此处输入图像描述

网址:

我的包裹: 在此处输入图像描述

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

        }
    }
}

万维网根

在此处输入图像描述

resx 示例

在此处输入图像描述

resx 属性

在此处输入图像描述

4

2 回答 2

1

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

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

于 2020-05-02T14:46:41.647 回答
0

如果您使用的是不需要的V4.0.0LazZiya.TagHelpers.Localization,只需将其卸载,因为LocalizeTagHelper已移至LazZiya.ExpressLocalization最新版本。

因此,只需将 localize taghelper 添加到_ViewImports.cshtml如下:

@addTagHelper *, LazZiya.ExpressLocalization

并将资源文件中字符串的访问修饰符更改为No code generation

在此处输入图像描述

更多细节可以在ExpressLocalization wiki中找到

于 2020-05-01T13:16:18.507 回答