Skip to content

Commit

Permalink
Add gettext
Browse files Browse the repository at this point in the history
  • Loading branch information
knewter committed Dec 22, 2015
1 parent 20169f9 commit 0e46bcd
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule ElixirStatus.Mixfile do
version: "0.0.1",
elixir: "~> 1.0",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix] ++ Mix.compilers,
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
Expand All @@ -18,7 +18,7 @@ defmodule ElixirStatus.Mixfile do
def application do
[mod: {ElixirStatus, []},
applications: [:phoenix, :phoenix_html, :cowboy, :logger,
:phoenix_ecto, :mariaex, :oauth2]]
:phoenix_ecto, :mariaex, :oauth2, :gettext]]
end

# Specifies which paths to compile per environment
Expand All @@ -44,7 +44,8 @@ defmodule ElixirStatus.Mixfile do
{:oauth2, "~> 0.3.0"},
{:oauth, github: "tim/erlang-oauth"},
{:extwitter, github: "rrrene/extwitter"},
{:scrivener, "~> 1.1"}
{:scrivener, "~> 1.1"},
{:gettext, "~> 0.9"}
]
end
end
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"ecto": {:hex, :ecto, "1.1.0"},
"extwitter": {:git, "https://github.com/rrrene/extwitter.git", "71f6f2819fcaf8220dfe42f0f4968909151186d8", []},
"fs": {:hex, :fs, "0.9.2"},
"gettext": {:hex, :gettext, "0.9.0"},
"hackney": {:hex, :hackney, "1.4.7"},
"html_sanitize_ex": {:hex, :html_sanitize_ex, "0.3.0"},
"httpoison": {:hex, :httpoison, "0.8.0"},
Expand Down
95 changes: 95 additions & 0 deletions priv/gettext/en/LC_MESSAGES/errors.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## `msgid`s in this file come from POT (.pot) files. Do not add, change, or
## remove `msgid`s manually here as they're tied to the ones in the
## corresponding POT file (with the same domain). Use `mix gettext.extract
## --merge` or `mix gettext.merge` to merge POT files into PO files.
msgid ""
msgstr ""
"Language: en\n"

## From Ecto.Changeset.cast/4
msgid "can't be blank"
msgstr ""

## From Ecto.Changeset.put_change/3
msgid "is invalid"
msgstr ""

## From Ecto.Changeset.validate_format/3
msgid "has invalid format"
msgstr ""

## From Ecto.Changeset.validate_subset/3
msgid "has an invalid entry"
msgstr ""

## From Ecto.Changeset.validate_exclusion/3
msgid "is reserved"
msgstr ""

## From Ecto.Changeset.validate_confirmation/3
msgid "does not match confirmation"
msgstr ""

## From Ecto.Changeset.no_assoc_constraint/3
msgid "is still associated to this entry"
msgstr ""

msgid "are still associated to this entry"
msgstr ""

## From Ecto.Changeset.validate_length/3
msgid "should be %{count} character(s)"
msgid_plural "should be %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have %{count} item(s)"
msgid_plural "should have %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should be at least %{count} character(s)"
msgid_plural "should be at least %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at least %{count} item(s)"
msgid_plural "should have at least %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should be at most %{count} character(s)"
msgid_plural "should be at most %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at most %{count} item(s)"
msgid_plural "should have at most %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

## From Ecto.Changeset.validate_number/3
msgid "must be less than %{count}"
msgid_plural "must be less than %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be greater than %{count}"
msgid_plural "must be greater than %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be less than or equal to %{count}"
msgid_plural "must be less than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be greater than or equal to %{count}"
msgid_plural "must be greater than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be equal to %{count}"
msgid_plural "must be equal to %{count}"
msgstr[0] ""
msgstr[1] ""
92 changes: 92 additions & 0 deletions priv/gettext/errors.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
## 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.
## From Ecto.Changeset.cast/4
msgid "can't be blank"
msgstr ""

## From Ecto.Changeset.put_change/3
msgid "is invalid"
msgstr ""

## From Ecto.Changeset.validate_format/3
msgid "has invalid format"
msgstr ""

## From Ecto.Changeset.validate_subset/3
msgid "has an invalid entry"
msgstr ""

## From Ecto.Changeset.validate_exclusion/3
msgid "is reserved"
msgstr ""

## From Ecto.Changeset.validate_confirmation/3
msgid "does not match confirmation"
msgstr ""

## From Ecto.Changeset.no_assoc_constraint/3
msgid "is still associated to this entry"
msgstr ""

msgid "are still associated to this entry"
msgstr ""

## From Ecto.Changeset.validate_length/3
msgid "should be %{count} character(s)"
msgid_plural "should be %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have %{count} item(s)"
msgid_plural "should have %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should be at least %{count} character(s)"
msgid_plural "should be at least %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at least %{count} item(s)"
msgid_plural "should have at least %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should be at most %{count} character(s)"
msgid_plural "should be at most %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at most %{count} item(s)"
msgid_plural "should have at most %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

## From Ecto.Changeset.validate_number/3
msgid "must be less than %{count}"
msgid_plural "must be less than %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be greater than %{count}"
msgid_plural "must be greater than %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be less than or equal to %{count}"
msgid_plural "must be less than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be greater than or equal to %{count}"
msgid_plural "must be greater than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""

msgid "must be equal to %{count}"
msgid_plural "must be equal to %{count}"
msgstr[0] ""
msgstr[1] ""
24 changes: 24 additions & 0 deletions web/gettext.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule ElixirStatus.Gettext do
@moduledoc """
A module providing Internationalization with a gettext-based API.
By using [Gettext](http://hexdocs.pm/gettext),
your module gains a set of macros for translations, for example:
import ElixirStatus.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](http://hexdocs.pm/gettext) for detailed usage.
"""
use Gettext, otp_app: :elixir_status
end
19 changes: 19 additions & 0 deletions web/views/changeset_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule ElixirStatus.ChangesetView do
use ElixirStatus.Web, :view

@doc """
Traverses and translates changeset errors.
See `Ecto.Changeset.traverse_errors/2` and
`ElixirStatus.ErrorHelpers.translate_error/1` for more details.
"""
def translate_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
end

def render("error.json", %{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: translate_errors(changeset)}
end
end
34 changes: 34 additions & 0 deletions web/views/error_helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule ElixirStatus.ErrorHelpers do
@moduledoc """
Conveniences for translating and building error messages.
"""
use Phoenix.HTML

@doc """
Generates tag for inlined form input errors.
"""
def error_tag(form, field) do
if error = form.errors[field] do
content_tag :span, translate_error(error), class: "help-block"
end
end

@doc """
Translates an error message using gettext.
"""
def translate_error({msg, opts}) do
# Because error messages were defined within Ecto, we must
# call the Gettext module passing our Gettext backend. We
# also use the "errors" domain as translations are placed
# in the errors.po file. On your own code and templates,
# this could be written simply as:
#
# dngettext "errors", "1 file", "%{count} files", count
#
Gettext.dngettext(ElixirStatus.Gettext, "errors", msg, msg, opts[:count], opts)
end

def translate_error(msg) do
Gettext.dgettext(ElixirStatus.Gettext, "errors", msg)
end
end
3 changes: 3 additions & 0 deletions web/web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ defmodule ElixirStatus.Web do
# Import URL helpers from the router
import ElixirStatus.Router.Helpers

import ElixirStatus.ErrorHelpers
import ElixirStatus.Gettext

# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML

Expand Down

0 comments on commit 0e46bcd

Please sign in to comment.