GettextSigils.Modifier behaviour (Gettext Sigils v0.5.1)

Copy Markdown View Source

Behaviour for ~t sigil modifiers.

Implement this behaviour to extend the ~t sigil with per-call transformations: validate opts (init/1), compute the Gettext domain/context (domain_context/3), rewrite the message at compile time (preprocess/2), turn it into a plural form (pluralize/2), or transform the final translated string at runtime (postprocess/2).

All callbacks are optional. use GettextSigils.Modifier installs no-op defaults; override only what you need:

defmodule MyApp.UpcaseModifier do
  use GettextSigils.Modifier

  @impl true
  def postprocess(string, _opts), do: {:ok, String.upcase(string)}
end

Wire it up via the :modifiers option:

use GettextSigils,
  backend: MyApp.Gettext,
  sigils: [
    modifiers: [
      u: MyApp.UpcaseModifier,
      # with opts:
      s: {MyApp.ShoutModifier, intensity: 3}
    ]
  ]

Every callback returns {:ok, value} or {:error, reason}, where reason is either a string or any exception struct (e.g. a NimbleOptions.ValidationError). Exception structs are normalized to strings via Exception.message/1. init/1 errors raise a NimbleOptions.ValidationError at use GettextSigils time; all other errors raise ArgumentError at the respective compile or runtime stage.

See the Modifiers guide for full callback semantics, chaining rules, timing, and examples.

Summary

Types

bindings()

@type bindings() :: keyword()

context()

@type context() :: binary() | nil

domain()

@type domain() :: binary() | :default | nil

domain_context()

@type domain_context() :: {domain(), context()}

input()

@type input() :: {msgid(), bindings()}

msgid()

@type msgid() :: binary()

opts()

@type opts() :: keyword()

pluralized()

@type pluralized() :: input() | {msgid(), msgid(), Macro.t(), bindings()}

reason()

@type reason() :: String.t() | Exception.t()

result(value)

@type result(value) :: {:ok, value} | {:error, reason()}

Callbacks

domain_context(input, opts, domain_context)

@callback domain_context(input(), opts(), domain_context()) :: result(domain_context())

init(opts)

@callback init(opts()) :: result(opts())

pluralize(input, opts)

@callback pluralize(input(), opts()) :: result(pluralized())

postprocess(t, opts)

@callback postprocess(String.t(), opts()) :: result(term())

preprocess(input, opts)

@callback preprocess(input(), opts()) :: result(input())