Packages

A tool for pseudolocalizing Gettext translations in Elixir applications. Helps identify hardcoded strings, character encoding issues, and UI problems by converting ASCII characters to similar-looking Unicode characters with text expansion simulation.

Current section

Files

Jump to
Raw

lib/plural.ex

defmodule GettextPseudolocalize.Plural do
@moduledoc """
A `Gettext.Plural` implementation that teaches Gettext about the `xx`
pseudo-locale produced by `gettext_pseudolocalize`.
`xx` is not a real CLDR locale, so the default `Gettext.Plural` raises
`Gettext.Plural.UnknownLocaleError` for it. That breaks `mix gettext.merge`,
`mix compile` (once a backend compiles against `priv/gettext/xx/`), and
runtime plural lookups. This module resolves `xx` to two plural forms — the
same shape as English (`nplurals=2; plural=(n != 1)`) — and delegates every
other locale to `Gettext.Plural`, so it is safe to register as the global
pluralizer:
config :gettext, :plural_forms, GettextPseudolocalize.Plural
If you already have a custom pluralizer, add the `"xx"` clauses to it instead
and keep delegating the rest to `Gettext.Plural`.
"""
@behaviour Gettext.Plural
# `Gettext.Plural.init/1` normalises the pluralization context into either the
# bare locale string (when the PO has no `Plural-Forms` header) or a
# `{locale, plural_forms}` tuple (when it does). Both shapes flow into the
# other callbacks, so every clause below accepts both for "xx".
@impl true
def init(context), do: Gettext.Plural.init(context)
@impl true
def nplurals("xx"), do: 2
def nplurals({"xx", _plural_forms}), do: 2
def nplurals(other), do: Gettext.Plural.nplurals(other)
@impl true
def plural("xx", n), do: english_plural(n)
def plural({"xx", _plural_forms}, n), do: english_plural(n)
def plural(other, n), do: Gettext.Plural.plural(other, n)
@impl true
def plural_forms_header("xx"), do: "nplurals=2; plural=(n != 1);"
def plural_forms_header(other), do: Gettext.Plural.plural_forms_header(other)
# English-style: singular for exactly one, plural otherwise.
defp english_plural(1), do: 0
defp english_plural(_), do: 1
end