Localize.Gettext.Interpolation (Localize v1.10.0)

Copy Markdown View Source

Gettext interpolation module for ICU MessageFormat 2 (MF2) messages.

Implements the Gettext.Interpolation behaviour so that MF2 messages can be used in Gettext .po/.pot files. At compile time, messages are parsed into an AST for efficient runtime formatting. At runtime, bindings are interpolated using Localize.Message.

Defining a Gettext module with MF2 interpolation

defmodule MyApp.Gettext do
  use Gettext.Backend,
    otp_app: :my_app,
    interpolation: Localize.Gettext.Interpolation
end

Now Gettext macros will parse and format MF2 messages:

import MyApp.Gettext

gettext("{{Hello {$name}!}}", %{name: "World"})
#=> "Hello \u2068World\u2069!"

Gettext's own placeholders

Ecto, Ash and Phoenix write their messages for Gettext's default interpolation, such as Ecto's "should be at least %{count} character(s)", and an application's errors.po translates them in the same syntax. This interpolator reads %{name} as the MF2 variable {$name}, so those messages interpolate in a backend whose own messages are MF2. See mf2_source/1.

Bidirectional isolation

Messages are formatted with TR35's Default Bidi Strategy, the default of Localize.Message.format/3: a placeholder that may not have the message's direction is wrapped in Unicode bidi isolates (U+2066 to U+2069), so that a right-to-left value in a left-to-right message, or the reverse, displays correctly.

Summary

Functions

Returns a Gettext message as MF2 source, reading Gettext's own %{name} placeholder as the MF2 variable {$name}.

Sentinel bindings that cause runtime_interpolate/2 and do_interpolate/2 to return the translated msgid unchanged, bypassing MF2 evaluation entirely.

Functions

mf2_source(message)

@spec mf2_source(String.t()) :: String.t()

Returns a Gettext message as MF2 source, reading Gettext's own %{name} placeholder as the MF2 variable {$name}.

Ecto, Ash and Phoenix write their messages for Gettext's default interpolation, such as Ecto's "should be at least %{count} character(s)", and an application's errors.po translates them in the same syntax. In MF2, %{count} is the text % followed by the literal count, so without this such a message would format as "should be at least %count character(s)". Localize.Message.format/3 keeps MF2's reading; this interpolator, which serves Gettext, reads Gettext's.

  • A message in MF2's complex form, or a quoted pattern {{…}}, has each %{name} replaced by {$name}.

  • Any other message with a %{name} placeholder is read as Gettext text: its \, { and } are escaped and its placeholders become variables. It is written as a simple message, or quoted as a pattern {{…}} where it begins with whitespace or ., which a simple message cannot, so its text is kept exactly. A placeholder whose name MF2 cannot take as a variable name, such as %{1x}, is kept as text.

  • A message with no %{name} placeholder, or one that is not valid UTF-8, is returned as it is.

The variables are formatted as the MF2 messages of the backend are, with bidi isolation by default.

Arguments

  • message is a Gettext msgid or msgstr.

Returns

  • The message as MF2 source.

Examples

iex> Localize.Gettext.Interpolation.mf2_source("should be at least %{count} character(s)")
"should be at least {$count} character(s)"

iex> Localize.Gettext.Interpolation.mf2_source("{{Hello {$name}!}}")
"{{Hello {$name}!}}"

iex> Localize.Gettext.Interpolation.mf2_source("must be one of {a, b}: %{value}")
"must be one of \\{a, b\\}: {$value}"

iex> Localize.Gettext.Interpolation.mf2_source(".po file %{name} is missing")
"{{.po file {$name} is missing}}"

skip_interpolation_sentinel()

@spec skip_interpolation_sentinel() :: %{__localize_skip_interpolation__: true}

Sentinel bindings that cause runtime_interpolate/2 and do_interpolate/2 to return the translated msgid unchanged, bypassing MF2 evaluation entirely.

Used by Localize.HTML.t/1 (and any caller that needs the raw translated MF2 source so it can run format_to_safe_list/3 itself for markup-aware rendering). Not intended for direct use by application code.

Returns

  • The sentinel map %{__localize_skip_interpolation__: true}.

Examples

iex> Localize.Gettext.Interpolation.skip_interpolation_sentinel()
%{__localize_skip_interpolation__: true}