gpt4 book ai didi

c# - 当前文化不存在 list

转载 作者:行者123 更新时间:2023-11-30 20:30:44 26 4
gpt4 key购买 nike

自学 .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;
}
}

我的资源文件存放在这里:

enter image description here

我对这个资源文件的属性是: enter image description here

我这里设置了一个断点:

_localizer = 定位器;

我检查了这个变量,但正如您所见,尚未找到 list ... enter image description here

请问我有什么不明白的?

最佳答案

问题 1

基于 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization

您应该将Resources 放在根目录下名为Resources 的文件夹中。你在 Properties 文件夹下...

此代码块(与您的略有不同)
Statup.cs

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

在我的项目中为我解析到该文件夹​​。

enter image description here


第 2 期

另一个问题是您的命名不正确。查看名为 Working With Resource Files 的部分这比我把它放在这里更能向你解释。

关于c# - 当前文化不存在 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com