I18n Helpers v0.5.3 I18nHelpers.Ecto.TranslatableSchema behaviour View Source

Provides macros for defining translatable fields and associations.

This module's purpose is to provide the I18nHelpers.Ecto.Translator module with a way to access the list of fields and associations from the Ecto Schema that needs to be translated, and with virtual fields allowing to store the translations for the current locale.

__using__\1 this module provides the caller module with two functions: get_translatable_fields\0 and get_translatable_assocs\0 listing all the translatable fields and the translatable associations respectively.

Fields that are to be translated are expected to hold maps

field :title, :map

where each key represents a locale and each value contains the text for that locale. Below is an example of such map:

%{
  "en" => "My Favorite Books",
  "fr" => "Mes Livres Préférés",
  "nl" => "Mijn Lievelingsboeken",
  "en-GB" => "My Favourite Books"
}

Each of those fields must come with a virtual field which is used to hold the translation for the current locale.

field :title, :map
field :translated_title, :string, virtual: true

Such a translatable field must be included in the translatable fields list:

def get_translatable_fields, do: [:title]

This module provides the macro translatable_field\1 which allows to execute those three steps above (add the field as :map, add the virtual field and add the field to the translatable fields list) in one line:

translatable_field :title

Macros marking associations as translatable are also provided:

  • translatable_belongs_to\2
  • translatable_has_many\2
  • translatable_has_one\2
  • translatable_many_to_many\3

The macros above add the given association field name to the translatable associations list, which is accessible with get_translatable_assocs\0.

Link to this section Summary

Functions

Defines a translatable belongs_to association.

Defines a translatable field on the schema.

Defines a translatable has_many association.

Defines a translatable has_one association.

Defines a translatable many_to_many association.

Link to this section Functions

Link to this macro

translatable_belongs_to(field_name, module_name)

View Source (macro)

Defines a translatable belongs_to association.

The macro will add the given field name into the translatable associations list.

Link to this macro

translatable_field(field_name)

View Source (macro)

Defines a translatable field on the schema.

This macro will generate two fields:

  • a field with the given name and type :map and
  • a virtual field with the given name prepended by "translated_" and type :string.

For example

translatable_field :title

will generate

field :title, :map
field :translated_title, :string, virtual: true

The macro will add the given field name into the translatable fields list.

Link to this macro

translatable_has_many(field_name, module_name)

View Source (macro)

Defines a translatable has_many association.

The macro will add the given field name into the translatable associations list.

Link to this macro

translatable_has_one(field_name, module_name)

View Source (macro)

Defines a translatable has_one association.

The macro will add the given field name into the translatable associations list.

Link to this macro

translatable_many_to_many(field_name, module_name, opts \\ [])

View Source (macro)

Defines a translatable many_to_many association.

The macro will add the given field name into the translatable associations list.

Link to this section Callbacks

Link to this callback

get_translatable_assocs()

View Source
get_translatable_assocs() :: [atom()]
Link to this callback

get_translatable_fields()

View Source
get_translatable_fields() :: [atom()]