【发布时间】:2023-08-20 10:53:01
【问题描述】:
我正在努力让我的应用正确本地化字符串。感觉我已经搜索了网络的每个角落,但没有找到我期望的有用的东西。
我使用RouteDataRequestCultureProvider,我要解决的第一个问题是能够使用文化的“速记版本”,例如sv 而不是 sv-SE,并且在创建区域性时 sv 被视为 sv-SE。这不会自动发生。
其次是让应用程序显示本地化字符串。这是我配置本地化的方式
public static IServiceCollection ConfigureLocalization(this IServiceCollection services)
{
services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new System.Globalization.CultureInfo("sv-SE"),
new System.Globalization.CultureInfo("en-US")
};
options.DefaultRequestCulture = new RequestCulture(culture: "sv-SE", uiCulture: "sv-SE");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new[]
{
new RouteDataRequestCultureProvider
{
Options = options
}
};
});
return services;
}
然后在Configure 我做app.UseRequestLocalizationOptions(); 注入之前配置的选项。
在文件夹Resources 中,我创建了资源文件Resource.resx 和Resource.sv.resx。
在我的视图文件中,我尝试了注入IStringLocalizer(由于没有注册默认实现而失败)和IStringLocalizer<My.Namespace.Resources.Resource>,但没有一个选项有效。我也尝试过老式的方式@My.Namespace.Resources.Resource.StringToLocalize
不可能有共享资源文件吗?不想诉诸Resource\Views\ViewA.resx 和Resource\Controllers\AController.resx。
谢谢
【问题讨论】:
-
不可能有共享资源文件吗?我认为这是正确的。就个人而言,我认为 dotnet core 中的默认本地化是一团糟。 dotnetcurry.com/aspnet/1314/…scottkdavis.com/posts/localizing-an-asp-web-application.html
标签: asp.net-core asp.net-core-mvc asp.net-core-localization