Skip to content

Commit

Permalink
Merge pull request #3 from petrus-jvrensburg/default-locale
Browse files Browse the repository at this point in the history
Exclude the default locale when generating embedded fields
  • Loading branch information
kipcole9 authored Aug 10, 2022
2 parents 42b621b + 9898823 commit 82a4d3e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 14 deletions.
1 change: 0 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ config :ex_cldr_trans, Cldr.Trans.Repo,
log: false

config :ex_cldr,
default_locale: "en-001",
default_backend: MyApp.Cldr
10 changes: 7 additions & 3 deletions lib/cldr_trans.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Cldr.Trans do
* `: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.
Defaults to the `default_locale` configured for the Cldr backend.
## Structured translations
Expand All @@ -26,7 +27,7 @@ defmodule Cldr.Trans do
defmodule MyApp.Article do
use Ecto.Schema
use Trans, translates: [:title, :body], default_locale: :en
use MyApp.Cldr.Trans, translates: [:title, :body]
schema "articles" do
field :title, :string
Expand Down Expand Up @@ -66,7 +67,7 @@ defmodule Cldr.Trans do
defmodule MyApp.Article do
use Ecto.Schema
use Trans, translates: [:title, :body], default_locale: :en
use MyApp.Cldr.Trans, translates: [:title, :body]
schema "articles" do
field :title, :string
Expand Down Expand Up @@ -217,6 +218,7 @@ defmodule Cldr.Trans do
end

defmacro translations(field_name, translation_module, locales, options) do
module = __CALLER__.module
options = Keyword.merge(Cldr.Trans.default_trans_options(), options)
{build_field_schema, options} = Keyword.pop(options, :build_field_schema)

Expand All @@ -229,7 +231,9 @@ defmodule Cldr.Trans do

embeds_one unquote(field_name), unquote(translation_module), unquote(options) do
for locale_name <- List.wrap(unquote(locales)) do
embeds_one locale_name, Module.concat(__MODULE__, Fields), on_replace: :update
if locale_name != Module.get_attribute(unquote(module), :trans_default_locale) do
embeds_one locale_name, Module.concat(__MODULE__, Fields), on_replace: :update
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/models/article.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Cldr.Trans.Article do
"""

use Ecto.Schema
use Cldr.Trans, translates: [:title, :body]
use MyApp.Cldr.Trans, translates: [:title, :body]

import Ecto.Changeset

Expand Down
2 changes: 1 addition & 1 deletion test/support/models/book.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Cldr.Trans.Book do
"""

use Ecto.Schema
use Cldr.Trans, translates: [:title, :body], default_locale: :en
use MyApp.Cldr.Trans, translates: [:title, :body]

schema "articles" do
field :title, :string
Expand Down
2 changes: 1 addition & 1 deletion test/support/models/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Cldr.Trans.Comment do
@moduledoc false

use Ecto.Schema
use Cldr.Trans, translates: [:comment], container: :transcriptions
use MyApp.Cldr.Trans, translates: [:comment], container: :transcriptions

import Ecto.Changeset

Expand Down
2 changes: 1 addition & 1 deletion test/support/models/magazine.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Cldr.Trans.Magazine do
use Ecto.Schema
use Cldr.Trans, translates: [:title, :body], default_locale: :en
use MyApp.Cldr.Trans, translates: [:title, :body]

schema "articles" do
field :title, :string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule MyApp.Cldr do
use Cldr,
gettext: MyApp.Gettext,
locales: ["en", "de", "ja", "en-AU", "th", "ar", "pl", "doi", "fr-CA", "nb", "no"],
default_locale: "en",
generate_docs: true,
providers: [Cldr.Trans]

end
13 changes: 8 additions & 5 deletions test/trans_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ defmodule Cldr.TransTest do
assert Article.__trans__(:container) == :translations
end

test "the default locale" do
test "the default locale can be set from the Cldr backend" do
assert Article.__trans__(:default_locale) == :en
end

test "the Cldr backend's default locale can be overridden per model" do
defmodule Book do
use Trans, translates: [:title, :body], default_locale: :en
use Trans, translates: [:title, :body], default_locale: :fr
defstruct title: "", body: "", translations: %{}
end

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

test "returns the custom translation container name if specified" do
Expand Down Expand Up @@ -88,7 +91,7 @@ defmodule Cldr.TransTest do
unique: true}} =
Cldr.Trans.Brochure.__schema__(:type, :translations)

assert [:ar, :de, :doi, :en, :"en-001", :"en-AU", :fr, :"fr-CA", :ja, :nb, :no, :pl, :th] =
assert [:ar, :de, :doi, :"en-AU", :fr, :"fr-CA", :ja, :nb, :no, :pl, :th] =
Cldr.Trans.Brochure.Translations.__schema__(:fields)
assert [:title, :body] = Cldr.Trans.Brochure.Translations.Fields.__schema__(:fields)
end
Expand Down

0 comments on commit 82a4d3e

Please sign in to comment.