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: 11 additions & 2 deletions lib/localize/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ defmodule Localize.Plug do

@session_key Localize.Plug.PutLocale.session_key()

defmodule NoSessionLocaleError do
@moduledoc """
Returned by `Localize.Plug.put_locale_from_session/2` when the session has no locale.
"""

defexception message: "No locale was found in the session"
end

@doc """
Puts the locale from the session into the current process.

Expand All @@ -29,7 +37,8 @@ defmodule Localize.Plug do

* `{:ok, locale}` or

* `{:error, {exception, reason}}`
* `{:error, exception}`. A session without a locale returns a
`Localize.Plug.NoSessionLocaleError`.

### Examples

Expand Down Expand Up @@ -61,7 +70,7 @@ defmodule Localize.Plug do
end

def put_locale_from_session(_session, _options) do
{:error, {Localize.UnknownLocaleError, "No locale was found in the session"}}
{:error, %NoSessionLocaleError{}}
end

# A locale that Gettext has no translations for is a configuration
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ defmodule LocalizeWeb.MixProject do
groups_for_modules: [
Plugs: [
Localize.Plug,
Localize.Plug.NoSessionLocaleError,
Localize.Plug.PutLocale,
Localize.Plug.PutSession,
Localize.Plug.AcceptLanguage,
Expand Down
7 changes: 5 additions & 2 deletions test/localize_web_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ defmodule LocalizeWebTest do
assert Localize.Plug.PutLocale.locale_from_host(nil) == nil
end

test "put_locale_from_session returns error when no locale in session" do
assert {:error, _} = Localize.Plug.put_locale_from_session(%{})
test "put_locale_from_session returns a NoSessionLocaleError for an empty session" do
assert {:error, %Localize.Plug.NoSessionLocaleError{} = error} =
Localize.Plug.put_locale_from_session(%{})

assert Exception.message(error) == "No locale was found in the session"
end

test "Phoenix.HTML.Safe protocol for Localize.LanguageTag" do
Expand Down
Loading