---
title: Localization
editUrl: https://github.com/Cratis/Arc.Elixir/edit/main/Documentation/localization.md
description: Enable UI language negotiation and message catalogs for Arc for Elixir, so requests, streams, and background work all resolve text in the caller's language.
lastUpdated: 2026-12-05T00:00:00.000Z
---

<!-- Copyright (c) Cratis. All rights reserved. -->
<!-- Licensed under the MIT license. See LICENSE file in the project root for full license information. -->

Configure a UI language policy to have Arc negotiate the language for commands, queries, and observable streams. Without this policy, Arc does not negotiate a language, add localization headers, or enrich framework validation findings. UI language does not change formatting, serialization, identity, or stored data.

## Configure a host

Set `localization:` on your [Arc application](/arc/backend/elixir/configuration/). Use an ordered list of distinct BCP 47 base tags and a default from that list. Tags are case-insensitive; Arc returns the canonical casing of the supported tag. Use `aliases:` to map tags explicitly, for example `%{"no" => "nb"}`. Arc does not equate `no`, `nb`, or `nn` automatically.

```elixir
config :my_app, MyApp.Arc,
  localization: [languages: ["en", "fr", "fr-CA"], default: "en"],
  message_catalogs: [
    %{"en" => %{"welcome" => "Welcome"}, "fr" => %{"welcome" => "Bienvenue"}}
  ]
```

Arc keeps the initial policy and catalogs for the life of the BEAM instance. Refreshing Arc does not reload them; restart the host process to apply configuration changes.

The default provider is `:accept_language`. It honors quality weights, exclusions, and `*`. To opt into query or cookie selection, set `providers:` in priority order, for example `[:query_string, :cookie, :accept_language]`. The query parameter is `ui-culture`. The `.AspNetCore.Culture` cookie uses `c=en|uic=fr`; Arc uses `c` only if `uic` is empty. On SSE and WebSocket handshakes, a single valid `_cratisUiCulture` query value takes precedence and is not bound to an observable query argument. SSE control requests cannot select the subscription's language; it comes from the stream handshake.

Arc sends `Content-Language` on localized responses, including errors and stream handshakes. The header provider adds `Accept-Language` to `Vary`. The cookie provider adds `Cookie` to `Vary` and sets `Cache-Control: no-store, private`, even when the request has no culture cookie. The query provider does not add either header.

## Read the language or a message

`Cratis.Arc.Localization.ui_language/1` reads the language from a Plug connection (`conn.assigns.arc_ui_language`) or a `Cratis.Arc.Context`. `message/2` returns `{:ok, text}` or `:error` for an unregistered ID. It uses the context's tag with the host matching policy, then the default. For example, a `fr-CA` request can use a `fr` message and fall back to `en` when neither has the ID. Without a policy, lookup uses the explicit context tag only. Arc validates and registers all catalogs together when initializing the application; duplicate language/ID pairs are errors.

## Render for a background recipient

Look up the recipient's preferred language from their profile and pass it explicitly into each job. Do not capture the request connection or reuse the initiating user's language. `with_ui_language/2` returns a new context and does not enable framework descriptors on an unconfigured host.

```elixir
alias Cratis.Arc.Localization

Task.async(fn ->
  ctx = MyApp.Arc |> Cratis.Arc.context() |> Localization.with_ui_language(recipient.language)
  Localization.message(ctx, "welcome")
end)
```

If the host uses Gettext, wrap translation calls and Ecto error interpolation in `Gettext.with_locale/3` inside the job. Gettext's process-local locale does not follow the task automatically. Supply the host's Gettext backend and the recipient's configured locale as a string; never convert input language tags to atoms.

## Return validation feedback

Arc keeps English framework fallback text in `message`. Localized hosts include stable `reason` and `reasonDetail` values such as `rule` / `Required`, and named `messageArguments` where a message takes arguments. Application validators can provide their own ID and arguments using `Cratis.Arc.Validation.error/3` or `warning/3`:

```elixir
Cratis.Arc.Validation.error("The name is too long.", [:name],
  reason_detail: "NameTooLong",
  message_arguments: %{"max" => 48}
)
```

Arguments have string keys and string, boolean, or finite number values within JavaScript's safe integer range. Invalid entries are omitted. Only pass values safe to show to the caller, not raw input, secrets, or diagnostic values. Arc provides no translation packs and does not interpret message template syntax.
