Skip to content

Commit

Permalink
Improve a deprecation warning (#406)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Hanks <hanks.brad@gmail.com>
  • Loading branch information
BradS2S and bradhanks authored Dec 7, 2024
1 parent 30fe469 commit 647d27f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dgettext("errors", "Here is an error message to translate")

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`).

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:
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:

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

`.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].
`.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].

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")`.

Expand Down
16 changes: 11 additions & 5 deletions lib/gettext.ex
Original file line number Diff line number Diff line change
Expand Up @@ -636,19 +636,25 @@ defmodule Gettext do

_other ->
# TODO: remove this once we stop supporting the old way of defining backends.
otp_app = Keyword.get(opts, :otp_app, :my_app)

IO.warn(
"""
defining a Gettext backend by calling
Defining a Gettext backend by calling:
use Gettext, otp_app: ...
use Gettext, otp_app: #{inspect(otp_app)}
is deprecated. To define a backend, call:
use Gettext.Backend, otp_app: :my_app
use Gettext.Backend, otp_app: #{inspect(otp_app)}
Then, replace importing your backend:
import #{inspect(__CALLER__.module)}
Then, instead of importing your backend, call this in your module:
with calling this in your module:
use Gettext, backend: MyApp.Gettext
use Gettext, backend: #{inspect(__CALLER__.module)}
""",
Macro.Env.stacktrace(__CALLER__)
)
Expand Down
24 changes: 22 additions & 2 deletions test/gettext_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,27 @@ defmodule GettextTest do
)
end)

assert stderr =~ "defining a Gettext backend by calling"
assert stderr =~ "is deprecated"
expected_message = """
Defining a Gettext backend by calling:
use Gettext, otp_app: :my_app
is deprecated. To define a backend, call:
use Gettext.Backend, otp_app: :my_app
Then, replace importing your backend:
import DeprecatedWayOfDefiningBackend
with calling this in your module:
use Gettext, backend: DeprecatedWayOfDefiningBackend
nofile:1: DeprecatedWayOfDefiningBackend (module)
"""

assert stderr =~ expected_message
end
end

0 comments on commit 647d27f

Please sign in to comment.