Skip to content

Commit

Permalink
Allow configuring a messages backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Vezoli authored and cpjolicoeur committed Apr 10, 2020
1 parent 2651a5d commit 04f9b65
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 56 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,46 @@ above. Just change the stylesheet link in the `torch.html.eex` layout.

If you want to use the theme, but override the colors, you'll need to include your
own stylesheet with the specific overrides.

## Internationalization

Torch comes with `.po` files for `en`, `ru` and `es` locales. If you wish to add more
translations you can configure it by creating a `MessagesBackend` module and then
adding it as a configuration on you `config.exs`. You can find the all needed messages
on [messages_backend.ex](lib/torch/messages_backend.ex).

**Example**

```elixir
defmodule MyApp.Torch.MessagesBackend do
import MyAppWeb.Gettext, only: [dgettext: 2]

def message("Contains"), do: dgettext("torch", "Contains")
def message("Equals"), do: dgettext("torch", "Equals")
def message("Choose one"), do: dgettext("torch", "Choose one")
def message("Before"), do: dgettext("torch", "Before")
def message("After"), do: dgettext("torch", "After")
def message("Greater Than"), do: dgettext("torch", "Greater Than")
def message("Greater Than Or Equal"), do: dgettext("torch", "Greater Than Or Equal")
def message("Less Than"), do: dgettext("torch", "Less Than")
def message("start"), do: dgettext("torch", "start")
def message("end"), do: dgettext("torch", "end")
def message("Select Date"), do: dgettext("torch", "Select Date")
def message("Select Start Date"), do: dgettext("torch", "Select Start Date")
def message("Select End Date"), do: dgettext("torch", "Select End Date")
def message("< Prev"), do: dgettext("torch", "< Prev")
def message("Next >"), do: dgettext("torch", "Next >")
# ...

# You can add a fallback so it won't break with new added texts
def message(text), do: Torch.MessagesBackend.message(text)
end
```

```elixir
# config.exs
config :torch,
otp_app: :my_app_name,
messages_backend: MyApp.Torch.MessagesBackend
template_format: "eex" || "slim"
```
4 changes: 4 additions & 0 deletions lib/torch/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ defmodule Torch.Config do
def template_format do
Application.get_env(:torch, :template_format)
end

def messages_backend do
Application.get_env(:torch, :messages_backend, Torch.MessagesBackend)
end
end
24 changes: 24 additions & 0 deletions lib/torch/messages_backend.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Torch.MessagesBackend do
@moduledoc """
Provides messages for different parts of the package. Can
also be overridden to include custom translations.
"""

import Torch.Gettext, only: [dgettext: 2]

def message("Contains"), do: dgettext("default", "Contains")
def message("Equals"), do: dgettext("default", "Equals")
def message("Choose one"), do: dgettext("default", "Choose one")
def message("Before"), do: dgettext("default", "Before")
def message("After"), do: dgettext("default", "After")
def message("Greater Than"), do: dgettext("default", "Greater Than")
def message("Greater Than Or Equal"), do: dgettext("default", "Greater Than Or Equal")
def message("Less Than"), do: dgettext("default", "Less Than")
def message("start"), do: dgettext("default", "start")
def message("end"), do: dgettext("default", "end")
def message("Select Date"), do: dgettext("default", "Select Date")
def message("Select Start Date"), do: dgettext("default", "Select Start Date")
def message("Select End Date"), do: dgettext("default", "Select End Date")
def message("< Prev"), do: dgettext("default", "< Prev")
def message("Next >"), do: dgettext("default", "Next >")
end
30 changes: 15 additions & 15 deletions lib/torch/views/filter_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Torch.FilterView do

use Phoenix.HTML

import Torch.Gettext, only: [dgettext: 2]
import Torch.Config, only: [messages_backend: 0]

@type prefix :: atom | String.t()
@type field :: atom | String.t()
Expand All @@ -27,7 +27,7 @@ defmodule Torch.FilterView do
:"#{field}_equals",
options,
value: params[to_string(prefix)]["#{field}_equals"],
prompt: dgettext("default", "Choose one")
prompt: messages_backend().message("Choose one")
)
end

