相关疑难解决方法(0)

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

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

c# localization asp.net-core asp.net-core-localization

5
推荐指数
1
解决办法
277
查看次数

更改 IStringLocalizer 的语言

我试图IStringLocalizer在运行时动态设置语言。似乎可以做到这一点的唯一可用方法是IStringLocalizer.WithCulture. 但是,尝试使用它会导致弃用警告。

public IStringLocalizer GetLocalizer(string locale) {
    this.localizerFactory.Create(typeof(CommonResources)).WithCulture(new CultureInfo(locale));
}
Run Code Online (Sandbox Code Playgroud)

我没有使用 ASP,而是在IHostedService处理来自另一个来源(各种网络聊天 API)的用户交互中执行此操作。此服务需要符合管理员为聊天服务器或频道设置的语言(存储在数据库中)。

设置 . 语言的正确的当前方法是IStringLocalizer什么?我应该完全使用另一个类吗?

c# internationalization .net-core

2
推荐指数
2
解决办法
2401
查看次数