Skip to content

Latest commit

 

History

History
150 lines (117 loc) · 4.3 KB

README.md

File metadata and controls

150 lines (117 loc) · 4.3 KB

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.

You might also be interested in set_locale which will enable urls like http://www.example.com/nl-nl/foo/bar and set the correct locale.

examples

Given an ecto module like :

defmodule MyApp.Post do
  ...
  import Ecto.Query
  use EctoTranslate, [:title, :body]
  ...
  schema "posts" do
    field :title, :string
    field :body, :string
  end
  ...
end

You can set translations using :

record = MyApp.Repo.get(MyApp.Post, 1)
EctoTranslate.set(record, 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

Docs

Docs can be found here

Installation

  1. Add ecto_translate to your list of dependencies in mix.exs:

    def deps do
      [{:ecto_translate, "~> 1.0"}]
    end
  2. Ensure ecto_translate is started before your application:

    def application do
      [applications: [:ecto_translate]]
    end
  3. Configure translatable_id_type if neccessary.

    If your models does not use integer primary keys, (e.g: they use binary id) you can configure EctoTranslate to use a different type of column type on translatable_id.

    To do this simply config :ecto_translate otp app for :translatable_id_type with your choice of type. (e.g: binary_id, string, etc.)

    config :ecto_translate,
        translatable_id_type: :binary_id
  4. Create a migration for the translation table by running:

    mix ecto_translate.gen.migration
  5. Migrate

    mix ecto.migrate
  6. Update your config.exs and add these settings

    config :ecto_translate, repo: MyApp.Repo, gettext: MyApp.Gettext
  7. Add the macro to your model that you want to translate

    defmodule MyApp.Post do
      ...
      import Ecto.Query
      use EctoTranslate, [:title, :body]
      ...
      schema "posts" do
        field :title, :string
        field :body, :string
      end
      ...
    end

    Important: Don't forget to import Ecto.Query before use EctoTranslate

  8. Set translations for your data

    record = MyApp.Repo.get(MyApp.Post, 1)
    EctoTranslate.set(record, locale: :nl, title: "Een nederlandse titel",  description: "Een nederlandse beschrijving"]
  9. Use the translations

    iex> translated_post = MyApp.Post.translate!(post, :nl)
    iex> translated_post.title
    "Een nederlandse titel"

    or

    iex> MyApp.Post.translated_title(post, :nl)
    "Een nederlandse titel"