Ramstack.LocaleAlignment.Generator
2.1.2
dotnet add package Ramstack.LocaleAlignment.Generator --version 2.1.2
NuGet\Install-Package Ramstack.LocaleAlignment.Generator -Version 2.1.2
<PackageReference Include="Ramstack.LocaleAlignment.Generator" Version="2.1.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Ramstack.LocaleAlignment.Generator" Version="2.1.2" />
<PackageReference Include="Ramstack.LocaleAlignment.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Ramstack.LocaleAlignment.Generator --version 2.1.2
#r "nuget: Ramstack.LocaleAlignment.Generator, 2.1.2"
#:package Ramstack.LocaleAlignment.Generator@2.1.2
#addin nuget:?package=Ramstack.LocaleAlignment.Generator&version=2.1.2
#tool nuget:?package=Ramstack.LocaleAlignment.Generator&version=2.1.2
Ramstack.LocaleAlignment.Generator
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.

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, andLC_TIMEare ignoredLANGis ignored whenLC_MESSAGESis set
For example, with LC_MESSAGES=en_US.UTF-8 and LANG=de_DE.UTF-8,
.NET applications use US number, currency, and date formats, while other applications use German 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,
without adding a runtime dependency.
At startup, the locale is resolved as defined by POSIX, separately for each category:
LANGdefines the base culture. If it is not set, the base culture is the one .NET resolved itself, e.g., fromLC_MESSAGES. Unlike POSIX, which uses the C locale in this case, this keeps the formats .NET would use without alignmentLC_NUMERIC,LC_MONETARY, andLC_TIMEoverride the number, currency, and date and time formatsLC_ALLtakes precedence over all of them. When it is set, alignment is skipped, leaving the culture as .NET resolved it- In
LANG,LC_NUMERIC,LC_MONETARY, andLC_TIME, 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 asxx_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 unchanged.
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, with C# 10 or later. With earlier C# versions, nothing is generated: the application still builds, but the culture is not aligned.
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.
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.