Skip to content

Commit

Permalink
Fix Cldr.Plug.SetLocale to allow locale names. Closes #131
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Oct 20, 2019
1 parent 1f5ef96 commit a2216bd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changelog for Cldr v2.11.1

This is the changelog for Cldr v2.11.1 released on October 20th, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)

### Bug Fixes

* Validate session-based locale in `Cldr.Plug.SetLocale`. Closes #131. Thanks for @Ray-Wang.

# Changelog for Cldr v2.11.0

This is the changelog for Cldr v2.11.0 released on October 10th, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
Expand Down
1 change: 1 addition & 0 deletions lib/cldr/plug/plug_set_locale.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ if Code.ensure_loaded?(Plug) do
defp fetch_param(conn, :session, _param, options) do
conn
|> get_session(options[:session_key])
|> Cldr.validate_locale(options[:cldr])
end

defp fetch_param(conn, :cookie, param, options) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.Mixfile do
use Mix.Project

@version "2.11.0"
@version "2.11.1"

def project do
[
Expand Down
36 changes: 34 additions & 2 deletions test/plug/plug_set_locale_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,42 @@ defmodule Cldr.Plug.SetLocale.Test do

conn =
:get
|> conn("/?locale=fr")
|> conn("/")
|> Plug.Session.call(session_opts)
|> fetch_session("cldr_locale")
|> put_session("cldr_locale", Cldr.Locale.new!("ru", opts[:cldr]))
|> Cldr.Plug.SetLocale.call(opts)

assert conn.private[:cldr_locale] ==
%Cldr.LanguageTag{
extensions: %{},
gettext_locale_name: nil,
locale: %{},
private_use: [],
transform: %{},
language_variant: nil,
canonical_locale_name: "ru-Cyrl-RU",
cldr_locale_name: "ru",
language: "ru",
rbnf_locale_name: "ru",
requested_locale_name: "ru",
script: "Cyrl",
territory: "RU"
}

assert Cldr.get_locale() == conn.private[:cldr_locale]
end

test "set the locale from the session using a locale name" do
opts = Cldr.Plug.SetLocale.init(from: :session, cldr: TestBackend.Cldr)
session_opts = Plug.Session.init(store: :cookie, key: "_key", signing_salt: "X")

conn =
:get
|> conn("/")
|> Plug.Session.call(session_opts)
|> fetch_session("cldr_locale")
|> put_session("cldr_locale", Cldr.Locale.new("ru", opts[:cldr]))
|> put_session("cldr_locale", "ru")
|> Cldr.Plug.SetLocale.call(opts)

assert conn.private[:cldr_locale] ==
Expand Down

0 comments on commit a2216bd

Please sign in to comment.