Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecation message #406

Merged
merged 5 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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