Commit b7abbd1dadb3cab0b674f6c2467ee7f375120522
Committed by
Rafael Manzo
1 parent
105876e8
Exists in
colab
and in
4 other branches
Improve code for selection of default locale
Showing
2 changed files
with
17 additions
and
19 deletions
Show diff stats
app/controllers/application_controller.rb
| ... | ... | @@ -10,22 +10,24 @@ class ApplicationController < ActionController::Base |
| 10 | 10 | before_filter :configure_permitted_parameters, if: :devise_controller? |
| 11 | 11 | before_filter :set_locale |
| 12 | 12 | |
| 13 | - # This is necessary for correct devise routing with locales: https://github.com/plataformatec/devise/wiki/How-To:--Redirect-with-locale-after-authentication-failure | |
| 14 | - def self.default_url_options | |
| 15 | - merge_locale_to_options(super()) | |
| 13 | + class << self | |
| 14 | + # This is necessary for correct devise routing with locales: https://github.com/plataformatec/devise/wiki/How-To:--Redirect-with-locale-after-authentication-failure | |
| 15 | + def default_url_options | |
| 16 | + locale_options | |
| 17 | + end | |
| 18 | + | |
| 19 | + def locale_options | |
| 20 | + { locale: I18n.locale } | |
| 21 | + end | |
| 16 | 22 | end |
| 17 | 23 | |
| 18 | 24 | # This happens after the *_url *_path helpers |
| 19 | - def default_url_options(options = {}) | |
| 20 | - self.class.merge_locale_to_options(options) | |
| 25 | + def default_url_options | |
| 26 | + self.class.locale_options | |
| 21 | 27 | end |
| 22 | 28 | |
| 23 | 29 | protected |
| 24 | 30 | |
| 25 | - def self.merge_locale_to_options(options) | |
| 26 | - { locale: I18n.locale }.merge options | |
| 27 | - end | |
| 28 | - | |
| 29 | 31 | # :nocov: |
| 30 | 32 | def configure_permitted_parameters |
| 31 | 33 | devise_parameter_sanitizer.for(:sign_up) << :name |
| ... | ... | @@ -33,14 +35,10 @@ class ApplicationController < ActionController::Base |
| 33 | 35 | end |
| 34 | 36 | |
| 35 | 37 | def set_locale |
| 36 | - unless params[:locale].nil? | |
| 37 | - I18n.locale = params[:locale] | |
| 38 | - else | |
| 39 | - compatible_locale = http_accept_language.compatible_language_from(I18n.available_locales) | |
| 40 | - unless compatible_locale.nil? | |
| 41 | - I18n.locale = compatible_locale | |
| 42 | - end | |
| 43 | - end | |
| 38 | + I18n.locale = ( | |
| 39 | + params[:locale] || | |
| 40 | + http_accept_language.compatible_language_from(I18n.available_locales) || | |
| 41 | + I18n.default_locale | |
| 42 | + ) | |
| 44 | 43 | end |
| 45 | - | |
| 46 | 44 | end | ... | ... |
config/application.rb
| ... | ... | @@ -18,7 +18,7 @@ module Mezuro |
| 18 | 18 | |
| 19 | 19 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. |
| 20 | 20 | config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s] |
| 21 | - # config.i18n.default_locale = :de | |
| 21 | + config.i18n.default_locale = :en | |
| 22 | 22 | config.i18n.enforce_available_locales = true |
| 23 | 23 | |
| 24 | 24 | # Do not swallow errors in after_commit/after_rollback callbacks. | ... | ... |