From c2fb80d4d9482cc9e923d2434556f2aadc4cbd3c Mon Sep 17 00:00:00 2001 From: Rodrigo Rosenfeld Rosas Date: Wed, 7 Jan 2015 11:35:45 -0200 Subject: [PATCH] Use Devise translations when inheriting from core controllers closes #3367 --- CHANGELOG.md | 2 ++ app/controllers/devise/confirmations_controller.rb | 4 ++++ app/controllers/devise/omniauth_callbacks_controller.rb | 4 ++++ app/controllers/devise/passwords_controller.rb | 4 ++++ app/controllers/devise/registrations_controller.rb | 4 ++++ app/controllers/devise/sessions_controller.rb | 4 ++++ app/controllers/devise/unlocks_controller.rb | 3 +++ app/controllers/devise_controller.rb | 9 ++++++++- 8 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39221519..18ef44b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ * `RegistrationsController#new` and `SessionsController#new` now yields the current resource (by @mtarnovan, @deivid-rodriguez) * Password length validation is now limited to 72 characters for newer apps (by @lleger) + * Controllers inheriting from any Devise core controller will now use appropriate translations. + The i18n scope can be overridden in `translation_scope`. ### 3.4.1 - 2014-10-29 diff --git a/app/controllers/devise/confirmations_controller.rb b/app/controllers/devise/confirmations_controller.rb index ec4aff14..f04f6098 100644 --- a/app/controllers/devise/confirmations_controller.rb +++ b/app/controllers/devise/confirmations_controller.rb @@ -44,4 +44,8 @@ class Devise::ConfirmationsController < DeviseController new_session_path(resource_name) end end + + def translation_scope + 'devise.confirmations' + end end diff --git a/app/controllers/devise/omniauth_callbacks_controller.rb b/app/controllers/devise/omniauth_callbacks_controller.rb index 92e40676..38c6857e 100644 --- a/app/controllers/devise/omniauth_callbacks_controller.rb +++ b/app/controllers/devise/omniauth_callbacks_controller.rb @@ -27,4 +27,8 @@ class Devise::OmniauthCallbacksController < DeviseController def after_omniauth_failure_path_for(scope) new_session_path(scope) end + + def translation_scope + 'devise.omniauth_callbacks' + end end diff --git a/app/controllers/devise/passwords_controller.rb b/app/controllers/devise/passwords_controller.rb index 5e3d3285..c97d22da 100644 --- a/app/controllers/devise/passwords_controller.rb +++ b/app/controllers/devise/passwords_controller.rb @@ -68,4 +68,8 @@ class Devise::PasswordsController < DeviseController resource.respond_to?(:unlock_strategy_enabled?) && resource.unlock_strategy_enabled?(:email) end + + def translation_scope + 'devise.passwords' + end end diff --git a/app/controllers/devise/registrations_controller.rb b/app/controllers/devise/registrations_controller.rb index 4e21eb3f..42b0f006 100644 --- a/app/controllers/devise/registrations_controller.rb +++ b/app/controllers/devise/registrations_controller.rb @@ -140,4 +140,8 @@ class Devise::RegistrationsController < DeviseController def account_update_params devise_parameter_sanitizer.sanitize(:account_update) end + + def translation_scope + 'devise.registrations' + end end diff --git a/app/controllers/devise/sessions_controller.rb b/app/controllers/devise/sessions_controller.rb index c0dd1fb4..d6a7a28a 100644 --- a/app/controllers/devise/sessions_controller.rb +++ b/app/controllers/devise/sessions_controller.rb @@ -46,6 +46,10 @@ class Devise::SessionsController < DeviseController { scope: resource_name, recall: "#{controller_path}#new" } end + def translation_scope + 'devise.sessions' + end + private # Check if there is no signed in user before doing the sign out. diff --git a/app/controllers/devise/unlocks_controller.rb b/app/controllers/devise/unlocks_controller.rb index 903dcdbb..bcc2b16b 100644 --- a/app/controllers/devise/unlocks_controller.rb +++ b/app/controllers/devise/unlocks_controller.rb @@ -43,4 +43,7 @@ class Devise::UnlocksController < DeviseController new_session_path(resource) if is_navigational_format? end + def translation_scope + 'devise.unlocks' + end end diff --git a/app/controllers/devise_controller.rb b/app/controllers/devise_controller.rb index 91c6209f..c8cb0b40 100644 --- a/app/controllers/devise_controller.rb +++ b/app/controllers/devise_controller.rb @@ -167,13 +167,20 @@ MESSAGE # Get message for given def find_message(kind, options = {}) - options[:scope] ||= "devise.#{controller_name}" + options[:scope] ||= translation_scope options[:default] = Array(options[:default]).unshift(kind.to_sym) options[:resource_name] = resource_name options = devise_i18n_options(options) I18n.t("#{options[:resource_name]}.#{kind}", options) end + # Controllers inheriting DeviseController are advised to override this + # method so that other controllers inheriting from them would use + # existing translations. + def translation_scope + "devise.#{controller_name}" + end + def clean_up_passwords(object) object.clean_up_passwords if object.respond_to?(:clean_up_passwords) end