ASP.NET Core 3.0中的本地化

3

我正在尝试使用.resx文件将我的项目本地化。

在我的项目中,无法正常工作;然而我的同事也在这个项目上工作,并且他的工作是正常的。

以下是有关代码的一些详细信息: Startup.cs 文件

 public void ConfigureServices(IServiceCollection services)
        {
            .
            .
            .
            // Localization

            services.AddLocalization(options => options.ResourcesPath = "Lang/");

            services.AddMvc(option => option.EnableEndpointRouting = false)
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();



            services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new List<CultureInfo>
                    {
                        new CultureInfo("cs"),
                        //new CultureInfo("en")
                    };

                options.DefaultRequestCulture = new RequestCulture(culture: "cs", uiCulture: "cs");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
            });
            services.AddTransient<Messages>();
            // Localization end
        }

        // 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();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            // Localization
            var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(locOptions.Value);
            // Localization end

            .
            .
            .

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }

我的控制器:

public class AccountController : BasicController
    {     
        private readonly UserManager<User> userManager;
        private readonly IPasswordHasher<User> passwordHasher;
        private IStringLocalizer<Default> _localizer;


        public AccountController(UserManager<User> userManager, SignInManager<User> signInManager, IPasswordHasher<User> passwordHasher,
            IStringLocalizer<Default> LangDefault, IDataProtectionProvider provider) : base(signInManager,provider)
        {
            this.userManager = userManager;
            this.passwordHasher = passwordHasher;
            _localizer = LangDefault;
        }

我的登录界面:

@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

@{
    Layout = "_LayoutLogin";
    ViewData["Title"] = Localizer["TitleLogin"];

我的项目结构

对于我来说,它返回 "TitleLogin",并且值 "ResourceNotFound" 为 true。

对于我的同事来说,使用相同的代码返回正确的值...

请问您能帮我看看 - 我做错了什么吗?

非常感谢。

1个回答

4

我不知道哪个resx文件包含TitleLogin。为了显示正确的本地化数据,当我将services.AddLocalization(options => options.ResourcesPath = "Lang/");修改为以下内容时,它可以正常工作

services.AddLocalization(options => options.ResourcesPath = "Lang");

然后在Lang文件夹中添加一个名为Views.Account.Login.cs.resx的resx文件。

更新3/19/2020

事实证明,在asp.net core 3.1中,您需要将Default.cs放在Resources文件夹之外(这里是您的Lang文件夹),请参见此github issue

如果类Default.csDefault.*.resx在同一文件夹中,则编译后的dll xxx.lang.dll中的命名空间将出现错误。

因此,解决方案是

1.删除原始的Default.cs并在项目直接创建一个新的:

namespace MyApp
{
    public class Default
    {
    }
}

2. 将Default.cs.resx添加到Lang文件夹中

3._ViewImports.cshtml

@using MyApp
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Default> LangDefault

非常感谢您的建议。这个方法可行,但是我希望只有几个resx文件(因为每个视图的许多消息都是共同的)。TitleLogin包含在Default.cs.resx中,所以我想使用LangDefault["TitleLogin"],但是找到的值为null... - Lukas
我只想补充一下,在ViewImports中,我有@inject IStringLocalizer<Default> LangDefault... 当我查看LangDefault ->非公共成员 ->_localizer -> 非公共成员 ->_missingManifestCache时,我可以找到我的TitleLogin,但值为null... - Lukas
@Lukas,我发现这是一个破坏性的更改,请检查我的编辑后的答案。 - Ryan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接