AshGettext.Locale.Normalizer (AshGettext v0.1.0)

Copy Markdown View Source

Reduce a locale as it arrives in the wild to one the catalogs actually have.

Clients send ru_RU, en-US, ru, or nothing at all. Catalogs are keyed by the language subtag. This module is the single place that reconciles the two, so no call site has to decide what en_KZ means.

AshGettext.Info.language/2 runs it on what the resolver returns. It runs on every translated error, so it does no I/O and keeps no state. A language the backend does not ship falls back to the default locale.

Summary

Functions

Strip the region and script from locale, leaving the language subtag.

Reduce locale to a supported locale, or default when it maps to none.

Functions

language_code(locale)

@spec language_code(String.t()) :: String.t()

Strip the region and script from locale, leaving the language subtag.

iex> AshGettext.Locale.Normalizer.language_code("en-US")
"en"

normalize(locale, supported, default)

@spec normalize(String.t() | nil, [String.t()], String.t()) :: String.t()

Reduce locale to a supported locale, or default when it maps to none.

The region and script are dropped, so "ru_RU" and "ru-Cyrl" both become "ru". A nil locale returns default. The result is always a member of supported or default itself.

iex> AshGettext.Locale.Normalizer.normalize("ru_RU", ["en", "ru"], "en")
"ru"

iex> AshGettext.Locale.Normalizer.normalize("de", ["en", "ru"], "en")
"en"

iex> AshGettext.Locale.Normalizer.normalize(nil, ["en", "ru"], "en")
"en"