我想允许我的应用中的所有区域性。如您在下面看到的,我允许很少的文化。而且我有一个定制的提供程序,它将了解用户的文化。如果他的文化不在SupportedCultures中,则意味着我无法处理他的文化(即使可以)。在分配SupportedCultures之前,我不知道将支持哪些文化。
例如GetTheUserCulture()返回“ de”。当我稍后尝试具有区域性时,它将回退到默认语言(在这种情况下为“ en”)。或者我希望它是“ de”。
是否有一种允许所有文化的方式?
const string defaultCulture = "en";
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo(defaultCulture),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
new CultureInfo("es"),
new CultureInfo("ru"),
new CultureInfo("ja"),
new CultureInfo("ar"),
new CultureInfo("zh"),
new CultureInfo("en-GB"),
new CultureInfo("en-UK")
};
options.DefaultRequestCulture = new RequestCulture(defaultCulture);
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
{
return new ProviderCultureResult(GetTheUserCulture());
}));
});
我们可以使用CultureInfo检索所有文化,然后将其添加到SupportedCultures。它看起来像这样:
services.Configure<RequestLocalizationOptions>(options =>
{
CultureInfo[] supportedCultures = CultureInfo.GetCultures(CultureTypes.AllCultures &~ CultureTypes.NeutralCultures)
.Where(cul => !String.IsNullOrEmpty(cul.Name))
.ToArray();
options.DefaultRequestCulture = new RequestCulture(defaultCulture);
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
}