Ramstack.LocaleAlignment.Generator 2.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ramstack.LocaleAlignment.Generator --version 2.1.0
                    
NuGet\Install-Package Ramstack.LocaleAlignment.Generator -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ramstack.LocaleAlignment.Generator" Version="2.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ramstack.LocaleAlignment.Generator" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Ramstack.LocaleAlignment.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ramstack.LocaleAlignment.Generator --version 2.1.0
                    
#r "nuget: Ramstack.LocaleAlignment.Generator, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ramstack.LocaleAlignment.Generator@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ramstack.LocaleAlignment.Generator&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Ramstack.LocaleAlignment.Generator&version=2.1.0
                    
Install as a Cake Tool

Ramstack.LocaleAlignment.Generator

NuGet MIT

A source generator that aligns .NET culture settings with the POSIX locale categories (LANG, LC_NUMERIC, LC_MONETARY, LC_TIME) on Linux and other Unix-like systems, without any code changes.

screenshot

The .NET column shows the culture settings .NET picks by itself, and the LocaleAlignment column shows them after alignment.

Problem

On Unix-like systems, .NET resolves a single locale the way POSIX resolves LC_MESSAGES (LC_ALL, then LC_MESSAGES, then LANG) and uses it for both CurrentCulture and CurrentUICulture. As a result:

  • LC_NUMERIC, LC_MONETARY, and LC_TIME are ignored
  • LANG is ignored when LC_MESSAGES is set

A common desktop setup, an English interface with the regional formats of another country, therefore gets US number, currency, and date formats in .NET applications, while other applications use the configured formats.

This is a known limitation, discussed in dotnet/runtime#110095.

What this package does

The generator embeds the alignment code into your application and runs it from a module initializer before Main, so the application gets no runtime dependency.

At startup, the locale is resolved as defined by POSIX, separately for each category:

  • LANG defines the base culture. If it is not set, the base culture is the one .NET resolved itself, e.g., from LC_MESSAGES. Unlike POSIX, which uses the C locale in this case, this keeps the formats .NET would use without alignment
  • LC_NUMERIC, LC_MONETARY, and LC_TIME override the number, currency, and date and time formats
  • LC_ALL takes precedence over all of them
  • A value without a language, such as .UTF-8, or one that .NET rejects as a culture name, such as @latin, is ignored as if the variable were not set. As in .NET, a well-formed name, such as xx_YY, is not checked against the ICU data

Locale names are converted to .NET culture names, e.g., sr_RS.UTF-8@latin to sr-Latn-RS.

The resulting culture becomes CurrentCulture and DefaultThreadCurrentCulture. As with customized regional formats on Windows, it keeps the name of the base culture, so en-US with German formats is still en-US. Likewise, dates use a calendar the base culture supports, so en-US with Thai formats uses the Gregorian calendar rather than the Thai Buddhist one.

CurrentUICulture is left as .NET resolved it, so the language of localized resources still follows LC_MESSAGES.

Nothing changes on Windows or in globalization-invariant mode.

Usage

Add the package to your project:

dotnet add package Ramstack.LocaleAlignment.Generator

dotnet add package marks the reference as private, so the generator does not flow to projects that reference yours. If you add the reference manually, mark it the same way:

<PackageReference Include="Ramstack.LocaleAlignment.Generator" Version="..." PrivateAssets="all" />

No other changes are required.

The initializer is generated only for applications (OutputType set to Exe or WinExe). Class libraries are left unchanged, so they never override the culture chosen by the application.

The initializer aligns the culture only when the application is the entry assembly of the process. When another process loads the application as a library, as integration tests with WebApplicationFactory or plugin hosts do, the culture of that process is left unchanged.

The initializer is compiled only for .NET 6 and later, so applications that also target older frameworks, such as .NET Framework, still build.

The generator supports C# only. For other .NET languages, use Ramstack.LocaleAlignment.

Supported versions

.NET 6 and later.

Contributions

Bug reports and contributions are welcome.

License

This package is released as open source under the MIT License.<br /> See the LICENSE file for more details.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.2 81 11/6/2026
2.1.1 105 11/3/2026
2.1.0 111 10/31/2026
2.0.2 381 2/4/2026
2.0.1 149 1/29/2026