CultureNotFoundException when setting culture to "pl-PL" in .NET 8 application

28 Views Asked by At

I'm developing an application with .NET 8 and attempting to set the default culture for my application threads to Polish ("pl-PL"). However, I encounter a CultureNotFoundException indicating that only the invariant culture is supported in globalization-invariant mode, and "pl-PL" is considered an invalid culture identifier. The exception is thrown when configuring the service to set the culture. Below is the relevant code snippet:

using System.Globalization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

namespace Hangfire.Web.Configurations.Services;

public static class Culture
{
    public static void ConfigureService(IServiceCollection services, IConfiguration configuration)
    {
        // Attempt to set the culture for threads to "pl-PL"
        var cultureInfo = new CultureInfo("pl-PL");

        CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
        CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
    }
}

The exception details are as follows:


System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name') pl-PL is an invalid culture identifier.
   at System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride)
   ...

I made sure to set the DOTNET_SYSTEM_GLOBALIZATION_INVARIANT environment variable to "0" in my launchSettings.json to enable culture-specific operations:

{
  "profiles": {
    "http": {
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT": "0"
      }
    },
    "https": {
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT": "0"
      }
    }
  }
}

Despite this configuration, the application still throws a CultureNotFoundException when attempting to set the culture to "pl-PL". I'm looking for assistance to understand why this exception is being thrown and how to successfully set the application culture to "pl-PL" in a .NET 8 environment. Is there an additional configuration step I'm missing, or could this be an issue specific to .NET 8?

0

There are 0 best solutions below