DialectoInContext.Gettext (dialecto_in_context v0.1.0)

Copy Markdown View Source

Marks a Gettext backend's translations for the in-context editor (spec-0017 §1.1). Opt in after use Gettext.Backend, guarded so it compiles away wherever the dev-only dependency is absent:

defmodule MyAppWeb.Gettext do
  use Gettext.Backend, otp_app: :my_app
  if Code.ensure_loaded?(DialectoInContext.Gettext), do: @before_compile(DialectoInContext.Gettext)
end

__before_compile__/1 runs after Gettext's own (hooks run in the order they are registered), makes the generated lgettext/5 and lngettext/7 overridable and wraps them: the original lookup runs (super) — unless a saved draft replaces it — and, only while marking is on, the text is wrapped in a marker whose payload is domain US key US locale RS argsJSON (DialectoInContext.Marker, DialectoInContext.Args). key is the msgid, or msgctxt <> U+0004 <> msgid. Results other than {:ok, _} and {:default, _} (gettext's missing-bindings error) pass through untouched.

With marking off and no drafts, a lookup costs two ETS reads on top of gettext's own.

Summary

Functions

The wrapped lgettext/5 of backend: lookup is the original (super). Called by the generated override; not for direct use.

The wrapped lngettext/7 of backend: lookup is the original (super). Called by the generated override; not for direct use.

The marker key of a message: its msgid, or msgctxt <> U+0004 <> msgid.

The plural form backend uses for n in locale — from the catalog's Plural-Forms when {locale, domain} has one, else the locale's rules; gettext's own fallback (n == 1 ⇒ 0) for a locale its plural module doesn't know.

Functions

__lgettext__(backend, arg, lookup)

The wrapped lgettext/5 of backend: lookup is the original (super). Called by the generated override; not for direct use.

__lngettext__(backend, call, lookup)

The wrapped lngettext/7 of backend: lookup is the original (super). Called by the generated override; not for direct use.

key(msgctxt, msgid)

@spec key(String.t() | nil, String.t()) :: String.t()

The marker key of a message: its msgid, or msgctxt <> U+0004 <> msgid.

plural_form(backend, locale, domain, n)

@spec plural_form(module(), String.t(), String.t(), number()) :: non_neg_integer()

The plural form backend uses for n in locale — from the catalog's Plural-Forms when {locale, domain} has one, else the locale's rules; gettext's own fallback (n == 1 ⇒ 0) for a locale its plural module doesn't know.