New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce middleware that resets current local thread's config after each request #382
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this will cause the translations to be reloaded upon every single request. For applications with large amounts of translations, this can lead to an unnecessary slow down on every request.
Since your issue seems to be just that the default locale is not used again for
I18n.locale
, why not have here instead:?
I think this would accomplish what you wish.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@radar thanks for the review, I don't think it will cause the translations to be re-loaded, as
Backend::Simple
is initialized as a class variable and once rails is initialized, the same object is persisted/re-used:https://github.com/svenfuchs/i18n/blob/6ab51c78be833eb011d5b4d3f5b2c54e74847512/lib/i18n/config.rb#L19
You can test this by going to
rails console
and checking thatbackend
'sobject_id
is always the same:That said, since the main issue is of TLS in a multi-threaded environemt, updating
Thread.current
itself made sense :). Lmk, if I missed something.