changed CHANGELOG.md
 
@@ -1,3 +1,19 @@
1
+ # Changelog for Cldr v2.22.0
2
+
3
+ This is the changelog for Cldr v2.22.0 released on May 20th, 2021. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
4
+
5
+ ## Soft deprecation
6
+
7
+ * The plug `Cldr.Plug.SetSession` was introduced in `ex_cldr` version 2.21.0. However the convention in Phoenix and Plug is `put`, not `set`. The plug is renamed to `Cldr.Plug.PutSession`. `Cldr.Plug.SetSession` is still available but will emit a deprecation notice and will delegatge to `Cldr.Plug.PutSession.` Apologies to all for the sloppy release review process.
8
+
9
+ ## Bug Fixes
10
+
11
+ * Fix typespec of Cldr.AcceptLanguage.best_match/2. Thanks to @adriankumpf.
12
+
13
+ ## Enhancements
14
+
15
+ * Make log level for "no match" errors in `Cldr.Plug.AcceptLanguage` configurable.
16
+
1
17
# Changelog for Cldr v2.21.0
2
18
3
19
This is the changelog for Cldr v2.21.0 released on May 17th, 2021. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
changed hex_metadata.config
 
@@ -27,8 +27,8 @@
27
27
<<"lib/cldr/language_tag/extensions/t.ex">>,
28
28
<<"lib/cldr/language_tag/rfc5646_parser.ex">>,
29
29
<<"lib/cldr/language_tag/parser.ex">>,<<"lib/cldr/language_tag.ex">>,
30
- <<"lib/cldr/plug">>,<<"lib/cldr/plug/plug_accept_language.ex">>,
31
- <<"lib/cldr/plug/plug_set_session.ex">>,
30
+ <<"lib/cldr/plug">>,<<"lib/cldr/plug/plug_put_session.ex">>,
31
+ <<"lib/cldr/plug/plug_accept_language.ex">>,
32
32
<<"lib/cldr/plug/plug_set_locale.ex">>,<<"lib/cldr/locale.ex">>,
33
33
<<"src/plural_rules_lexer.xrl">>,<<"src/plural_rules_parser.yrl">>,
34
34
<<"config">>,<<"config/dev.exs">>,<<"config/config.exs">>,
 
@@ -55,10 +55,10 @@
55
55
{<<"licenses">>,[<<"Apache 2.0">>]}.
56
56
{<<"links">>,
57
57
[{<<"Changelog">>,
58
- <<"https://github.com/elixir-cldr/cldr/blob/v2.21.0/CHANGELOG.md">>},
58
+ <<"https://github.com/elixir-cldr/cldr/blob/v2.22.0/CHANGELOG.md">>},
59
59
{<<"GitHub">>,<<"https://github.com/elixir-cldr/cldr">>},
60
60
{<<"Readme">>,
61
- <<"https://github.com/elixir-cldr/cldr/blob/v2.21.0/README.md">>}]}.
61
+ <<"https://github.com/elixir-cldr/cldr/blob/v2.22.0/README.md">>}]}.
62
62
{<<"name">>,<<"ex_cldr">>}.
63
63
{<<"requirements">>,
64
64
[[{<<"app">>,<<"cldr_utils">>},
 
@@ -106,4 +106,4 @@
106
106
{<<"optional">>,true},
107
107
{<<"repository">>,<<"hexpm">>},
108
108
{<<"requirement">>,<<"~> 0.4.0">>}]]}.
109
- {<<"version">>,<<"2.21.0">>}.
109
+ {<<"version">>,<<"2.22.0">>}.
changed lib/cldr/accept_language.ex
 
