Localize

Build statusHex.pmHex.pmHex.pmHex.pm

Locale-aware formatting, validation, and data access for Elixir, built on the Unicode CLDR repository.

Localize consolidates the functionality of the ex_cldr_* library family into a single package. No compile-time backend modules or code generation is required — all CLDR data is loaded at runtime and cached in :persistent_term.

Try it without installing anything at the Localize playground.

Features

Claude Code skill

Localize ships a Claude Code skill that teaches Claude the library's APIs and localization-first patterns — every example execution-verified against the library. With the skill installed, Claude writes plural-correct, currency-correct, collation-correct Elixir by default, even for single-locale applications.

/plugin marketplace add elixir-localize/localize
/plugin install localize@localize

The skill source lives in skills/localize.

MCP server

The companion localize_mcp package is a Model Context Protocol server that lets any MCP host — Claude Code, Claude Desktop, Codex, Zed — search, browse, and invoke the Localize API through eleven typed tools, so agents discover the right function, options, and atom forms without grepping source. Add {:localize_mcp, "~> 0.1", only: :dev} to your project and run claude mcp add localize -- mix localize_mcp; the host configuration guide covers the other hosts.

Supported Elixir and OTP versions

Localize requires Elixir 1.17+ and Erlang/OTP 26+.

On OTP 26, also add the json_polyfill package — it provides the OTP 27+ :json module that Localize uses for JSON decoding. OTP 27 and later need no extra dependency.

Installation

Add localize to your dependencies in mix.exs:

def deps do
[
{:localize, "~> 1.0"}
]
end

On OTP 26 only:

def deps do
[
{:localize, "~> 1.0"},
{:json_polyfill, "~> 0.2 or ~> 1.0"}
]
end

By default the :localize OTP application starts automatically and brings up its own supervision tree. Applications that prefer to mount Localize under their own supervisor can do so by marking the dependency runtime: false and adding Localize.Supervisor to their children list — see the Supervision guide.

Quick start

iex> # Numbers
iex> Localize.Number.to_string(1_234_567.89)
{:ok, "1,234,567.89"}
iex> Localize.Number.to_string(0.456, format: :percent)
{:ok, "46%"}
iex> # Dates
iex> Localize.Date.to_string(~D[2025-03-22])
{:ok, "Mar 22, 2025"}
iex> Localize.Date.to_string(~D[2025-03-22], format: :long)
{:ok, "March 22, 2025"}
iex> # Units
iex> Localize.Unit.to_string(Localize.Unit.new!(3.5, "kilometer"))
{:ok, "3.5 kilometers"}
iex> # Lists
iex> Localize.List.to_string(["apple", "banana", "cherry"])
{:ok, "apple, banana, and cherry"}
iex> # Territories and languages
iex> Localize.Territory.display_name(:US)
{:ok, "United States"}
iex> Localize.Language.display_name(:fr)
{:ok, "French"}
iex> # Collation
iex> Localize.Collation.sort(["banana", "apple", "Cherry"])
["apple", "banana", "Cherry"]

Locale management

Localize maintains a per-process current locale and an application-wide default:

iex> # Get the current locale (defaults to :en)
iex> Localize.get_locale()
iex> # Set the process locale
iex> Localize.put_locale(:de)
iex> # Temporarily use a different locale
iex> Localize.with_locale(:ja, fn ->
...> Localize.Number.to_string(1234)
...> end)
{:ok, "1,234"}

The default locale is resolved from (in order):

  1. LOCALIZE_DEFAULT_LOCALE environment variable.
  2. config :localize, default_locale: :fr in application config.
  3. LANG environment variable.
  4. :en as a final fallback.

All formatting functions default their :locale option to Localize.get_locale() when no locale is explicitly passed.

Configuration

Localize requires no compile-time configuration. All options are set in your application config and take effect at runtime. It is also perfectly reasonable to have no configuration, at least when you are just exploring the library. The :en locale is always installed so that will be used for formatting and parsing until you add some configuration.

config :localize,
default_locale: :fr,
supported_locales: [:en, :fr, :de, :ja, :es, "zh-*"],
locale_provider: MyApp.LocaleProvider,
locale_cache_max_entries: 2_000,
format_cache_max_entries: 5_000,
otp_app: :my_app,
nif: true,
cacertfile: "/path/to/cacerts.pem",
https_proxy: "http://proxy.example.com:8080"

Configuring the locale cache directory

Where Localize writes downloaded locale ETF files is controlled by two application-environment keys, :otp_app and :locale_cache_dir. There are three supported forms — pick the one that matches your situation:

1. :otp_app only (recommended). Caches under your app's runtime priv/ directory at the conventional subpath:

config :localize, otp_app: :my_app
# → Application.app_dir(:my_app, "priv/localize/locales")

2. :otp_app + a relative :locale_cache_dir. Same app-anchored resolution, but you choose the subpath:

config :localize,
otp_app: :my_app,
locale_cache_dir: "priv/i18n/cache"
# → Application.app_dir(:my_app, "priv/i18n/cache")

3. An absolute :locale_cache_dir. Used verbatim — :otp_app is ignored. Use this for a shared mount or a fixed system path:

config :localize, locale_cache_dir: "/var/lib/localize/locales"

