繁体   English   中英

Asp.net 核心本地化 有没有办法允许所有文化?

[英]Asp.net core localization Is there any way to allow all cultures?

我想在我的应用程序中允许所有文化。 正如你在下面看到的,我允许的文化很少。 而且我有一个自定义提供程序,可以了解用户的文化。 如果他的文化不在 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;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM