Skip to content

Commit 0cdb06a

Browse files
authored
Deprecate "use Gettext" for backends and improve docs (#392)
1 parent 57c6249 commit 0cdb06a

10 files changed

Lines changed: 889 additions & 455 deletions

File tree

lib/gettext.ex

Lines changed: 124 additions & 116 deletions
Large diffs are not rendered by default.

lib/gettext/backend.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ defmodule Gettext.Backend do
8585
8686
For example, if something like this is called:
8787
88-
MyApp.Gettext.gettext("Hello %{name}, your favorite color is %{color}", name: "Jane", color: "blue")
88+
gettext("Hello %{name}, your favorite color is %{color}", name: "Jane", color: "blue")
8989
9090
and our `it/LC_MESSAGES/default.po` looks like this:
9191

lib/gettext/compiler.ex

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ defmodule Gettext.Compiler do
6161
Gettext.ExtractorAgent.add_backend(__MODULE__)
6262
end
6363

64-
unquote(macros())
65-
6664
# These are the two functions we generate inside the backend.
6765

6866
@impl Gettext.Backend
@@ -99,12 +97,12 @@ defmodule Gettext.Compiler do
9997
end)
10098
end
10199

102-
defp macros() do
100+
def generate_macros(_env) do
103101
quote unquote: false do
104102
defmacro dpgettext_noop(domain, msgctxt, msgid) do
105-
domain = Gettext.Compiler.expand_to_binary(domain, "domain", __MODULE__, __CALLER__)
106-
msgid = Gettext.Compiler.expand_to_binary(msgid, "msgid", __MODULE__, __CALLER__)
107-
msgctxt = Gettext.Compiler.expand_to_binary(msgctxt, "msgctxt", __MODULE__, __CALLER__)
103+
domain = Gettext.Compiler.expand_to_binary(domain, "domain", __CALLER__)
104+
msgid = Gettext.Compiler.expand_to_binary(msgid, "msgid", __CALLER__)
105+
msgctxt = Gettext.Compiler.expand_to_binary(msgctxt, "msgctxt", __CALLER__)
108106

109107
if Gettext.Extractor.extracting?() do
110108
Gettext.Extractor.extract(
@@ -143,12 +141,10 @@ defmodule Gettext.Compiler do
143141
end
144142

145143
defmacro dpngettext_noop(domain, msgctxt, msgid, msgid_plural) do
146-
domain = Gettext.Compiler.expand_to_binary(domain, "domain", __MODULE__, __CALLER__)
147-
msgid = Gettext.Compiler.expand_to_binary(msgid, "msgid", __MODULE__, __CALLER__)
148-
msgctxt = Gettext.Compiler.expand_to_binary(msgctxt, "msgctxt", __MODULE__, __CALLER__)
149-
150-
msgid_plural =
151-
Gettext.Compiler.expand_to_binary(msgid_plural, "msgid_plural", __MODULE__, __CALLER__)
144+
domain = Gettext.Compiler.expand_to_binary(domain, "domain", __CALLER__)
145+
msgid = Gettext.Compiler.expand_to_binary(msgid, "msgid", __CALLER__)
146+
msgctxt = Gettext.Compiler.expand_to_binary(msgctxt, "msgctxt", __CALLER__)
147+
msgid_plural = Gettext.Compiler.expand_to_binary(msgid_plural, "msgid_plural", __CALLER__)
152148

153149
if Gettext.Extractor.extracting?() do
154150
Gettext.Extractor.extract(
@@ -309,7 +305,7 @@ defmodule Gettext.Compiler do
309305
end
310306

311307
defmacro gettext_comment(comment) do
312-
comment = Gettext.Compiler.expand_to_binary(comment, "comment", __MODULE__, __CALLER__)
308+
comment = Gettext.Compiler.expand_to_binary(comment, "comment", __CALLER__)
313309
Gettext.Compiler.append_extracted_comment(comment)
314310
:ok
315311
end
@@ -320,9 +316,16 @@ defmodule Gettext.Compiler do
320316
Expands the given `msgid` in the given `env`, raising if it doesn't expand to
321317
a binary.
322318
"""
323-
@spec expand_to_binary(binary, binary, module, Macro.Env.t()) :: binary | no_return
324-
def expand_to_binary(term, what, gettext_module, env)
319+
@spec expand_to_binary(binary, binary, Macro.Env.t()) :: binary | no_return
320+
def expand_to_binary(term, what, env)
325321
when what in ~w(domain msgctxt msgid msgid_plural comment) do
322+
gettext_module =
323+
if Module.open?(env.module) do
324+
Module.get_attribute(env.module, :__gettext_backend__)
325+
else
326+
List.first(env.module.__info__(:attributes)[:__gettext_backend__])
327+
end
328+
326329
raiser = fn term ->
327330
raise ArgumentError, """
328331
Gettext macros expect message keys (msgid and msgid_plural),
@@ -337,7 +340,7 @@ defmodule Gettext.Compiler do
337340
module:
338341
339342
string = "hello world"
340-
Gettext.gettext(#{inspect(gettext_module)}, string)
343+
Gettext.gettext(#{if(gettext_module, do: inspect(gettext_module), else: "backend")}, string)
341344
"""
342345
end
343346

@@ -355,6 +358,34 @@ defmodule Gettext.Compiler do
355358
end
356359
end
357360

361+
@doc """
362+
Expands the given `term` to a compile-time atom in the given `env`.
363+
"""
364+
@spec expand_backend(Macro.t(), Macro.Env.t()) :: module
365+
def expand_backend(term, env) do
366+
case Macro.expand(term, env) do
367+
term when is_atom(term) and term not in [nil, false, true] ->
368+
term
369+
370+
_other ->
371+
raise ArgumentError, """
372+
Gettext.Macros macros (that end with "_with_backend") expect the backend argument
373+
to be an atom at compile-time, but the given term doesn't. This is what the macro
374+
received:
375+
376+
#{inspect(term)}
377+
378+
Dynamic messages should be avoided as they limit Gettext's
379+
ability to extract messages from your source code. If you are
380+
sure you need dynamic lookup, you can use the functions in the Gettext
381+
module:
382+
383+
string = "hello world"
384+
Gettext.gettext(backend, string)
385+
"""
386+
end
387+
end
388+
358389
@doc """
359390
Appends the given comment to the list of extracted comments in the process dictionary.
360391
"""

0 commit comments

Comments
 (0)