Skip to content

Commit

Permalink
Manually generate translations module with @moduledoc false
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Mar 5, 2024
1 parent 9670c37 commit d396f7b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# Changelog

## Cldr Trans v1.1.2

This is the changelog for Cldr Trans version 1.1.2 released on March 6th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_trans/tags)

### Bug Fixes

* Adds `@moduledoc false` to the translations generated modules to reduce noise.

## Cldr Trans v1.1.1

This is the changelog for Cldr Trans version 1.1.1 released on March 5th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_trans/tags)

### Bug Fixes

* Adds `@moduledoc false` to the translations and field generated modules to reduce noise. Thanks to @ArthurClemens for the issue. Closes #9.
* Adds `@moduledoc false` to the translations field generated modules to reduce noise. Thanks to @ArthurClemens for the issue. Closes #9.

### Enhancements

Expand Down
43 changes: 29 additions & 14 deletions lib/cldr_trans.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,21 @@ defmodule Cldr.Trans do
alias Cldr.Locale

@typedoc """
A translatable struct that uses `Trans`
A translatable struct that uses `Trans`.
"""
@type translatable() :: struct()

@typedoc """
A struct field as an atom
A struct field as an atom.
"""
@type field :: atom()

@typedoc """
When translating or querying either a single
locale or a list of locales can be provided
locale or a list of locales can be provided.
"""
@type locale_list :: Locale.locale_reference() | [Locale.locale_name(), ...]

Expand Down Expand Up @@ -166,7 +169,7 @@ defmodule Cldr.Trans do

@doc false
def default_trans_options do
[on_replace: :update, primary_key: false, build_field_schema: true]
[build_field_schema: true]
end

defmacro translations(field_name, translation_module \\ nil, locales_or_options \\ []) do
Expand Down Expand Up @@ -195,32 +198,44 @@ defmodule Cldr.Trans do
end

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

quote do
if unquote(translation_module) && unquote(build_field_schema) do
@before_compile {Cldr.Trans, :__build_embedded_schema__}
end

@translation_module unquote(translation_module)
@translation_module Module.concat(unquote(caller), unquote(translation_module))
@locales unquote(locales)

embeds_one unquote(field_name), unquote(translation_module), unquote(options) do
for locale_name <- List.wrap(unquote(locales)),
locale_name != Module.get_attribute(unquote(module), :trans_default_locale) do
embeds_one locale_name, Module.concat(__MODULE__, Fields), on_replace: :update
end
end
embeds_one unquote(field_name), @translation_module, on_replace: :update
end
end

defmacro __build_embedded_schema__(env) do
translation_module = Module.get_attribute(env.module, :translation_module)
fields = Module.get_attribute(env.module, :trans_fields)
locales = Module.get_attribute(env.module, :locales)
default_locale = Module.get_attribute(env.module, :trans_default_locale)

quote do
defmodule Module.concat(__MODULE__, unquote(translation_module).Fields) do
defmodule unquote(translation_module) do
@moduledoc false

use Ecto.Schema

@primary_key false
embedded_schema do
for locale_name <- List.wrap(unquote(locales)),
locale_name != unquote(default_locale) do
embeds_one locale_name, Module.concat(__MODULE__, Fields), on_replace: :update
end
end
end

defmodule Module.concat(unquote(translation_module), Fields) do
@moduledoc false

use Ecto.Schema
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.Trans.Mixfile do
use Mix.Project

@version "1.1.1"
@version "1.1.2"

def project do
[
Expand Down
29 changes: 21 additions & 8 deletions test/trans_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ defmodule Cldr.TransTest 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

test "the Cldr backend's default locale can be overridden per model" do
defmodule Book do
use Trans, translates: [:title, :body], default_locale: :fr
defstruct title: "", body: "", translations: %{}
Expand Down Expand Up @@ -58,6 +58,12 @@ defmodule Cldr.TransTest do
test "translations/3 macro" do
assert :translations in Trans.Magazine.__schema__(:fields)

assert [:es, :it, :de] =
Trans.Magazine.Translations.__schema__(:fields)

assert [:title, :body] =
Trans.Magazine.Translations.Fields.__schema__(:fields)

assert {
:parameterized, Ecto.Embedded,
%Ecto.Embedded{
Expand All @@ -70,14 +76,17 @@ defmodule Cldr.TransTest do
related: Trans.Magazine.Translations,
unique: true}} =
Trans.Magazine.__schema__(:type, :translations)

assert [:es, :it, :de] = Trans.Magazine.Translations.__schema__(:fields)
assert [:title, :body] = Trans.Magazine.Translations.Fields.__schema__(:fields)
end

test "MyApp.Cldr.Trans.translations/3 macro" do
assert :translations in Cldr.Trans.Brochure.__schema__(:fields)

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)

assert {
:parameterized, Ecto.Embedded,
%Ecto.Embedded{
Expand All @@ -90,9 +99,13 @@ defmodule Cldr.TransTest do
related: Cldr.Trans.Brochure.Translations,
unique: true}} =
Cldr.Trans.Brochure.__schema__(:type, :translations)
end

test "Confirm translations embedded schemas have no docs" do
assert {:docs_v1, 1, :elixir, "text/markdown", :hidden, %{}, _} =
Code.fetch_docs(Cldr.Trans.Brochure.Translations)

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)
assert {:docs_v1, 1, :elixir, "text/markdown", :hidden, %{}, _} =
Code.fetch_docs(Cldr.Trans.Brochure.Translations.Fields)
end
end

0 comments on commit d396f7b

Please sign in to comment.