0

I'm trying to list all the cultures available in a dropdown so that end user can choose a language-REGION locale code.

I have <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData> enabled in .csproj and I'm using the following code in my .NET8 Blazor WASM app.

<InputSelect @bind-Value="Locale" id="Locale" class="input">
    <option value="">Select</option>
    @foreach (var culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        <option checked="@(Settings.GeneralSettings.Locale == culture.Name)" value="@culture.Name">@culture.EnglishName</option>
    }
</InputSelect>

I only see the below list which contains ar-SA (arabic-SAUDIARABIA) but not ar-KW (arabic-KUWAIT)

Code Output

I tried installing the language in my Windows 10 machine as shown below but still not appearing even after restart.

Installed Languages in Windows 10

Then I thought I should install it in browser languages. But not able to find arabic-KUWAIT in browser language list and ended up installing just Arabic. Here is what I see in Google Chrome latest version as of writing this.

Installed Languages in Chrome

I'm confused on what I'm missing. Please assist me.

1 Answer 1

1

Seems the CultureTypes.SpecificCultures and CultureTypes.AllCultures are related to Windows or service packs. For more details, you can check this thread: Why does CultureInfo.GetCultures(CultureTypes.SpecificCultures) return different sets of cultures on different computers

And my suggestion is we should Custom CultureInfo, then the web app can be used for global users.

Here is the workaround for you.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace BlazorApp3
{
    public class CustomCulture
    {
        public string Name { get; set; }
        public string EnglishName { get; set; }
        public string NativeName { get; set; }
        public DateTimeFormatInfo DateTimeFormat { get; set; }
        public NumberFormatInfo NumberFormat { get; set; }

        public CustomCulture(string name, string englishName, string nativeName)
        {
            Name = name;
            EnglishName = englishName;
            NativeName = nativeName;
            DateTimeFormat = new DateTimeFormatInfo
            {
                ShortDatePattern = "dd/MM/yyyy",
                LongDatePattern = "dddd, dd MMMM yyyy",
                ShortTimePattern = "HH:mm",
                LongTimePattern = "HH:mm:ss",
                FullDateTimePattern = "dddd, dd MMMM yyyy HH:mm:ss"
            };
            NumberFormat = new NumberFormatInfo
            {
                CurrencySymbol = "KD",
                CurrencyDecimalDigits = 3,
                NumberDecimalDigits = 3
            };
        }
    }

    public static class CustomCultureInfo
    {
        private static readonly List<CustomCulture> CustomCultures = new List<CustomCulture>();

        public static void RegisterCustomCulture()
        {
            var arKWCulture = new CustomCulture("ar-KW", "ar (KW)", "العربية (الكويت)");
            CustomCultures.Add(arKWCulture);
        }

        public static IEnumerable<CustomCulture> GetAllCultures()
        {
            var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                .Select(c => new CustomCulture(c.Name, c.EnglishName, c.NativeName));
            return specificCultures.Concat(CustomCultures).OrderBy(culture => culture.Name);
        }
    }
}

Register it.

using BlazorApp3;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

CustomCultureInfo.RegisterCustomCulture();

await builder.Build().RunAsync();

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.