ecto_translate v0.2.0 EctoTranslate
EctoTranslate is a library that helps with translating Ecto data. EctoTranslate can help you with returning translated values of your Ecto data attributes. For this it uses a singe table called “translations” which will contain polymorphic entries for all of your Ecto data stucts.
examples
Given an ecto module like :
defmodule MyApp.Post do
...
use EctoTranslate, [:title, :body]
...
schema "posts" do
field :title, :string
field :body, :string
end
...
end
You can set translations using : locale: :nl, title: “Een nederlandse titel”, description: “Een nederlandse beschrijving”]
Then you can ask for a translated fields explicitly using :
iex> MyApp.Post.translated_title(post, :nl)
"Een nederlandse titel"
Or you can update the model by replacing the fields with their translations using :
iex> translated_post = MyApp.Post.translate!(post, :nl)
iex> translated_post.title
"Een nederlandse titel"
iex> translated_post.description
"Een nederlandse beschrijving"
You can also pass in a collection to translate in batch preventing n+1 queries
iex> posts = MyApp.Post |> MyApp.Repo.all
iex> translated_posts = MyApp.Post.translate!(posts, :nl)
If a translation is not found, it will fall back to the original database value. If you ommit the locale in the function calls, the current gettext locale will be used.
iex> Gettext.set_locale(MyApp.Gettext, :nl)
iex> translated_post = MyApp.Post.translate!(post)
iex> translated_post.title
Summary
Functions
Builds a changeset based on the struct
and params
and validates the required fields and given locale
An helper method to get the current Gettext locale
An helper method to get the known Gettext locales
Creates the translations for the given fields in the database or will update those when they already exist
Macros
EctoTranslate is meant to be use
d by a module
Functions
Specs
changeset(struct :: Ecto.Schema.t, params :: Map.t) :: Ecto.Changeset.t
Builds a changeset based on the struct
and params
and validates the required fields and given locale
Specs
current_locale :: String.t
An helper method to get the current Gettext locale
Specs
An helper method to get the known Gettext locales
Specs
set(model :: Ecto.Schema.t, options :: <a href="http://elixir-lang.org/docs/stable/elixir/List.html#t:t/0">List.t</a>[{<a href="http://elixir-lang.org/docs/stable/elixir/Atom.html#t:t/0">Atom.t</a>, Any.t}]) ::
:ok |
{:error, List.t}
Creates the translations for the given fields in the database or will update those when they already exist.
Example
iex> %EctoTranslate.ExampleModel{title: "A title in english", description: "A description in english"}
...> |> EctoTranslate.Repo.insert!
...> |> EctoTranslate.set(locale: :de, title: "Eine deutche titel", description: "Ein deutsche umschreibung")
[
%EctoTranslate{__meta__: #Ecto.Schema.Metadata<:loaded, "translations">, content: "Eine deutche titel", field: "title", id: 241, inserted_at: #Ecto.DateTime<2016-07-01 21:09:11>, locale: "de", translatable_id: 221, translatable_type: "test_model", updated_at: #Ecto.DateTime<2016-07-01 21:09:11>},
%EctoTranslate{__meta__: #Ecto.Schema.Metadata<:loaded, "translations">, content: "Ein deutsche umschreibung", field: "description", id: 242, inserted_at: #Ecto.DateTime<2016-07-01 21:09:11>, locale: "de", translatable_id: 221, translatable_type: "test_model", updated_at: #Ecto.DateTime<2016-07-01 21:09:11>}
]
Macros
Specs
EctoTranslate is meant to be use
d by a module.
use
needs a list of attributes that you would like to make available for translation.
defmodule MyApp.Post do
use EctoTranslatable, [:title, :body]
end
When use
is called, it will add the following functions to your module
- translatable_fields/0
- translate!/1
- translate!/2
- translated_attr/1 i.e. translated_title/1
- translated_attr/2 i.e. translated_title/2
For each of the functions the second parameter is an optional locale. if ommitted, it will use the current Gettext locale.