Skip to content

Commit 9df6f4e

Browse files
committed
Use compile_env if available
1 parent 91c0585 commit 9df6f4e

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

lib/gettext.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,15 @@ defmodule Gettext do
584584

585585
@doc false
586586
defmacro __using__(opts) do
587+
env_fun =
588+
if function_exported?(Application, :compile_env, 3), do: :compile_env, else: :get_env
589+
587590
quote do
588591
require Logger
589592

590-
@gettext_opts unquote(opts)
593+
opts = unquote(opts)
594+
otp_app = Keyword.fetch!(opts, :otp_app)
595+
@gettext_opts Keyword.merge(opts, Application.unquote(env_fun)(otp_app, __MODULE__, []))
591596
@before_compile Gettext.Compiler
592597

593598
def handle_missing_bindings(exception, incomplete) do

lib/gettext/compiler.ex

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,8 @@ defmodule Gettext.Compiler do
1616

1717
@doc false
1818
defmacro __before_compile__(env) do
19-
compile_time_opts = Module.get_attribute(env.module, :gettext_opts)
20-
21-
# :otp_app is only supported in "use Gettext" (because we need it to get the Mix config).
22-
{otp_app, compile_time_opts} = Keyword.pop(compile_time_opts, :otp_app)
23-
24-
if is_nil(otp_app) do
25-
# We're using Keyword.fetch!/2 to raise below.
26-
Keyword.fetch!(compile_time_opts, :otp_app)
27-
end
28-
29-
# Options given to "use Gettext" have higher precedence than options set
30-
# throught Mix.Config.
31-
mix_config_opts = Application.get_env(otp_app, env.module, [])
32-
opts = Keyword.merge(mix_config_opts, compile_time_opts)
33-
19+
opts = Module.get_attribute(env.module, :gettext_opts)
20+
otp_app = Keyword.fetch!(opts, :otp_app)
3421
priv = Keyword.get(opts, :priv, @default_priv)
3522
translations_dir = Application.app_dir(otp_app, priv)
3623
external_file = String.replace(Path.join(".compile", priv), "/", "_")

lib/gettext/plural.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ defmodule Gettext.Plural do
7070
use Gettext, otp_app: :my_app, plural_forms: MyApp.Plural
7171
end
7272
73+
**Note**: set `:plural_forms` in your `config/config.exs` and
74+
not in `config/runtime.exs`, as this configuration is read when
75+
compiling your backends.
76+
7377
Notice that tasks such as `mix gettext.merge` use the plural
7478
backend configured under the `:gettext` application, so generally
7579
speaking the first format is preferred.

0 commit comments

Comments
 (0)