View Source Expo.PluralForms (expo v0.1.0-beta.6)

Gettext Plural Helper

https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html

Link to this section Summary

Functions

Compile plural forms so that it returns the index

Convert parsed plural form to string

Get Index from PluralForms Header

Get known locales where plural form information is available

Parse Plural Forms Header

Parse Plural Forms Header

Get Plural Forms for language

Link to this section Types

@type parse_error() ::
  {:error,
   {:parse_error, message :: String.t(), line :: pos_integer(),
    offset :: pos_integer()}}
@type plural() ::
  :n
  | integer()
  | {:!= | :> | :< | :== | :% | :<= | :>= | :&& | :||, plural(), plural()}
  | {:if, plural(), plural(), plural()}
  | {:paren, plural()}
@type t() :: {nplurals :: pos_integer(), plural :: plural()}

Link to this section Functions

Link to this function

compile_index(plural_forms)

View Source
@spec compile_index(plural_forms :: plural()) :: Macro.t()

Compile plural forms so that it returns the index

bindings

Bindings

  • n - the number to get the index for

usage

Usage

defmodule MyModule do
  {:ok, {2, plurals}} = Expo.PluralForms.parse_string("nplurals=2; plural=n>1;")
  def index(n), do: unquote(Expo.PluralForms.compile_index(plurals))
end
@spec compose(plural_forms :: t()) :: iodata()

Convert parsed plural form to string

@spec index(plural_forms :: plural(), n :: non_neg_integer()) :: non_neg_integer()

Get Index from PluralForms Header

examples

Examples

iex> {:ok, {2, plurals}} = Expo.PluralForms.parse_string("nplurals=2; plural=n != 1;")
iex> Expo.PluralForms.index(plurals, 4)
1
@spec known_locales() :: [String.t()]

Get known locales where plural form information is available

examples

Examples

iex> "de" in Expo.PluralForms.known_locales()
true

iex> "invalid" in Expo.PluralForms.known_locales()
false
@spec parse_string!(content :: String.t()) :: t() | no_return()

Parse Plural Forms Header

Works exactly like parse_string/1, but returns a plural forms tuple if there are no errors or raises a Expo.PluralForms.SyntaxError error if there are.

examples

Examples

iex> Expo.PluralForms.parse_string!("nplurals=2; plural=n != 1;")
{2, {:!=, :n, 1}}

iex> Expo.PluralForms.parse_string!("invalid")
** (Expo.PluralForms.SyntaxError) 1:0 expected string "nplurals="
@spec parse_string(content :: String.t()) :: {:ok, t()} | parse_error()

Parse Plural Forms Header

examples

Examples

iex> Expo.PluralForms.parse_string("nplurals=2; plural=n != 1;")
{:ok, {2, {:!=, :n, 1}}}
Link to this function

plural_form(iso_language_tag)

View Source
@spec plural_form(iso_language_tag :: String.t()) :: {:ok, t()} | :error

Get Plural Forms for language

examples

Examples

iex> Expo.PluralForms.plural_form("de")
{:ok, {2, {:paren, {:!=, :n, 1}}}}

iex> Expo.PluralForms.plural_form("invalid")
:error