Skip to content

Commit

Permalink
Use compile_env if available
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 25, 2020
1 parent 91c0585 commit 9df6f4e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
7 changes: 6 additions & 1 deletion lib/gettext.ex
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,15 @@ defmodule Gettext do

@doc false
defmacro __using__(opts) do
env_fun =
if function_exported?(Application, :compile_env, 3), do: :compile_env, else: :get_env

quote do
require Logger

@gettext_opts unquote(opts)
opts = unquote(opts)
otp_app = Keyword.fetch!(opts, :otp_app)
@gettext_opts Keyword.merge(opts, Application.unquote(env_fun)(otp_app, __MODULE__, []))
@before_compile Gettext.Compiler

def handle_missing_bindings(exception, incomplete) do
Expand Down
17 changes: 2 additions & 15 deletions lib/gettext/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,8 @@ defmodule Gettext.Compiler do

@doc false
defmacro __before_compile__(env) do
compile_time_opts = Module.get_attribute(env.module, :gettext_opts)

# :otp_app is only supported in "use Gettext" (because we need it to get the Mix config).
{otp_app, compile_time_opts} = Keyword.pop(compile_time_opts, :otp_app)

if is_nil(otp_app) do
# We're using Keyword.fetch!/2 to raise below.
Keyword.fetch!(compile_time_opts, :otp_app)
end

# Options given to "use Gettext" have higher precedence than options set
# throught Mix.Config.
mix_config_opts = Application.get_env(otp_app, env.module, [])
opts = Keyword.merge(mix_config_opts, compile_time_opts)

opts = Module.get_attribute(env.module, :gettext_opts)
otp_app = Keyword.fetch!(opts, :otp_app)
priv = Keyword.get(opts, :priv, @default_priv)
translations_dir = Application.app_dir(otp_app, priv)
external_file = String.replace(Path.join(".compile", priv), "/", "_")
Expand Down
4 changes: 4 additions & 0 deletions lib/gettext/plural.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ defmodule Gettext.Plural do
use Gettext, otp_app: :my_app, plural_forms: MyApp.Plural
end
**Note**: set `:plural_forms` in your `config/config.exs` and
not in `config/runtime.exs`, as this configuration is read when
compiling your backends.
Notice that tasks such as `mix gettext.merge` use the plural
backend configured under the `:gettext` application, so generally
speaking the first format is preferred.
Expand Down

0 comments on commit 9df6f4e

Please sign in to comment.