Why :otp_app is the recommended anchor: Application.app_dir/2 is re-resolved on every read, so a single config value works correctly in every runtime phase — mix tasks land files in _build/<env>/lib/<app>/priv/..., mix test reads the same path, and releases read from /path/to/release/lib/<app>-X.Y.Z/priv/.... No config/runtime.exs duplication is needed.

Why a bare relative :locale_cache_dir is refused. A relative path without an :otp_app anchor resolves against the BEAM's current working directory, which differs between mix tasks (project root), mix test, and a release (release root) — one value cannot be correct in all phases. If you set a relative :locale_cache_dir without :otp_app, Localize raises Localize.LocaleCacheDirError at app start. Fix it by pairing the relative path with :otp_app (form 2 above) or by switching to an absolute path (form 3).

Using Gettext locales

If your application uses Gettext, you can derive :supported_locales from your Gettext backend in config/runtime.exs (where the module is already compiled and available):

# config/runtime.exs
config :localize,
supported_locales: Gettext.known_locales(MyApp.Gettext)

POSIX-style locale names returned by Gettext (e.g. "pt_BR", "zh_Hans") are automatically normalized to BCP 47 and resolved to their CLDR canonical form (:pt, :zh). No manual mapping is needed.

Pre-populating the locale cache

Use mix localize.download_locales at build time to download locale data into the on-disk cache. By default it downloads the configured :supported_locales:

# Dockerfile
RUN mix localize.download_locales

Specific locales can also be downloaded explicitly: mix localize.download_locales en fr de. Use --all for all CLDR locales. Locale data is loaded lazily into :persistent_term on first access from the cache.

When :supported_locales is not configured (the default), validate_locale/1 matches against all ~766 CLDR locales.

Environment variables

The following environment variables influence Localize behaviour.

Runtime

VariableDescription
LOCALIZE_DEFAULT_LOCALESets the application-wide default locale (e.g., en-AU, ja). Takes precedence over the LANG variable and the :default_locale application config. Evaluated once on first call to Localize.get_locale/0 or Localize.default_locale/0.
LANGStandard POSIX locale variable (e.g., en_US.UTF-8). Used as a fallback when LOCALIZE_DEFAULT_LOCALE is not set and no :default_locale is configured. The value is converted from POSIX format (underscores replaced with hyphens, encoding suffix stripped).
LOCALIZE_UNSAFE_HTTPSWhen set to a truthy value, disables SSL certificate verification for HTTPS connections (e.g., locale data downloads). The values nil, NIL, false, FALSE, an empty string, or unset all keep verification enabled. Intended for development behind corporate proxies with self-signed certificates. Do not use in production.
LOCALIZE_HTTP_TIMEOUTHTTP request timeout in milliseconds for locale data downloads. Overrides the default timeout.
LOCALIZE_HTTP_CONNECTION_TIMEOUTHTTP connection timeout in milliseconds for locale data downloads. Overrides the default connection timeout.
HTTPS_PROXY / https_proxyHTTPS proxy URL for outbound connections. Also configurable via the :https_proxy application config key.

Compile time

VariableDescription
LOCALIZE_NIFSet to true to compile the optional NIF extension (e.g., LOCALIZE_NIF=true mix compile). Enables ICU4C-based Unicode normalisation, collation sort-key generation, and number/message formatting. Can also be enabled with config :localize, nif: true.

Default locale resolution order

When Localize.get_locale/0 is called and no process-level locale has been set, the default locale is resolved in this order:

  1. LOCALIZE_DEFAULT_LOCALE environment variable.

  2. :default_locale application config (config :localize, default_locale: :fr).

  3. LANG environment variable (POSIX format converted to BCP 47).

  4. :en as the final fallback.

The resolved locale is cached in :persistent_term after first resolution so this lookup happens only once per BEAM lifetime.

Optional NIF Backend

Localize includes an optional NIF backend powered by ICU4C. When enabled, specific functions can use the NIF for formatting by passing backend: :nif. The default backend is always :elixir — no NIF is required.

Function:backend optionNIF implementation
Localize.Number.to_string/2backend: :nifICU4C NumberFormatter
Localize.Unit.to_string/2backend: :nifICU4C NumberFormatter (unit)
Localize.Number.PluralRule.plural_type/2backend: :nifICU4C PluralRules
Localize.Message.format/3backend: :nifICU4C MessageFormat 2
Localize.Collation.compare/3backend: :nifICU4C Collator

If :nif is specified but the NIF is not compiled or not available, it silently falls back to the pure Elixir implementation. See the Performance Guide for benchmarks and guidance.

Documentation

Full documentation is available on HexDocs.

Migrating from ex_cldr

If you are migrating from the ex_cldr family of libraries, see the Migration Guide for a detailed walkthrough of configuration changes, API differences, and upgrade steps.

Additional Localize libraries

Localize is the core CLDR-backed formatting and validation library. The following companion packages build on top of it and cover domains that fall outside the core CLDR data model:

Supplemental localization libraries

Form input components

Locale-aware Phoenix LiveView inputs, so a user can enter a value under their own conventions rather than fighting a browser control. These are the newest part of the family and still 0.1.x. The inputs playground demonstrates them live against any locale.

MessageFormat 2 tooling

Libraries that depend on Localize

Acknowledgements

License

Apache License 2.0, together with the Unicode License v3 for the CLDR and UCD data embedded in the package. See the LICENSE file for details, including which Unicode data is used and what it becomes.