gpt4 book ai didi

asp.net-mvc - 如何将 IStringLocalizer 注入(inject) IApplicationModelConvention?

转载 作者:行者123 更新时间:2023-12-05 04:03:07 25 4
gpt4 key购买 nike

我有一个自定义约定,需要在其中包含本地化字符串。据我所知,实例化 IStringLocalizer 的唯一方法是 DependencyInjection。

如何在 CustomConvention 中使用 IStringLocalizer?

约定是这样注册的

public void ConfigureServices(IServiceCollection services)
{
//First I register the localization settings
services.AddLocalization(o =>
{
o.ResourcesPath = "Resources";
});

services.AddMvc(options =>
{
//My custom convention, I would need to inject an IStringLocalizer
//into constructor here, but I can' instantiate it
options.Conventions.Add(new LocalizationRouteConvention());
})
}

最佳答案

我的解决方案并不完美。

您可以做类似的事情,构建服务提供者以获取 StringLocalizer 的实例。

public void ConfigureServices(IServiceCollection services)
{
//First I register the localization settings
services.AddLocalization(o =>
{
o.ResourcesPath = "Resources";
});

var stringLocalizer = services.BuildServiceProvider().GetService<IStringLocalizer<Resource>>();

services.AddMvc(options =>
{
//My custom convention, I would need to inject an IStringLocalizer
//into constructor here, but I can' instantiate it
options.Conventions.Add(new LocalizationRouteConvention(stringLocalizer));
})
}

关于asp.net-mvc - 如何将 IStringLocalizer 注入(inject) IApplicationModelConvention?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53869945/

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