View Source Bonfire.Common.Localise.Gettext.Helpers (Bonfire v0.9.10-classic-beta.169)
A module providing Internationalization with a gettext-based API.
By using Gettext, your module gains a set of macros for translations, for example:
Simple translation
iex> l("Hello")
"Hello"
iex> l("Hello %{name}", name: "Bookchin")
"Hello Bookchin"
iex> l("Hi", [], "test context")
"Hi"
Plural translation
iex> lp("Hi friend", "Hi friends", 2)
"Hi friends"
iex> lp("Hiya %{user_or_users}", "Hiyas %{user_or_users}", 1, [user_or_users: "Bookchin"], "test context")
"Hiya Bookchin"
See the Gettext Docs for details.
Summary
Functions
Translates a string with optional bindings, context, and domain.
Dynamically localises a text. This function is useful for localising strings only known at runtime (when you can't use the l
or lp
macros).
Localizes a list of strings at compile time.
Translates a plural text with optional bindings, context, and domain.
Functions
l(original_text_or_id, bindings \\ [], context \\ nil, domain \\ nil)
View Source (macro)Translates a string with optional bindings, context, and domain.
This macro provides translation capabilities based on Gettext. It determines the appropriate domain and context for the translation.
Examples
iex> l("Hello")
"Hello"
iex> l("Hello %{name}", name: "Bookchin")
"Hello Bookchin"
iex> l("Hi", [], "test context")
"Hi"
Parameters
msgid
- The text or message ID to be translated.bindings
- (Optional) A list or map of bindings to interpolate in the message.context
- (Optional) A context for the translation.domain
- (Optional) A domain for the translation.
Dynamically localises a text. This function is useful for localising strings only known at runtime (when you can't use the l
or lp
macros).
Examples
iex> localise_dynamic("some_message_id")
"some_message_id"
iex> localise_dynamic("some_message_id", MyApp.MyModule)
"some_message_id"
Parameters
msgid
- The message id to be localized.caller_module
- (Optional) The module from which the call originates.
Localizes a list of strings at compile time.
This macro evaluates the list of strings and localizes each string based on the domain derived from the caller module. This is useful if you want to provide a list of strings at compile time that will later be used at runtime by localise_dynamic/2
.
Examples
iex> localise_strings(["hello", "world"])
["hello", "world"]
iex> localise_strings(["hello", "world"], MyApp.MyModule)
["hello", "world"]
Parameters
strings
- A list of strings to be localized.caller_module
- (Optional) The module from which the call originates.
lp(original_text_or_id, msgid_plural, n, bindings \\ [], context \\ nil, domain \\ nil)
View Source (macro)Translates a plural text with optional bindings, context, and domain.
This macro provides plural translation capabilities based on Gettext. It determines the appropriate domain and context for the translation.
Examples
iex> lp("Hi friend", "Hi friends", 2)
"Hi friends"
iex> lp("Hiya %{user_or_users}", "Hiyas %{user_or_users}", 1, [user_or_users: "Bookchin"], "test context")
"Hiya Bookchin"
Parameters
msgid
- The singular message id to be translated.msgid_plural
- The plural message id to be translated.n
- The number used to determine singular or plural form.bindings
- (Optional) A list or map of bindings to interpolate in the message.context
- (Optional) A context for the translation.domain
- (Optional) A domain for the translation.