在尝试本地化我的应用程序时,我遵循了此处的步骤: https://docs.asp.net/en/latest/fundamentals/localization.html
这是我的代码:
Startup.cs
public List<IRequestCultureProvider> RequestCultureProviders { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services.AddOptions();
services.AddTransient<IViewRenderingService, ViewRenderingService>();
services.AddTransient<IEmailSender, EmailSender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory())),
EnableDirectoryBrowsing = true
});
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures,
RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider
{
QueryStringKey = "culture",
UIQueryStringKey = "ui-culture"
}
}
});
}
MyController.cs
public class MyController : Controller
{
private readonly IViewRenderingService _viewRenderingService;
private IStringLocalizer<MyController> _localizer;
private MyOptions _options;
//Constructor for dependency injection principle
public MyController(IViewRenderingService viewRenderingService, IStringLocalizer<MyController> localizer, IOptions<MyOptions> options)
{
_viewRenderingService = viewRenderingService;
_localizer = localizer;
_options = options.Value;
}
[HttpGet]
public string Get()
{
// _localizer["Name"]
return _localizer["Product"];
}
}
*.resx
文件存储在 Resources
文件夹中,名称为 Controllers.MyController.fr.resx
(其中有一个“产品”条目)。
但是,它无法找到资源文件,并且永远不会以法语返回“Product”。我正在使用查询字符串,所以这里是查询字符串:
localhost:3333/my?culture=fr
同样在 View 中,@Localizer["Product"]
返回“Product”。
谁能帮我找到丢失的东西?
编辑:
经过一些调查,我发现文化正在发生变化,但是无法找到资源文件。我正在使用 VS2015。谁能帮忙?
最佳答案
我遇到了类似的问题。比我想出来的 “Localization.AspNetCore.TagHelpers”我的项目中缺少 nuget packag。 它看起来像是 QueryStringRequestCultureProvider 的必需包。
关于c# - ASP.Net Core MVC 中的本地化不起作用 - 无法找到资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39132868/