-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from shayonj/s/thread-reset-middleware
Introduce middleware that resets current local thread's config after each request
- Loading branch information
Showing
4 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module I18n | ||
class Middleware | ||
|
||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
@app.call(env) | ||
ensure | ||
Thread.current[:i18n_config] = I18n::Config.new | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'test_helper' | ||
|
||
class I18nMiddlewareTest < I18n::TestCase | ||
def setup | ||
super | ||
I18n.default_locale = :fr | ||
@app = DummyRackApp.new | ||
@middleware = I18n::Middleware.new(@app) | ||
end | ||
|
||
test "middleware initializes new config object after request" do | ||
old_i18n_config_object_id = Thread.current[:i18n_config].object_id | ||
@middleware.call({}) | ||
|
||
updated_i18n_config_object_id = Thread.current[:i18n_config].object_id | ||
assert_not_equal updated_i18n_config_object_id, old_i18n_config_object_id | ||
end | ||
|
||
test "succesfully resets i18n locale to default locale by defining new config" do | ||
@middleware.call({}) | ||
|
||
assert_equal :fr, I18n.locale | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters