Skip to content
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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,24 @@ You can change where toasts are anchored by passing the `corner` setting to `toa

### Internationalization

You can provide translations for the defaul error toasts by adding the following to your `config.exs`:
LiveToast can translate its built-in connection notices. Enable this by adding the following to your `config.exs`:

```elixir
config :live_toast,
gettext_backend: MyApp.Gettext
```

Without this configuration, LiveToast renders its built-in connection notices in English.

`send_toast/3`, `put_toast/4`, and custom connection-notification copy render the message and title you provide unchanged. Translate application-owned copy before passing it to LiveToast:

```elixir
LiveToast.send_toast(
:error,
MyApp.Gettext.dgettext("errors", "Failed to send the email")
)
```

You have to create a `live_toast.po` file, inside the `priv/gettext/<language>/LC_MESSAGES/` folder for each language you want to support.

For example, if you want to support spanish, you would create the file `live_toast.po` in the `priv/gettext/es/LC_MESSAGES/` folder, with the following content:
Expand Down
3 changes: 0 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ if Mix.env() == :dev do
~w(--format=iife --target=es2016 --global-name=LiveMotion --minify --outfile=../priv/static/live_toast.min.js)
)
end

config :live_toast,
gettext_backend: LiveToast.Gettext
16 changes: 8 additions & 8 deletions lib/live_toast/components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ defmodule LiveToast.Components do
toast_class_fn={@toast_class_fn}
id="client-error"
kind={@connection_notifications.client_error.kind}
title={Utility.translate(@connection_notifications.client_error.title)}
title={@connection_notifications.client_error.title}
duration={0}
dismissible={false}
data-live-toast-connection="client_error"
Expand All @@ -182,7 +182,7 @@ defmodule LiveToast.Components do
hidden
>
<%= if @client_error == [] do %>
{Utility.translate(@connection_notifications.client_error.body)}
{@connection_notifications.client_error.body}
<Utility.svg name="hero-arrow-path" class="inline-block ml-1 h-3 w-3 animate-spin" />
<% else %>
{render_slot(
Expand All @@ -198,7 +198,7 @@ defmodule LiveToast.Components do
toast_class_fn={@toast_class_fn}
id="server-error"
kind={@connection_notifications.server_error.kind}
title={Utility.translate(@connection_notifications.server_error.title)}
title={@connection_notifications.server_error.title}
duration={0}
dismissible={false}
data-live-toast-connection="server_error"
Expand All @@ -211,7 +211,7 @@ defmodule LiveToast.Components do
hidden
>
<%= if @server_error == [] do %>
{Utility.translate(@connection_notifications.server_error.body)}
{@connection_notifications.server_error.body}
<Utility.svg name="hero-arrow-path" class="inline-block ml-1 h-3 w-3 animate-spin" />
<% else %>
{render_slot(
Expand Down Expand Up @@ -285,13 +285,13 @@ defmodule LiveToast.Components do
defaults = %{
client_error: %{
kind: :error,
title: "We can't find the internet",
body: "Attempting to reconnect"
title: Utility.translate("We can't find the internet"),
body: Utility.translate("Attempting to reconnect")
},
server_error: %{
kind: :error,
title: "Something went wrong!",
body: "Hang in there while we get back on track"
title: Utility.translate("Something went wrong!"),
body: Utility.translate("Hang in there while we get back on track")
}
}

Expand Down
4 changes: 0 additions & 4 deletions lib/live_toast/gettext.ex

This file was deleted.

5 changes: 2 additions & 3 deletions lib/live_toast/live_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule LiveToast.LiveComponent do
use Phoenix.LiveComponent

alias LiveToast.Components
alias LiveToast.Utility

@client_toast_option_keys ~w(duration metadata title)

Expand Down Expand Up @@ -130,10 +129,10 @@ defmodule LiveToast.LiveComponent do
action={action}
metadata={metadata}
corner={@corner}
title={if title, do: Utility.translate(title), else: nil}
title={title}
target={@myself}
>
{Utility.translate(msg)}
{msg}
</Components.toast>
</div>

Expand Down
7 changes: 4 additions & 3 deletions lib/live_toast/utility.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ defmodule LiveToast.Utility do
end

def translate(message) do
:live_toast
|> Application.get_env(:gettext_backend, LiveToast.Gettext)
|> Gettext.dgettext("live_toast", message)
case Application.get_env(:live_toast, :gettext_backend) do
nil -> message
backend -> Gettext.dgettext(backend, "live_toast", message)
end
end
end
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ defmodule LiveToast.MixProject do
lib/live_toast/components.ex
lib/live_toast/live_component.ex
lib/live_toast/utility.ex
lib/live_toast/gettext.ex
CHANGELOG.md
LICENSE.md
mix.exs
Expand Down
Loading