ExI18n is key-based internationalization library for Elixir.
Add exi18n
to your list of dependencies and to applications
in mix.exs
:
# mix.exs
def deps do
[
{:exi18n, "~> 0.9.0"},
]
end
def application do
[applications: [
:exi18n,
]]
end
Add configuration to your config/config.exs
:
# config.exs
config :exi18n,
default_locale: "en",
locales: ~w(en),
fallback: false,
loader: :yml,
loader_options: %{path: "priv/locales"},
var_prefix: "%{",
var_suffix: "}"
Option | Description | Default |
---|---|---|
default_locale | Default locale in your application. | "en" |
locales | Supported locales. | ["en"] |
fallback | Fallback to default locale if translation empty. | false |
loader | Translation loader. Supported types: :yml , :http , MyApp.Loader . |
:yml |
loader_options | Translation loader options. | %{} |
var_prefix | Prefix for values in translations. | "%{" |
var_suffix | Suffix for values in translations. | "}" |
This loader will use yaml files from path
to load translations.
ExI18n.Loader.YAML
# mix.exs
def deps do
[
{:exi18n, "~> 0.9.0"},
{:yaml_elixir, "~> 2.0"},
]
end
def application do
[applications: [
:exi18n,
:yaml_elixir,
]]
end
Option | Required | Description |
---|---|---|
path | Yes | Path to locale files. |
# config.exs
config :exi18n,
loader: :yml,
loader_options: %{
path: "priv/locales" # or {MyHelper, :path, ["priv/locales"]}
}
MyApp.Loader
Make sure that your custom loader has load/2
function that accepts locale
and options
as parameters and returns Map
with translations.
Example:
defmodule MyApp.Loader do
def load(locale, _options) do
%{
"en" => %{...},
"de" => %{...}
}[locale]
end
end
# mix.exs
def deps do
[
{:exi18n, "~> 0.9.0"},
]
end
def application do
[applications: [
:exi18n,
]]
end
# config.exs
config :exi18n,
loader: MyApp.Loader,
loader_options: %{my_config: "value"}