Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to allow all cultures in my application. As you can see below, I'm allowing few cultures. And I'm having a custom provider that will get the user's culture. If his culture isn't in the SupportedCultures that means I can't handle his culture (even if I can). I can't know before assigning the SupportedCultures what cultures will be supported.

E.g. GetTheUserCulture() returns "de". When I'll try later to have the culture, it will fallback to the default language ("en" in this case). Or I want it to be "de".

Is there a way to Allow all cultures ?

            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());
                }));
            });
like image 433
Tris Avatar asked Feb 04 '20 16:02

Tris


People also ask

Which control can you use to apply localization?

Localizing Static Text If a page includes static text, you can use ASP.NET localization by including it in a Localize control, and then using explicit localization to set the static text. The Localize control renders no markup; its only function is to act as a placeholder for localized text.

Do we have global ASAX in .NET core?

Global. ASP.NET Core introduced a new mechanism for bootstrapping an app. The entry point for ASP.NET applications is the Global. asax file. Tasks such as route configuration and filter and area registrations are handled in the Global.


1 Answers

We can retrieve all cultures with CultureInfo and then add it to the SupportedCultures. It will look like that :

            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;
            }
like image 94
Tris Avatar answered Oct 02 '22 12:10

Tris