Localize.Gettext.Interpolation (Localize v1.8.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!"

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

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

Functions

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}