自学 .Net Core asp。
我已将资源文件添加到我的解决方案中。 我已向该资源文件添加了一个测试字符串。 我现在正在尝试在我的 Controller 中访问此测试字符串/ key 。
所以,在我的startup.cs中我有这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-GB"),
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
};
opts.DefaultRequestCulture = new RequestCulture("en");
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, WorkerContext context)
{
app.UseStaticFiles();
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
在我的 Controller 中我有这个:
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
}
我的资源文件存储在这里:
我在这里设置了一个断点:
_localizer = 本地化器;
我不明白什么?
最佳答案
问题 1
基于https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization
您应该将您的资源
放在根目录下名为Resources的文件夹中。它位于 Properties
文件夹下...
此代码块(与您的略有不同)
Statup.cs
services.AddLocalization(options => options.ResourcesPath = "Resources");
在我的项目中解析为该文件夹。
问题 2
另一个问题是你的命名不正确。看一下名为 Working With Resource Files 的部分这比我把它放在这里更能向你解释它。
关于c# - 当前文化不存在 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356050/