【发布时间】:2020-02-01 17:00:24
【问题描述】:
我可能没有在Startup.cs 文件中进行正确的配置。我创建了一个演示应用程序以使其正常工作,但在尝试了各种方法后它无法正常工作。演示存储库可在以下链接获得
https://github.com/gurpreet42/MyAppV3
startup.cs 文件的配置是
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<LocService>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("nl")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
{
var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
return factory.Create("SharedResource", assemblyName.Name);
};
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
// Localisation
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseSession();
app.UseSession();
app.UseCookiePolicy();
}
LocService类中的代码是
public class LocService
{
private readonly IStringLocalizer _localizer;
public LocService(IStringLocalizerFactory factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create("SharedResource", assemblyName.Name);
}
public LocalizedString GetLocalizedHtmlString(string key)
{
var value= _localizer[key];
return value;
}
}
现在在我们的控制器上,我们可以访问本地化字符串
localizerService.GetLocalizedHtmlString("my_string")
在“资源”文件夹下,我们有以下文件
SharedResource.cs
SharedResource.en-US.resx
SharedResource.nl.resx
请提出配置错误的地方还是我需要添加一些额外的包?
【问题讨论】:
-
你的问题解决了吗?我也有同样的问题,就是在 .net core 3.1 dll 中找不到资源文件
标签: c# .net asp.net-core .net-core