Gettext interpolation module that supports both ICU MessageFormat v1 and MessageFormat 2 (MF2) messages.
Messages are compiled as V2 first; if the V2 parser fails, the V1
parser is tried as a fallback. At runtime, Cldr.Message.format/3
auto-detects the version. This allows the same .POT files to
contain both v1 and v2 messages, all tagged with icu-format.
Defining a Gettext Interpolation Module
# CLDR backend module
defmodule MyApp.Cldr do
use Cldr,
locales: ["en", "fr", "ja"],
default_locale: "en",
providers: [Cldr.Number, Cldr.Message],
gettext: MyApp.Gettext
end
# Define an interpolation module that handles both v1 and v2
defmodule MyApp.Gettext.Interpolation do
use Cldr.Gettext.Interpolation.V2, cldr_backend: MyApp.Cldr
end
# Define a gettext module
defmodule MyApp.Gettext do
use Gettext, otp_app: :my_app, interpolation: MyApp.Gettext.Interpolation
endNow you can use Gettext with both message formats:
# V1 message
gettext("{greeting} world!", greeting: "Hello")
#=> "Hello world!"
# V2 message (auto-detected by leading `{{`)
gettext("{{Hello {$name}!}}", %{"name" => "World"})
#=> "Hello World!"