PhoenixKitWeb.Gettext (phoenix_kit v2.32.1)

Copy Markdown View Source

A module providing Internationalization with a gettext-based API.

By using Gettext, your module gains a set of macros for translations, for example:

use Gettext, backend: PhoenixKitWeb.Gettext

# Simple translation
gettext("Here is the string to translate")

# Plural translation
ngettext("Here is the string to translate",
         "Here are the strings to translate",
         3)

# Domain-based translation
dgettext("errors", "Here is the error message to translate")

See the Gettext Docs for detailed usage.

This backend does not use Gettext.Backend. That macro compiles each message into a function clause, and ~2.7k messages × 7 translated locales is superlinear in the Erlang compiler (a clean mix compile --force spent ~13s on this file even after split_module_by: [:locale], tripping the ">10s" notice). Translations are parsed from priv/gettext at compile time into a nested map, embedded as a compressed binary, and looked up with Map.get/2. Callers still go through Gettext.dgettext/3 and friends; only the storage changes.

Summary

Functions

Decodes the embedded catalogue into :persistent_term ahead of the first lookup.

Functions

warm_catalog()

@spec warm_catalog() :: :ok

Decodes the embedded catalogue into :persistent_term ahead of the first lookup.

Called from PhoenixKit.Application.start/2. The decode costs ~20ms and :persistent_term.put/2 scans every process, so leaving it to the first gettext call puts both on a random request instead of on boot.