Quick update: I just released v0.5.0 of this library, with support for custom modifiers!
In addition to the existing domain/context modifier keyword options, it is now possible to define custom modifiers as modules that implement the GettextSigils.Modifier behaviour.
For example, custom modifiers can be used to postprocess the translated message:
defmodule MyApp.MarkdownModifier do
use GettextSigils.Modifier
@impl true
def postprocess(string, opts),
do: MDEx.to_html(string, opts)
end
defmodule MyApp.RawModifier do
use GettextSigils.Modifier
@impl true
def postprocess(string, _opts) do
{:ok, Phoenix.HTML.raw(string)}
end
end
The behaviour can also be used to implement custom pluralization rules (I recommend sticking with the default N modifier, though).
defmodule MyApp.SplitPluralModifier do
use GettextSigils.Modifier
@schema NimbleOptions.new!(
separator: [
type: :string,
default: "|",
doc: "Separator between singular and plural forms in the msgid."
]
)
@impl true
def init(opts), do: NimbleOptions.validate(opts, @schema)
@impl true
def pluralize({msgid, bindings}, opts) do
case Keyword.pop(bindings, :count) do
{nil, _} ->
{:error, ~s|`n` modifier requires a "count" binding|}
{count, remaining} ->
separator = Keyword.fetch!(opts, :separator)
case String.split(msgid, separator, parts: 2) do
[singular, plural] ->
{:ok, {singular, plural, count, remaining}}
_ ->
{:error, ~s|`n` modifier requires msgid in the form "singular#{separator}plural"|}
end
end
end
end
Then configure GettextSigils to use the modifiers:
use GettextSigils,
backend: MyApp.Gettext,
sigils: [
modifiers: [
e: [domain: "errors"],
r: MyApp.RawModifier,
m: {MyApp.MarkdownModifier, extension: [strikethrough: true]},
n: MyApp.SplitPluralModifier
]
]
~t"**One** error|**#{count}** errors"mrn
# => {:safe, "<p><strong>5</strong> errors</p>"}
Thanks to @ErikNaslund for opening an issue that eventually lead to this! 