ASP.NET Core 3.0中的本地化

编程入门 行业动态 更新时间:2024-10-25 04:13:56
本文介绍了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 core 3.1中,您需要将Default.cs放在Resources文件夹(这里是您的Lang文件夹)外面,请参阅此 github问题.

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

如果类Default.cs和Default.*.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中的本地化

本文发布于:2023-11-15 06:32:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!