ASP.NET Core 3.0中的本地化 [英] Localization in ASP.NET Core 3.0

查看:205
本文介绍了ASP.NET Core 3.0中的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make my project Localized, using .resx files.

对我来说,这是行不通的,对我的同事来说,它也在项目中工作.

For me it does not work, for my colleague, who is working on the project too it works.

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

Some details about the code: Startup.cs file

 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.

for me it returns "TitleLogin" and the value "ResourceNotFound" is true.

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

For my colleague it returns correct value with the same code...

您能帮我吗-我做错了吗?

Could you please help me - what I'm doing wrong?

非常感谢.

推荐答案

我不知道哪个resx文件包含TitleLogin.为了显示正确的本地化数据,当我将services.AddLocalization(options => options.ResourcesPath = "Lang/");修改为

I do not know which resx file contains TitleLogin.For displaying correct localization data, it wroks when I modify services.AddLocalization(options => options.ResourcesPath = "Lang/"); to

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

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

And then add a resx file named Views.Account.Login.cs.resx in Lang folder.

更新2020年3月19日

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

It turns out that in asp.net core 3.1, you need to place Default.cs out of Resources folder(your Lang folder here),see this github issue.

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

If class Default.cs and Default.*.resx in same folder, the namespace will be error in compiled dll xxx.lang.dll.

所以,解决方案是

1.删除原始的Default.cs,然后直接在项目下创建一个新的文件:

1.Delete original Default.cs and create a new one under the project directly:

namespace MyApp
{
    public class Default
    {
    }
}

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

2.Add Default.cs.resx in the Lang folder

3._ViewImports.cshtml

3._ViewImports.cshtml

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

这篇关于ASP.NET Core 3.0中的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