-
Notifications
You must be signed in to change notification settings - Fork 260
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
Set locale based on Accept-Language header #420
Changes from 3 commits
3c14d91
bca00e5
39060ab
d4232c0
b9e531d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,28 @@ class ApplicationController < ActionController::Base | |
# For APIs, you may want to use :null_session instead. | ||
protect_from_forgery with: :exception | ||
before_action :mobile_filter_header | ||
before_action :set_locale | ||
|
||
def mobile_filter_header | ||
@mobile = true | ||
end | ||
|
||
def mobile_filter_header | ||
@mobile = true | ||
end | ||
|
||
def set_locale | ||
I18n.locale = extract_locale_from_accept_language_header | ||
end | ||
|
||
private | ||
def extract_locale_from_accept_language_header | ||
accept_language = request.env['HTTP_ACCEPT_LANGUAGE'] | ||
if accept_language.nil? | ||
"en" | ||
else | ||
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first | ||
end | ||
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. This sets the locale to be the top-preferred locale of the user's browser, whether we have that locale translated or not. Unfortunately, if the top-preferred locale is one we don't have, users see the translation key names in the page, rather than translated strings from an actual locale. I think we should
Can use a gem to handle this, or do it directly in custom Ruby logic. |
||
end | ||
|
||
end |
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.
Consider using
http_accept_language.
language_region_compatible_from
(I18n.available_locales)
?According to this README: