@@ -97,69 +97,105 @@ defmodule Gettext.Compiler do
9797 end )
9898 end
9999
100- # TODO: remove once we deprecate "use Gettext" for generating backends.
101- def generate_macros ( _env ) do
102- quote unquote: false do
103- require Gettext.Macros
100+ ## BEGIN of deprecated code
101+ ## TODO: Remove this block once "use Gettext" is removed
104102
103+ defmacro generate_macros ( _env ) do
104+ quote unquote: false do
105105 defmacro dpgettext_noop ( domain , msgctxt , msgid ) do
106- Gettext.Macros . dpgettext_noop_with_backend ( __MODULE__ , domain , msgctxt , msgid )
106+ domain = Gettext.Compiler . expand_to_binary ( domain , "domain" , __MODULE__ , __CALLER__ )
107+ msgid = Gettext.Compiler . expand_to_binary ( msgid , "msgid" , __MODULE__ , __CALLER__ )
108+ msgctxt = Gettext.Compiler . expand_to_binary ( msgctxt , "msgctxt" , __MODULE__ , __CALLER__ )
109+
110+ if Gettext.Extractor . extracting? ( ) do
111+ Gettext.Extractor . extract (
112+ __CALLER__ ,
113+ __MODULE__ ,
114+ domain ,
115+ msgctxt ,
116+ msgid ,
117+ Gettext.Compiler . get_and_flush_extracted_comments ( )
118+ )
119+ end
120+
121+ msgid
107122 end
108123
109124 defmacro dgettext_noop ( domain , msgid ) do
110125 quote do
111- Gettext.Macros . dgettext_noop_with_backend ( __MODULE__ , unquote ( domain ) , unquote ( msgid ) )
126+ unquote ( __MODULE__ ) . dpgettext_noop ( unquote ( domain ) , nil , unquote ( msgid ) )
112127 end
113128 end
114129
115130 defmacro gettext_noop ( msgid ) do
131+ domain = __gettext__ ( :default_domain )
132+
116133 quote do
117- Gettext.Macros . gettext_noop_with_backend ( __MODULE__ , unquote ( msgid ) )
134+ unquote ( __MODULE__ ) . dpgettext_noop ( unquote ( domain ) , nil , unquote ( msgid ) )
118135 end
119136 end
120137
121138 defmacro pgettext_noop ( msgid , context ) do
139+ domain = __gettext__ ( :default_domain )
140+
122141 quote do
123- Gettext.Macros . pgettext_noop_with_backend ( __MODULE__ , context , unquote ( msgid ) )
142+ unquote ( __MODULE__ ) . dpgettext_noop ( unquote ( domain ) , unquote ( context ) , unquote ( msgid ) )
124143 end
125144 end
126145
127146 defmacro dpngettext_noop ( domain , msgctxt , msgid , msgid_plural ) do
128- Gettext.Macros . dpgettext_noop_with_backend (
129- __MODULE__ ,
130- domain ,
131- msgctxt ,
132- msgid ,
133- msgid_plural
134- )
147+ domain = Gettext.Compiler . expand_to_binary ( domain , "domain" , __MODULE__ , __CALLER__ )
148+ msgid = Gettext.Compiler . expand_to_binary ( msgid , "msgid" , __MODULE__ , __CALLER__ )
149+ msgctxt = Gettext.Compiler . expand_to_binary ( msgctxt , "msgctxt" , __MODULE__ , __CALLER__ )
150+
151+ msgid_plural =
152+ Gettext.Compiler . expand_to_binary ( msgid_plural , "msgid_plural" , __MODULE__ , __CALLER__ )
153+
154+ if Gettext.Extractor . extracting? ( ) do
155+ Gettext.Extractor . extract (
156+ __CALLER__ ,
157+ __MODULE__ ,
158+ domain ,
159+ msgctxt ,
160+ { msgid , msgid_plural } ,
161+ Gettext.Compiler . get_and_flush_extracted_comments ( )
162+ )
163+ end
164+
165+ { msgid , msgid_plural }
135166 end
136167
137168 defmacro dngettext_noop ( domain , msgid , msgid_plural ) do
138169 quote do
139- Gettext.Macros . dngettext_noop_with_backend (
140- __MODULE__ ,
170+ unquote ( __MODULE__ ) . dpngettext_noop (
141171 unquote ( domain ) ,
172+ nil ,
142173 unquote ( msgid ) ,
143174 unquote ( msgid_plural )
144175 )
145176 end
146177 end
147178
148179 defmacro pngettext_noop ( msgctxt , msgid , msgid_plural ) do
180+ domain = __gettext__ ( :default_domain )
181+
149182 quote do
150- Gettext.Macros . pngettext_noop_with_backend (
151- __MODULE__ ,
152- msgctxt ,
183+ unquote ( __MODULE__ ) . dpngettext_noop (
184+ unquote ( domain ) ,
185+ unquote ( msgctxt ) ,
153186 unquote ( msgid ) ,
154187 unquote ( msgid_plural )
155188 )
156189 end
157190 end
158191
159192 defmacro ngettext_noop ( msgid , msgid_plural ) do
193+ domain = __gettext__ ( :default_domain )
194+
160195 quote do
161- Gettext.Macros . ngettext_noop_with_backend (
162- __MODULE__ ,
196+ unquote ( __MODULE__ ) . dpngettext_noop (
197+ unquote ( domain ) ,
198+ nil ,
163199 unquote ( msgid ) ,
164200 unquote ( msgid_plural )
165201 )
@@ -168,31 +204,31 @@ defmodule Gettext.Compiler do
168204
169205 defmacro dpgettext ( domain , msgctxt , msgid , bindings \\ Macro . escape ( % { } ) ) do
170206 quote do
171- Gettext.Macros . dpgettext_with_backend (
172- __MODULE__ ,
207+ msgid =
208+ unquote ( __MODULE__ ) . dpgettext_noop ( unquote ( domain ) , unquote ( msgctxt ) , unquote ( msgid ) )
209+
210+ Gettext . dpgettext (
211+ unquote ( __MODULE__ ) ,
173212 unquote ( domain ) ,
174213 unquote ( msgctxt ) ,
175- unquote ( msgid ) ,
214+ msgid ,
176215 unquote ( bindings )
177216 )
178217 end
179218 end
180219
181220 defmacro dgettext ( domain , msgid , bindings \\ Macro . escape ( % { } ) ) do
182221 quote do
183- Gettext.Macros . dgettext_with_backend (
184- __MODULE__ ,
185- unquote ( domain ) ,
186- unquote ( msgid ) ,
187- unquote ( bindings )
188- )
222+ unquote ( __MODULE__ ) . dpgettext ( unquote ( domain ) , nil , unquote ( msgid ) , unquote ( bindings ) )
189223 end
190224 end
191225
192226 defmacro pgettext ( msgctxt , msgid , bindings \\ Macro . escape ( % { } ) ) do
227+ domain = __gettext__ ( :default_domain )
228+
193229 quote do
194- Gettext.Macros . pgettext_with_backend (
195- __MODULE__ ,
230+ unquote ( __MODULE__ ) . dpgettext (
231+ unquote ( domain ) ,
196232 unquote ( msgctxt ) ,
197233 unquote ( msgid ) ,
198234 unquote ( bindings )
@@ -201,19 +237,29 @@ defmodule Gettext.Compiler do
201237 end
202238
203239 defmacro gettext ( msgid , bindings \\ Macro . escape ( % { } ) ) do
240+ domain = __gettext__ ( :default_domain )
241+
204242 quote do
205- Gettext.Macros . gettext_with_backend ( __MODULE__ , unquote ( msgid ) , unquote ( bindings ) )
243+ unquote ( __MODULE__ ) . dpgettext ( unquote ( domain ) , nil , unquote ( msgid ) , unquote ( bindings ) )
206244 end
207245 end
208246
209247 defmacro dpngettext ( domain , msgctxt , msgid , msgid_plural , n , bindings \\ Macro . escape ( % { } ) ) do
210248 quote do
211- Gettext.Macros . dpngettext_with_backend (
212- __MODULE__ ,
249+ { msgid , msgid_plural } =
250+ unquote ( __MODULE__ ) . dpngettext_noop (
251+ unquote ( domain ) ,
252+ unquote ( msgctxt ) ,
253+ unquote ( msgid ) ,
254+ unquote ( msgid_plural )
255+ )
256+
257+ Gettext . dpngettext (
258+ unquote ( __MODULE__ ) ,
213259 unquote ( domain ) ,
214260 unquote ( msgctxt ) ,
215- unquote ( msgid ) ,
216- unquote ( msgid_plural ) ,
261+ msgid ,
262+ msgid_plural ,
217263 unquote ( n ) ,
218264 unquote ( bindings )
219265 )
@@ -222,9 +268,9 @@ defmodule Gettext.Compiler do
222268
223269 defmacro dngettext ( domain , msgid , msgid_plural , n , bindings \\ Macro . escape ( % { } ) ) do
224270 quote do
225- Gettext.Macros . dngettext_with_backend (
226- __MODULE__ ,
271+ unquote ( __MODULE__ ) . dpngettext (
227272 unquote ( domain ) ,
273+ nil ,
228274 unquote ( msgid ) ,
229275 unquote ( msgid_plural ) ,
230276 unquote ( n ) ,
@@ -234,9 +280,12 @@ defmodule Gettext.Compiler do
234280 end
235281
236282 defmacro ngettext ( msgid , msgid_plural , n , bindings \\ Macro . escape ( % { } ) ) do
283+ domain = __gettext__ ( :default_domain )
284+
237285 quote do
238- Gettext.Macros . ngettext_with_backend (
239- __MODULE__ ,
286+ unquote ( __MODULE__ ) . dpngettext (
287+ unquote ( domain ) ,
288+ nil ,
240289 unquote ( msgid ) ,
241290 unquote ( msgid_plural ) ,
242291 unquote ( n ) ,
@@ -246,9 +295,11 @@ defmodule Gettext.Compiler do
246295 end
247296
248297 defmacro pngettext ( msgctxt , msgid , msgid_plural , n , bindings \\ Macro . escape ( % { } ) ) do
298+ domain = __gettext__ ( :default_domain )
299+
249300 quote do
250- Gettext.Macros . pngettext_with_backend (
251- __MODULE__ ,
301+ unquote ( __MODULE__ ) . dpngettext (
302+ unquote ( domain ) ,
252303 unquote ( msgctxt ) ,
253304 unquote ( msgid ) ,
254305 unquote ( msgid_plural ) ,
@@ -259,11 +310,63 @@ defmodule Gettext.Compiler do
259310 end
260311
261312 defmacro gettext_comment ( comment ) do
262- Gettext.Macros . gettext_comment ( comment )
313+ comment = Gettext.Compiler . expand_to_binary ( comment , "comment" , __MODULE__ , __CALLER__ )
314+ Gettext.Compiler . append_extracted_comment ( comment )
315+ :ok
263316 end
264317 end
265318 end
266319
320+ @ doc false
321+ # TODO: remove me
322+ def expand_to_binary ( term , what , gettext_module , env )
323+ when what in ~w( domain msgctxt msgid msgid_plural comment) do
324+ raiser = fn term ->
325+ raise ArgumentError , """
326+ Gettext macros expect message keys (msgid and msgid_plural),
327+ domains, and comments to expand to strings at compile-time, but the given #{ what }
328+ doesn't. This is what the macro received:
329+
330+ #{ inspect ( term ) }
331+
332+ Dynamic messages should be avoided as they limit Gettext's
333+ ability to extract messages from your source code. If you are
334+ sure you need dynamic lookup, you can use the functions in the Gettext
335+ module:
336+
337+ string = "hello world"
338+ Gettext.gettext(#{ inspect ( gettext_module ) } , string)
339+ """
340+ end
341+
342+ # We support nil too in order to fall back to a nil context and always use the *p
343+ # variants of the Gettext macros.
344+ case Macro . expand ( term , env ) do
345+ term when is_binary ( term ) or is_nil ( term ) ->
346+ term
347+
348+ { :<<>> , _ , pieces } = term ->
349+ if Enum . all? ( pieces , & is_binary / 1 ) , do: Enum . join ( pieces ) , else: raiser . ( term )
350+
351+ other ->
352+ raiser . ( other )
353+ end
354+ end
355+
356+ @ doc false
357+ def append_extracted_comment ( comment ) do
358+ existing = Process . get ( :gettext_comments , [ ] )
359+ Process . put ( :gettext_comments , [ " " <> comment | existing ] )
360+ :ok
361+ end
362+
363+ @ doc false
364+ def get_and_flush_extracted_comments ( ) do
365+ Enum . reverse ( Process . delete ( :gettext_comments ) || [ ] )
366+ end
367+
368+ ## END of deprecated block
369+
267370 @ doc """
268371 Logs a warning via `Logger.error/1` if `domain` contains slashes.
269372
0 commit comments