我很努力讓我的應用程序正確地本地化字符串。感覺我搜索了網絡的每一個角落,卻沒有找到我期望的東西。本地化ASP.NET Core MVC應用程序
我使用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核心的默認本地化是混亂的。 http://www.dotnetcurry.com/aspnet/1314/aspnet-core-globalization-localization http://scottkdavis.com/posts/localizing-an-asp-web-application.html –