【发布时间】:2020-02-03 14:36:02
【问题描述】:
我们在 Asp.net 核心版本 2.2 中有一个网站。 我们已将网站从 2.2 版更新到 3.1 版。
将应用程序更新到 3.1 版后,我们在 2.2 版中用于本地化的相同代码无法正常工作。
我已将代码放在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 核心版本 2.2 更新到版本 3.1 后,值没有转换为所选语言。
【问题讨论】:
-
你能显示 v3.1 的
startup.cs吗? -
@LazZiya 我已经编辑了帖子显示提到了startup.cs的内容
-
它仍然无法正常工作。你能检查我创建的演示应用程序并建议我在那里做错了吗github.com/gurpreet42/MyAppV3我怎样才能使它工作。
标签: c# asp.net-core .net-core