我正在尝试使用.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。
对于我的同事,它使用相同的代码返回正确的值...
您能帮我吗-我做错了吗?
非常感谢。
我不知道哪个resx文件包含TitleLogin
。为显示正确的本地化数据,当我修改services.AddLocalization(options => options.ResourcesPath = "Lang/");
为
services.AddLocalization(options => options.ResourcesPath = "Lang");
然后添加一个Views.Account.Login.cs.resx
在Lang
文件夹中命名的resx文件。
更新3/19/2020
事实证明,在asp.net core 3.1中,您需要放置Default.cs
在Resources文件夹(您的Lang
文件夹在此处)之外,请参阅此github问题。
如果类Default.cs
和Default.*.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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句