TranslaTable (TranslaTable v0.2.2) View Source

TranslaTable is a helper library for creating a translation schema inside your ecto schema.

Translation

To define a translation schema it just need to use the TranslaTable helper inside your schema to be translated

defmodule MyApp.Post do
  use Ecto.Schema
  use TranslaTable.Schema,
    translation_mod: MyApp.PostTranslation

  import Ecto.Changeset

  alias MyApp.PostTranslation

  schema "post" do
    field :title, :string
    field :description, :string
    field :author, :string
    field :slug, :string

    has_many_translations()

    timestamps()
  end

  @doc false
  def changeset(post, attrs) do
    post
    |> cast(attrs, [:title, :description, :author, :slug])
    |> cast_translation()
  end
end

Then create the translation module

defmodule MyApp.PostTranslation
  alias MyApp.Post
  alias MyApp.Language

  use TranslaTable,
    module: Post, # Module to be translated
    lang_mod: Language, # Language schema table
    fields: [:title, :description, :slug]
end

Then the TranslaTable Macro will create a schema with the given fields and make the relation with your Language module.

Link to this section Summary

Link to this section Functions

Link to this macro

cast_translation(changeset, mod)

View Source (macro)
Link to this macro

has_many_translations(mod)

View Source (macro)