-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
[#1547] Add and use Current.locale #1623
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0ce23c5
Set Current.locale in set_current_locale before action
veganstraightedge 50abf66
Use Current.locale in app controller before action
veganstraightedge c11637f
Delete unused Locale.current method
veganstraightedge bd6dd1f
Split extract redirect_to_locale_subdomain from set_current_locale_fr…
veganstraightedge 5380968
Move set_current_theme before action to a middleware
veganstraightedge 8629ff5
Move current locale methods to CurrentLocale middleware
veganstraightedge a8c9ac3
Use Current.locale in header link to language page
veganstraightedge e97da9b
Use Current.locale in 2020 homepage footer link to lite mode
veganstraightedge 3e6ed68
Use Current.locale in 2017 footer link to lite mode
veganstraightedge f86aa40
Use Current.locale in language page subdomain switcher
veganstraightedge f868cb6
Use Current.locale in #preferred_localization
veganstraightedge c36db45
Delete Locale.english?
veganstraightedge edf4de4
Use Current.locale in TagsHelper
veganstraightedge 147c4bc
Use Current.locale in MarkdownHelper
veganstraightedge 0f90ac5
Use Current.locale in locale_nav_item_classes_for (2020 layout)
veganstraightedge edb00d6
Set Current.theme in spec_helper
veganstraightedge 17477cd
Set Current.locale before each spec
veganstraightedge cea8506
Set Current.locale before each spec in spec helper
veganstraightedge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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,86 @@ | ||
module Rack | ||
class CurrentLocale | ||
def initialize app | ||
@app = app | ||
end | ||
|
||
def call env | ||
@req = Rack::Request.new(env) | ||
|
||
set_current_locale | ||
set_current_locale_from_subdomain | ||
redirect_to_locale_subdomain | ||
|
||
@app.call(env) | ||
end | ||
|
||
private | ||
|
||
def set_current_locale | ||
Current.locale = ::Locale.find_by abbreviation: I18n.locale | ||
end | ||
|
||
def set_current_locale_from_subdomain | ||
return if subdomain.blank? | ||
return unless I18n.available_locales.include? subdomain.to_sym | ||
|
||
Current.locale = ::Locale.find_by abbreviation: subdomain | ||
end | ||
|
||
def redirect_to_locale_subdomain | ||
# Force the subdomain to match the locale. | ||
# Don’t do this in development, because typically local development | ||
# environments don’t support subdomains (en.localhost doesn’t resolve). | ||
|
||
# Don’t redirect to en.crimethinc.com | ||
return if Current.locale.english? | ||
|
||
# Don’t redirect if there’s a subdomain | ||
return if subdomain.present? | ||
|
||
# Don’t redirect in development | ||
return if Rails.env.production? | ||
|
||
redirect localized_url | ||
end | ||
|
||
def subdomain | ||
fully_qualified_domain = @req.host | ||
locale_subdomain = fully_qualified_domain.split('.').first | ||
|
||
locale_subdomain unless locale_subdomain == fully_qualified_domain | ||
end | ||
|
||
def decorated_subdomain | ||
"#{subdomain}." unless subdomain.blank? | ||
end | ||
|
||
def decorated_port | ||
":#{@req.port}" if Rails.env.development? | ||
end | ||
|
||
def decorated_query_params | ||
"?#{@req.query_string}" if @req.query_string.present? | ||
end | ||
|
||
def localized_url | ||
[ | ||
@req.scheme, | ||
'://', | ||
decorated_subdomain, | ||
@req.host, | ||
decorated_port, | ||
@req.path, | ||
decorated_query_params | ||
].join | ||
end | ||
|
||
def redirect location | ||
[ | ||
301, | ||
{ 'Location' => location, 'Content-Type' => 'text/html' }, | ||
['Moved Permanently'] | ||
] | ||
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,13 @@ | ||
module Rack | ||
class CurrentTheme | ||
def initialize app | ||
@app = app | ||
end | ||
|
||
def call env | ||
Current.theme = ENV.fetch('THEME') { '2017' } | ||
|
||
@app.call(env) | ||
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :user, :theme | ||
attribute :user, :locale, :theme | ||
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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -16,9 +16,9 @@ | |||||
<li class="nav-link"><%= link_to t("header.about"), "/about" %></li> | ||||||
</ul> | ||||||
|
||||||
<% unless Locale.english? %> | ||||||
<% unless Current.locale.english? %> | ||||||
<ul> | ||||||
<li class="nav-link"><%= link_to t('header.locale_articles'), language_path(@site_locale.canonical) %></li> | ||||||
<li class="nav-link"><%= link_to t('header.locale_articles'), language_path(Current.locale.name) %></li> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if this is the source of the test failures, but
Suggested change
|
||||||
</ul> | ||||||
<% end %> | ||||||
</nav> | ||||||
|
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
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.
this is why the tests are failing.
You are making it so that the routing logic now depends on the locale database tables to exist an be populated
this will be
nil
in the specs because there are no locales, and that will make line 36 fail.website/app/middlewares/rack/current_locale.rb
Line 36 in cea8506
you can get the specs passing by putting
before { create(:locale, :en) }
at the top of the failing specs, but this brings up a question for meshould middleware routing be tied to the state of the DB? and does making a database call outside of application code introduce any risks (security or otherwise)
I am not familiar enough with rack to really know, but if it's fine then adding the
before
in failing specs should do the trick