Cldr.Gettext.Interpolation (Cldr Messages v0.13.2) View Source

As of Gettext 0.19, Gettext supports user-defined interpolation modules. This makes it easy to combine the power of ICU message formats with the broad gettext ecosystem and the inbuilt support for gettext in Phoenix.

The documentation for Gettext should be followed with considerations in mind:

  1. A Gettext backend module should use the :interpolation option defined referring to the ex_cldr_messages backend you have defined.
  2. The message format is in the ICU message format (instead of the Gettext format).

Defining a Gettext Interpolation Module

Any ex_cldr backend module that has a Cldr.Message provider configured can be used as an interpolation module. Here is an example:

# CLDR backend module
defmodule MyApp.Cldr do
  use Cldr,
    locales: ["en", "fr", "ja", "he", "th", "ar"],
    default_locale: "en",
    providers: [Cldr.Number, Cldr.DateTime, Cldr.Unit, Cldr.List, Cldr.Calendar, Cldr.Message],
    gettext: MyApp.Gettext,
    message_formats: %{
      USD: [format: :long]

# Define an interpolation module for ICU messages
defmodule MyApp.Gettext.Interpolation do
  use Cldr.Gettext.Interpolation, cldr_backend: MyApp.Cldr


# Define a gettext module with ICU message interpolation
defmodule MyApp.Gettext do
  use Gettext, otp_app: :ex_cldr_messages, interpolation: MyApp.Gettext.Interpolation

Now you can proceed to use Gettext in the normal manner, most typically with the gettext/3 macro.