Skip to content
forked from change/linguist

Elixir Internationalization library

License

Notifications You must be signed in to change notification settings

dolfinus/linguist

 
 

Repository files navigation

Linguist

Linguist is a simple Elixir Internationalization library

Usage

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"

Configuration

Pluralization key

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:

  1. By default, linguist creates its own Cldr backend module Linguist.Cldr. It handles only en 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.

  1. You can pass your own Cldr backend module within application config:
config :linguist, cldr: MyApp.Cldr
  1. 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
  1. 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.

:persistent_term support

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

About

Elixir Internationalization library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 100.0%