Expand All @@ -47,8 +47,8 @@ defmodule Torch.FilterView do
{selected, _value} = find_param(params[prefix_str], field)

opts = [
{dgettext("default", "Contains"), "#{prefix}[#{field}_contains]"},
{dgettext("default", "Equals"), "#{prefix}[#{field}_equals]"}
{messages_backend().message("Contains"), "#{prefix}[#{field}_contains]"},
{messages_backend().message("Equals"), "#{prefix}[#{field}_equals]"}
]

select(:filters, "", opts, class: "filter-type", value: "#{prefix}[#{selected}]")
Expand All @@ -70,8 +70,8 @@ defmodule Torch.FilterView do
{selected, _value} = find_param(params[prefix_str], field)

opts = [
{dgettext("default", "Before"), "#{prefix}[#{field}_before]"},
{dgettext("default", "After"), "#{prefix}[#{field}_after]"}
{messages_backend().message("Before"), "#{prefix}[#{field}_before]"},
{messages_backend().message("After"), "#{prefix}[#{field}_after]"}
]

select(:filters, "", opts, class: "filter-type", value: "#{prefix}[#{selected}]")
Expand All @@ -92,10 +92,10 @@ defmodule Torch.FilterView do
{selected, _value} = find_param(params[prefix_str], field)

opts = [
{dgettext("default", "Equals"), "#{prefix}[#{field}_equals]"},
{dgettext("default", "Greater Than"), "#{prefix}[#{field}_greater_than]"},
{dgettext("default", "Greater Than Or Equal"), "#{prefix}[#{field}_greater_than_or]"},
{dgettext("default", "Less Than"), "#{prefix}[#{field}_less_than]"}
{messages_backend().message("Equals"), "#{prefix}[#{field}_equals]"},
{messages_backend().message("Greater Than"), "#{prefix}[#{field}_greater_than]"},
{messages_backend().message("Greater Than Or Equal"), "#{prefix}[#{field}_greater_than_or]"},
{messages_backend().message("Less Than"), "#{prefix}[#{field}_less_than]"}
]

select(:filters, "", opts, class: "filter-type", value: "#{prefix}[#{selected}]")
Expand Down Expand Up @@ -174,14 +174,14 @@ defmodule Torch.FilterView do
torch_date_input(
"#{prefix}[#{field}_between][start]",
get_in(params, [prefix, "#{field}_between", "start"]),
dgettext("default", "start")
messages_backend().message("start")
)

{:safe, ending} =
torch_date_input(
"#{prefix}[#{field}_between][end]",
get_in(params, [prefix, "#{field}_between", "end"]),
dgettext("default", "end")
messages_backend().message("end")
)

raw(start ++ ending)
Expand Down Expand Up @@ -227,7 +227,7 @@ defmodule Torch.FilterView do
class: "datepicker",
name: name,
value: value,
placeholder: dgettext("default", "Select Date")
placeholder: messages_backend().message("Select Date")
)
end

Expand All @@ -238,7 +238,7 @@ defmodule Torch.FilterView do
class: "datepicker start",
name: name,
value: value,
placeholder: dgettext("default", "Select Start Date")
placeholder: messages_backend().message("Select Start Date")
)
end

Expand All @@ -249,7 +249,7 @@ defmodule Torch.FilterView do
class: "datepicker end",
name: name,
value: value,
placeholder: dgettext("default", "Select End Date")
placeholder: messages_backend().message("Select End Date")
)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/torch/views/pagination_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Torch.PaginationView do
use Phoenix.View, root: "lib/torch/templates"
use Phoenix.HTML

import Torch.Gettext, only: [dgettext: 2]
import Torch.Config, only: [messages_backend: 0]
import Torch.TableView

