Linguist is a simple Elixir Internationalization library
defmodule I18n do
use Linguist.Vocabulary
locale "en", [
flash: [
notice: [
hello: "hello %{first} %{last}",
bye: "bye now, %{name}!"
]
],
users: [
title: "Users",
profiles: [
title: "Profiles",
]
]
]
locale "fr", Path.join([__DIR__, "fr.exs"])
end
# fr.exs
[
flash: [
notice: [
hello: "salut %{first} %{last}"
]
]
]
iex> I18n.t!("en", "flash.notice.hello", first: "chris", last: "mccord")
"hello chris mccord"
iex> I18n.t!("fr", "flash.notice.hello", first: "chris", last: "mccord")
"salut chris mccord"
iex> I18n.t!("en", "users.title")
"Users"
The key to use for pluralization is configurable, and should likely be an atom:
# default
config :linguist, pluralization_key: :count
Will cause the system to pluralize based on the count
parameter passed to the t
function.
But also you should add proper locale to Cldr backend.
There are multiple ways to perform that:
- By default, linguist creates its own Cldr backend module
Linguist.Cldr
. It handles onlyen
locale plurals but you can add more locales:
config :linguist, Linguist.Cldr, locales: ["fr", "es"]
But this way is not recommended because it is not flexible.
- You can pass your own Cldr backend module within application config:
config :linguist, cldr: MyApp.Cldr
- If you've using
Linguist.Vocabulary
, you can pass own Cldr module name explicitly in your I18n module:
defmodule I18n do
use Linguist.Vocabulary, cldr: MyApp.Cldr
...
end
or like that:
defmodule I18n do
use Linguist.Vocabulary
@cldr MyApp.Cldr
end
- If you've using
Linguist.MemoizedVocabulary
, you can set up own Cldr module name:
Linguist.MemorizedVocabulary.cldr(MyApp.Cldr)
will cause the system to pluralize based on the count
parameter passed to the t
function.
You can use :persistent_term
backend instead of :ets
in Linguist.MemoizedVocabulary
by setting up:
config :linguist, vocabulary_backend: :persistent_term
This is only available on OTP >= 21.2