Culture and global configuration
Orientation
Humanizer chooses language and grammar from one of three places:
- A
CultureInfopassed to the operation. CultureInfo.CurrentCultureorCurrentUICulture, depending on the API.- A configured registry fallback when the requested culture has no matching component.
Per-call culture is the narrowest and safest choice. Ambient culture is useful when a request pipeline already establishes it. Global Configurator changes are process-wide policy.
Example
The example passes French directly to ToWords, establishes French ambient culture for APIs that need it, and keeps the custom transformer local:
using System.Globalization;
using Humanizer;
var french = CultureInfo.GetCultureInfo("fr-FR");
CultureInfo.CurrentCulture = french;
CultureInfo.CurrentUICulture = french;
AssertEqual("quarante-deux", 42.ToWords(french));
AssertEqual("BONJOUR", "bonjour".Transform(french, new LoudTransformer()));
Console.WriteLine("quarante-deux; BONJOUR");
static void AssertEqual(string expected, string actual)
{
if (actual != expected)
throw new InvalidOperationException($"Expected '{expected}', got '{actual}'.");
}
sealed class LoudTransformer : ICulturedStringTransformer
{
public string Transform(string input) =>
Transform(input, CultureInfo.CurrentCulture);
public string Transform(string input, CultureInfo culture) =>
culture.TextInfo.ToUpper(input);
}
That separation is intentional: select language close to the call, and reserve global configuration for behavior that truly applies to the entire application.
Registry behavior in this release
This 2.x release keeps LocaliserRegistry<TLocaliser> registrations mutable.
Changing a process-wide registry after requests have begun can therefore make
later work observe different components. Register custom components once during
startup even though the API does not freeze the table.
Date humanization strategies are mutable properties under the 2.x strategy
namespaces. Enum description-property selection is also assignable through
Configurator.EnumDescriptionPropertyLocator; set both policies during
startup.
Pitfall
Setting only CurrentCulture is not always enough. Collection and enum localization can depend on CurrentUICulture, while numeric formatting commonly follows CurrentCulture. Set both at a well-defined boundary when you need consistent ambient behavior.
Version notes
The selected package uses the mutable 2.x configuration model and the
Humanizer.Configuration namespace. Treat its generated API reference as the
authority for exact strategy and registry type names.