ecto_translate v0.2.4 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

EctoTranslate is meant to be used by a module

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

Returns translatable id type configured for application

Functions

EctoTranslate is meant to be used 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.

changeset(struct, params \\ %{})
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

current_locale()
current_locale() :: String.t

An helper method to get the current Gettext locale

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>}
]
translatable_id_type()
translatable_id_type() :: atom

Returns translatable id type configured for application

The id type can be configured by setting :translatable_id_type config for :ecto_translate otp application.

Example

  config ecto_translate,
    translatable_id_type: :string

By default the is type is presumed as :integer