Skip to content

Commit 647d27f

Browse files
BradS2Sbradhanks
andauthored
Improve a deprecation warning (#406)
Co-authored-by: Brad Hanks <hanks.brad@gmail.com>
1 parent 30fe469 commit 647d27f

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dgettext("errors", "Here is an error message to translate")
4848

4949
Messages in Gettext are stored in Portable Object files (`.po`). Such files must be placed at `priv/gettext/LOCALE/LC_MESSAGES/DOMAIN.po`, where `LOCALE` is the locale and `DOMAIN` is the domain (the default domain is called `default`).
5050

51-
For example, the message to `pt_BR` of the first two `*gettext` calls in the snippet above must be placed in the `priv/gettext/pt_BR/LC_MESSAGES/default.po` file with contents:
51+
For example, the messages for `pt_BR` from the first two `*gettext` calls in the snippet above must be placed in the `priv/gettext/pt_BR/LC_MESSAGES/default.po` file with the following contents:
5252

5353
```pot
5454
msgid "Here is one string to translate"
@@ -60,7 +60,7 @@ msgstr[0] "Aqui está o texto para traduzir"
6060
msgstr[1] "Aqui estão os textos para traduzir"
6161
```
6262

63-
`.po` are text-based files and can be edited directly by translators. Some may even use existing tools for managing them, such as [Poedit][poedit] or [poeditor.com][poeditor.com].
63+
`.po` files are text-based and can be edited directly by translators. Some may even use existing tools for managing them, such as [Poedit][poedit] or [poeditor.com][poeditor.com].
6464

6565
Finally, because messages are based on strings, your source code does not lose readability as you still see literal strings, like `gettext("here is an example")`, instead of paths like `translate("some.path.convention")`.
6666

lib/gettext.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,19 +636,25 @@ defmodule Gettext do
636636

637637
_other ->
638638
# TODO: remove this once we stop supporting the old way of defining backends.
639+
otp_app = Keyword.get(opts, :otp_app, :my_app)
640+
639641
IO.warn(
640642
"""
641-
defining a Gettext backend by calling
643+
Defining a Gettext backend by calling:
642644
643-
use Gettext, otp_app: ...
645+
use Gettext, otp_app: #{inspect(otp_app)}
644646
645647
is deprecated. To define a backend, call:
646648
647-
use Gettext.Backend, otp_app: :my_app
649+
use Gettext.Backend, otp_app: #{inspect(otp_app)}
650+
651+
Then, replace importing your backend:
652+
653+
import #{inspect(__CALLER__.module)}
648654
649-
Then, instead of importing your backend, call this in your module:
655+
with calling this in your module:
650656
651-
use Gettext, backend: MyApp.Gettext
657+
use Gettext, backend: #{inspect(__CALLER__.module)}
652658
""",
653659
Macro.Env.stacktrace(__CALLER__)
654660
)

test/gettext_test.exs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,27 @@ defmodule GettextTest do
279279
)
280280
end)
281281

282-
assert stderr =~ "defining a Gettext backend by calling"
283-
assert stderr =~ "is deprecated"
282+
expected_message = """
283+
Defining a Gettext backend by calling:
284+
285+
use Gettext, otp_app: :my_app
286+
287+
is deprecated. To define a backend, call:
288+
289+
use Gettext.Backend, otp_app: :my_app
290+
291+
Then, replace importing your backend:
292+
293+
import DeprecatedWayOfDefiningBackend
294+
295+
with calling this in your module:
296+
297+
use Gettext, backend: DeprecatedWayOfDefiningBackend
298+
299+
nofile:1: DeprecatedWayOfDefiningBackend (module)
300+
301+
"""
302+
303+
assert stderr =~ expected_message
284304
end
285305
end

0 commit comments

Comments
 (0)