Skip to content

Commit

Permalink
Add config option :default_locale
Browse files Browse the repository at this point in the history
Allows declaring the name of the default locale
for the base translatable columns. This is helpful
when introspecting the schema to know what locales
are available.
  • Loading branch information
kipcole9 committed Jan 26, 2022
1 parent 6150d30 commit 2ec66cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/trans.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Trans do
* `:translates` (required) - list of the fields that will be translated.
* `:container` (optional) - name of the field that contains the embedded translations.
Defaults to`:translations`.
* `:default_locale` (optional) - declares the locale of the base untranslated column.
## Structured translations
Expand All @@ -25,7 +26,7 @@ defmodule Trans do
defmodule MyApp.Article do
use Ecto.Schema
use Trans, translates: [:title, :body]
use Trans, translates: [:title, :body], default_locale: :en
schema "articles" do
field :title, :string
Expand Down Expand Up @@ -65,7 +66,7 @@ defmodule Trans do
defmodule MyApp.Article do
use Ecto.Schema
use Trans, translates: [:title, :body]
use Trans, translates: [:title, :body], default_locale: :en
schema "articles" do
field :title, :string
Expand Down Expand Up @@ -98,6 +99,7 @@ defmodule Trans do
* `__trans__(:fields)` - Returns the list of translatable fields.
* `__trans__(:container)` - Returns the name of the translation container.
* `__trans__(:default_locale)` - Returns the name of default locale.
"""

@typedoc """
Expand All @@ -114,6 +116,7 @@ defmodule Trans do
quote do
Module.put_attribute(__MODULE__, :trans_fields, unquote(translatable_fields(opts)))
Module.put_attribute(__MODULE__, :trans_container, unquote(translation_container(opts)))
Module.put_attribute(__MODULE__, :trans_default_locale, unquote(translation_default_locale(opts)))

@after_compile {Trans, :__validate_translatable_fields__}
@after_compile {Trans, :__validate_translation_container__}
Expand All @@ -123,6 +126,9 @@ defmodule Trans do

@spec __trans__(:container) :: atom
def __trans__(:container), do: @trans_container

@spec __trans__(:default_locale) :: atom
def __trans__(:default_locale), do: @trans_default_locale
end
end

Expand Down Expand Up @@ -230,4 +236,11 @@ defmodule Trans do
{:ok, container} -> container
end
end

defp translation_default_locale(opts) do
case Keyword.fetch(opts, :default_locale) do
:error -> nil
{:ok, default_locale} -> default_locale
end
end
end
10 changes: 10 additions & 0 deletions test/trans_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ defmodule TransTest do
assert Article.__trans__(:container) == :translations
end

test "the default locale" do
defmodule Book do
use Trans, translates: [:title, :body], default_locale: :en
defstruct title: "", body: "", translations: %{}
end

assert Book.__trans__(:default_locale) == :en
assert Article.__trans__(:default_locale) == nil
end

test "returns the custom translation container name if specified" do
assert Comment.__trans__(:container) == :transcriptions
end
Expand Down

0 comments on commit 2ec66cb

Please sign in to comment.