@@ -391,7 +391,8 @@ defmodule Cldr.AcceptLanguage do
391
391
392
392
"""
393
393
@spec best_match(String.t(), Cldr.backend()) ::
394
- {:ok, LanguageTag.t()} | {:error, {Cldr.AcceptLanguageError, String.t()}}
394
+ {:ok, LanguageTag.t()}
395
+ | {:error, {Cldr.AcceptLanguageError | Cldr.NoMatchingLocale, String.t()}}
395
396
396
397
def best_match(accept_language, backend) when is_binary(accept_language) do
397
398
with {:ok, languages} <- parse(accept_language, backend) do
changed lib/cldr/plug/plug_accept_language.ex
 
@@ -12,6 +12,11 @@ if Code.ensure_loaded?(Plug) do
12
12
option is provided and no default backend is configured
13
13
then an exception will be raised.
14
14
15
+ * `:no_match_log_level` determines the logging level for
16
+ the case when no matching locale is configured to meet the users
17
+ request. The default is `:warn`. If set to `nil` then no logging
18
+ is performed.
19
+
15
20
## Example
16
21
17
22
# Using a specific backend to validate
 
@@ -29,20 +34,23 @@ if Code.ensure_loaded?(Plug) do
29
34
require Logger
30
35
31
36
@language_header "accept-language"
37
+ @default_log_level :warn
32
38
33
39
@doc false
34
40
def init(options \\ []) do
35
- Keyword.get_lazy(options, :cldr_backend, &Cldr.default_backend!/0)
41
+ backend = Keyword.get_lazy(options, :cldr_backend, &Cldr.default_backend!/0)
42
+ log_level = Keyword.get(options, :no_match_log_level, @default_log_level)
43
+ %{backend: backend, log_level: log_level}
36
44
end
37
45
38
46
@doc false
39
- def call(conn, backend) do
47
+ def call(conn, options) do
40
48
case get_req_header(conn, @language_header) do
41
49
[accept_language] ->
42
- put_private(conn, :cldr_locale, best_match(accept_language, backend))
50
+ put_private(conn, :cldr_locale, best_match(accept_language, options))
43
51
44
52
[accept_language | _] ->
45
- put_private(conn, :cldr_locale, best_match(accept_language, backend))
53
+ put_private(conn, :cldr_locale, best_match(accept_language, options))
46
54
47
55
[] ->
48
56
put_private(conn, :cldr_locale, nil)
 
@@ -58,13 +66,17 @@ if Code.ensure_loaded?(Plug) do
58
66
nil
59
67
end
60
68
61
- def best_match(accept_language, backend) do
62
- case Cldr.AcceptLanguage.best_match(accept_language, backend) do
69
+ def best_match(accept_language, options) do
70
+ case Cldr.AcceptLanguage.best_match(accept_language, options.backend) do
63
71
{:ok, locale} ->
64
72
locale
65
73
74
+ {:error, {Cldr.NoMatchingLocale = exception, reason}} ->
75
+ if options.log_level, do: Logger.log(options.log_level,"#{inspect exception}: #{reason}")
76
+ nil
77
+
66
78
{:error, {exception, reason}} ->
67
- Logger.warn("#{exception}: #{reason}")
79
+ Logger.warn("#{inspect exception}: #{reason}")
68
80
nil
69
81
end
70
82
end
added lib/cldr/plug/plug_put_session.ex
 
@@ -0,0 +1,66 @@
1
+ if Code.ensure_loaded?(Plug) do
2
+ defmodule Cldr.Plug.SetSession do
3
+ @moduledoc false
4
+
5
+ @deprecated "Please use Cldr.Plug.PutSession"
6
+ defdelegate init(options), to: Cldr.Plug.PutSession
7
+
8
+ defdelegate call(conn, options), to: Cldr.Plug.PutSession
9
+ end
10
+
11
+ defmodule Cldr.Plug.PutSession do
12
+ @moduledoc """
13
+ Puts the CLDR locale name in the session.
14
+
15
+ The session
16
+ key is fixed to be `cldr_locale` in order that downstream
17
+ functions like those in `liveview` don't have to
18
+ be passed options.
19
+
20
+ ## Examples
21
+
22
+ # Define a router module that
23
+ # sets the locale for the current process
24
+ # and then also sets it in the session
25
+ defmodule MyAppWeb.Router do
26
+ use MyAppWeb, :router
27
+
28
+ pipeline :browser do
29
+ plug :accepts, ["html"]
30
+ plug :fetch_session
31
+ plug Cldr.Plug.SetLocale,
32
+ apps: [:cldr, :gettext],
33
+ from: [:path, :query],
34
+ gettext: MyApp.Gettext,
35
+ cldr: MyApp.Cldr
36
+ plug Cldr.Plug.PutSession
37
+ plug :fetch_flash
38
+ plug :protect_from_forgery
39
+ plug :put_secure_browser_headers
40
+ end
41
+ end
42
+
43
+ """
44
+
45
+ import Plug.Conn
46
+ alias Cldr.Plug.SetLocale
47
+
48
+ @doc false
49
+ def init(_options) do
50
+ []
51
+ end
52
+
53
+ @doc false
54
+ def call(conn, _options) do
55
+ case SetLocale.get_cldr_locale(conn) do
56
+ %Cldr.LanguageTag{cldr_locale_name: cldr_locale} ->
57
+ conn
58
+ |> fetch_session()
59
+ |> put_session(SetLocale.session_key(), cldr_locale)
60
+
61
+ _other ->
62
+ conn
63
+ end
64
+ end
65
+ end
66
+ end
removed lib/cldr/plug/plug_set_session.ex
 
@@ -1,56 +0,0 @@
1
- if Code.ensure_loaded?(Plug) do
2
- defmodule Cldr.Plug.SetSession do
3
- @moduledoc """
4
- Sets a session key for the CLDR locale.
5
-
6
- The session
7
- key is fixed to be `cldr_locale` in order that downstream
8
- functions like those in `liveview` don't have to
9
- be passed options.
10
-
11
- ## Examples
12
-
13
- # Define a router module that
14
- # sets the locale for the current process
15
- # and then also sets it in the session
16
- defmodule MyAppWeb.Router do
17
- use MyAppWeb, :router
18
-
19
- pipeline :browser do
20
- plug :accepts, ["html"]
21
- plug :fetch_session
22
- plug Cldr.Plug.SetLocale,
23
- apps: [:cldr, :gettext],
24
- from: [:path, :query],
25
- gettext: MyApp.Gettext,
26
- cldr: MyApp.Cldr
27
- plug :Cldr.Plug.SetSession
28
- plug :fetch_flash
29
- plug :protect_from_forgery
30
- plug :put_secure_browser_headers
31
- end
32
- end
33
-
34
- """
35
-
36
- import Plug.Conn
37
- alias Cldr.Plug.SetLocale
38
-
39
- @doc false
40
- def init(_options) do
41
- []
42
- end
43
-
44
- @doc false
45
- def call(conn, _options) do
46
- case SetLocale.get_cldr_locale(conn) do
47
- %{cldr_locale_name: cldr_locale} ->
48
- conn
49
- |> fetch_session()
50
- |> put_session(SetLocale.session_key(), cldr_locale)
51
- _other ->
52
- conn
53
- end
54
- end
55
- end
56
- end
\ No newline at end of file
changed mix.exs
 
@@ -1,7 +1,7 @@
1
1
defmodule Cldr.Mixfile do
2
2
use Mix.Project
3
3
4
- @version "2.21.0"
4
+ @version "2.22.0"
5
5
6
6
def project do
7
7
[