Skip to content
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

Merged
merged 5 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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:

language_region_compatible_from(languages): Returns the first of the user preferred languages that is also found in available languages. Finds best fit by matching on primary language first and secondarily on region. If no matching region is found, return the first language in the group matching that primary language.
vs:
compatible_language_from(languages): Returns the first of the user_preferred_languages that is compatible with the available locales. Ignores region.


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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

  • Go through each of the user's preferred locales and use the first one we have.
  • Find a way to revert to en when the user's requested locales aren't ones we have.

Can use a gem to handle this, or do it directly in custom Ruby logic.

end

end
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ class Application < Rails::Application
resource "/api/*", headers: :any, methods: [:get, :post, :options]
end
end

# I18n stuff
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

end
end