Skip to content

Commit

Permalink
Merge pull request #102 from solov9ev/feature/101-add-i18n-support
Browse files Browse the repository at this point in the history
Add I18N support.
  • Loading branch information
danielberkompas authored Feb 2, 2019
2 parents 6f92a93 + 1885e19 commit 79ee3cc
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 18 deletions.
18 changes: 18 additions & 0 deletions lib/gettext.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Torch.Gettext do
@moduledoc """
A module providing Internationalization with a gettext-based API.
By using [Gettext](https://hexdocs.pm/gettext),
your module gains a set of macros for translations, for example:
import ExchangeWeb.Gettext
# Simple translation
gettext("Here is the string to translate")
# Plural translation
ngettext("Here is the string to translate",
"Here are the strings to translate",
3)
# Domain-based translation
dgettext("errors", "Here is the error message to translate")
See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
"""
use Gettext, otp_app: :torch
end
26 changes: 14 additions & 12 deletions lib/torch/views/filter_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Torch.FilterView do

use Phoenix.HTML

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

@type prefix :: atom | String.t()
@type field :: atom | String.t()

Expand All @@ -24,7 +26,7 @@ defmodule Torch.FilterView do
:"#{field}_equals",
options,
value: params[to_string(prefix)]["#{field}_equals"],
prompt: "Choose one"
prompt: dgettext("default", "Choose one")
)
end

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

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

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

opts = [
{"Equals", "#{prefix}[#{field}_equals]"},
{"Greater Than", "#{prefix}[#{field}_greater_than]"},
{"Greater Than Or Equal", "#{prefix}[#{field}_greater_than_or]"},
{"Less Than", "#{prefix}[#{field}_less_than]"}
{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]"}
]

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

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

raw(start ++ ending)
Expand Down Expand Up @@ -169,7 +171,7 @@ defmodule Torch.FilterView do
:"#{field}_equals",
[{"True", true}, {"False", false}],
value: value,
prompt: "Choose one"
prompt: dgettext("default", "Choose one")
)
end

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

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

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

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

@doc """
Expand All @@ -25,7 +26,9 @@ defmodule Torch.PaginationView do
"""
def prev_link(conn, current_page, sort_opts \\ nil) do
if current_page != 1 do
link("< Prev", to: "?" <> querystring(conn, page: current_page - 1, sort_opts: sort_opts))
link(dgettext("default", "< Prev"),
to: "?" <> querystring(conn, page: current_page - 1, sort_opts: sort_opts)
)
end
end

Expand All @@ -43,12 +46,14 @@ defmodule Torch.PaginationView do
"""
def next_link(conn, current_page, num_pages, sort_opts \\ nil) do
if current_page != num_pages do
link("Next >", to: "?" <> querystring(conn, page: current_page + 1, sort_opts: sort_opts))
link(dgettext("default", "Next >"),
to: "?" <> querystring(conn, page: current_page + 1, sort_opts: sort_opts)
)
end
end

defp start_page(current_page, distance)
when current_page - distance <= 0 do
when current_page - distance <= 0 do
1
end

Expand Down Expand Up @@ -76,4 +81,4 @@ defmodule Torch.PaginationView do
defp sort_opts(%{sort_field: sort_field, sort_direction: sort_direction}) do
%{sort_field: sort_field, sort_direction: sort_direction}
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Torch.MixProject do
version: "2.0.0-rc.1",
elixir: "~> 1.5",
start_permanent: Mix.env() == :prod,
compilers: [:phoenix] ++ Mix.compilers(),
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
name: "Torch",
description: "Rapid admin generator for Phoenix",
source_url: "https://github.com/danielberkompas/torch",
Expand Down Expand Up @@ -42,6 +42,7 @@ defmodule Torch.MixProject do
[
{:phoenix, "~> 1.3"},
{:phoenix_html, "~> 2.10"},
{:gettext, "~> 0.16", override: true},
{:scrivener_ecto, ">= 1.2.1"},
{:filtrex, "~> 0.4.1"},
{:excoveralls, ">= 0.0.0", only: [:dev, :test]},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"excoveralls": {:hex, :excoveralls, "0.8.1", "0bbf67f22c7dbf7503981d21a5eef5db8bbc3cb86e70d3798e8c802c74fa5e27", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
"filtrex": {:hex, :filtrex, "0.4.1", "fea45c814534778969342d346552d1bf213807d31410b413fbff6f81bc7f03bd", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: false]}, {:timex, "~> 3.1", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm"},
"gettext": {:hex, :gettext, "0.15.0", "40a2b8ce33a80ced7727e36768499fc9286881c43ebafccae6bab731e2b2b8ce", [:mix], [], "hexpm"},
"gettext": {:hex, :gettext, "0.16.1", "e2130b25eebcbe02bb343b119a07ae2c7e28bd4b146c4a154da2ffb2b3507af2", [:mix], [], "hexpm"},
"hackney": {:hex, :hackney, "1.11.0", "4951ee019df102492dabba66a09e305f61919a8a183a7860236c0fde586134b6", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm"},
Expand Down
73 changes: 73 additions & 0 deletions priv/gettext/default.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## This file is a PO Template file.
##
## `msgid`s here are often extracted from source code.
## Add new translations manually only if they're dynamic
## translations that can't be statically extracted.
##
## Run `mix gettext.extract` to bring this file up to
## date. Leave `msgstr`s empty as changing them here as no
## effect: edit them in PO (`.po`) files instead.
msgid ""
msgstr ""

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

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

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

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

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

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

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

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

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

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

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

#, elixir-format
#: lib/torch/views/filter_view.ex:196
msgid "Select End Date"
msgstr ""
59 changes: 59 additions & 0 deletions priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#, elixir-format
#: lib/torch/views/pagination_view.ex:29
msgid "< Prev"
msgstr ""

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

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

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

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

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

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

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

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

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

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

#, elixir-format
#: lib/torch/views/filter_view.ex:196
msgid "Select End Date"
msgstr ""
59 changes: 59 additions & 0 deletions priv/gettext/ru/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#, elixir-format
#: lib/torch/views/pagination_view.ex:29
msgid "< Prev"
msgstr "< Предыдущая"

#, elixir-format
#: lib/torch/views/pagination_view.ex:47
msgid "Next >"
msgstr "Следующая >"

#, elixir-format
#: lib/torch/views/filter_view.ex:29 lib/torch/views/filter_view.ex:174
msgid "Choose one"
msgstr "Выберите один элемент"

#, elixir-format
#: lib/torch/views/filter_view.ex:50 lib/torch/views/filter_view.ex:71
msgid "Equals"
msgstr "Равно"

#, elixir-format
#: lib/torch/views/filter_view.ex:49
msgid "Contains"
msgstr "Содержит"

#, elixir-format
#: lib/torch/views/filter_view.ex:72
msgid "Greater Than"
msgstr "Больше"

#, elixir-format
#: lib/torch/views/filter_view.ex:73
msgid "Greater Than Or Equal"
msgstr "Больше или равно"

#, elixir-format
#: lib/torch/views/filter_view.ex:74
msgid "Less Than"
msgstr "Меньше"

#, elixir-format
#: lib/torch/views/filter_view.ex:139
msgid "start"
msgstr "начало"

#, elixir-format
#: lib/torch/views/filter_view.ex:146
msgid "end"
msgstr "конец"

#, elixir-format
#: lib/torch/views/filter_view.ex:185
msgid "Select Start Date"
msgstr "Выберите дату начала"

#, elixir-format
#: lib/torch/views/filter_view.ex:196
msgid "Select End Date"
msgstr "Выберите дату окончания"
Loading

0 comments on commit 79ee3cc

Please sign in to comment.