@doc """
Expand All @@ -26,7 +26,7 @@ defmodule Torch.PaginationView do
"""
def prev_link(conn, current_page, sort_opts \\ nil) do
if current_page != 1 do
link(dgettext("default", "< Prev"),
link(messages_backend().message("< Prev"),
to: "?" <> querystring(conn, page: current_page - 1, sort_opts: sort_opts)
)
end
Expand All @@ -46,7 +46,7 @@ defmodule Torch.PaginationView do
"""
def next_link(conn, current_page, num_pages, sort_opts \\ nil) do
if current_page != num_pages do
link(dgettext("default", "Next >"),
link(messages_backend().message("Next >"),
to: "?" <> querystring(conn, page: current_page + 1, sort_opts: sort_opts)
)
end
Expand Down
41 changes: 27 additions & 14 deletions priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,76 @@ msgid ""
msgstr ""

#, elixir-format
#: lib/torch/views/pagination_view.ex:29
#: lib/torch/messages_backend.ex:17
msgid "< Prev"
msgstr ""

#, elixir-format
#: lib/torch/views/pagination_view.ex:47
#: lib/torch/messages_backend.ex:18
msgid "Next >"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:29
#: lib/torch/views/filter_view.ex:174
#: lib/torch/messages_backend.ex:6
msgid "Choose one"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:50
#: lib/torch/views/filter_view.ex:71
#: lib/torch/messages_backend.ex:5
msgid "Equals"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:49
#: lib/torch/messages_backend.ex:4
msgid "Contains"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:72
#: lib/torch/messages_backend.ex:9
msgid "Greater Than"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:73
#: lib/torch/messages_backend.ex:10
msgid "Greater Than Or Equal"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:74
#: lib/torch/messages_backend.ex:11
msgid "Less Than"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:139
#: lib/torch/messages_backend.ex:12
msgid "start"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:146
#: lib/torch/messages_backend.ex:13
msgid "end"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:185
#: lib/torch/messages_backend.ex:15
msgid "Select Start Date"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:196
#: lib/torch/messages_backend.ex:16
msgid "Select End Date"
msgstr ""

#, elixir-format
#: lib/torch/messages_backend.ex:8
msgid "After"
msgstr ""

#, elixir-format
#: lib/torch/messages_backend.ex:7
msgid "Before"
msgstr ""

#, elixir-format
#: lib/torch/messages_backend.ex:14
msgid "Select Date"
msgstr ""
39 changes: 27 additions & 12 deletions priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
@@ -1,59 +1,74 @@
#, elixir-format
#: lib/torch/views/pagination_view.ex:29
#: lib/torch/messages_backend.ex:17
msgid "< Prev"
msgstr ""

#, elixir-format
#: lib/torch/views/pagination_view.ex:47
#: lib/torch/messages_backend.ex:18
msgid "Next >"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:29 lib/torch/views/filter_view.ex:174
#: lib/torch/messages_backend.ex:6
msgid "Choose one"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:50 lib/torch/views/filter_view.ex:71
#: lib/torch/messages_backend.ex:5
msgid "Equals"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:49
#: lib/torch/messages_backend.ex:4
msgid "Contains"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:72
#: lib/torch/messages_backend.ex:9
msgid "Greater Than"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:73
#: lib/torch/messages_backend.ex:10
msgid "Greater Than Or Equal"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:74
#: lib/torch/messages_backend.ex:11
msgid "Less Than"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:139
#: lib/torch/messages_backend.ex:12
msgid "start"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:146
#: lib/torch/messages_backend.ex:13
msgid "end"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:185
#: lib/torch/messages_backend.ex:15
msgid "Select Start Date"
msgstr ""

#, elixir-format
#: lib/torch/views/filter_view.ex:196
#: lib/torch/messages_backend.ex:16
msgid "Select End Date"
msgstr ""

#, elixir-format
#: lib/torch/messages_backend.ex:8
msgid "After"
msgstr ""

#, elixir-format
#: lib/torch/messages_backend.ex:7
msgid "Before"
msgstr ""

#, elixir-format, fuzzy
#: lib/torch/messages_backend.ex:14
msgid "Select Date"
msgstr ""
Loading

0 comments on commit 04f9b65

Please sign in to comment.