From 9706979540529a752b100d6407d443a8b5b9a91f Mon Sep 17 00:00:00 2001 From: "Ben Sheldon [he/him]" Date: Fri, 7 Jul 2023 11:42:00 -0700 Subject: [PATCH] Create custom subclass of `I18n::Config` to separate GoodJob-specific locales --- .../good_job/application_controller.rb | 24 ++++++++++++------- .../good_job/cron_entries_controller.rb | 2 +- app/helpers/good_job/application_helper.rb | 1 - app/views/good_job/shared/_navbar.erb | 2 +- lib/good_job.rb | 1 + lib/good_job/i18n_config.rb | 24 +++++++++++++++++++ .../good_job/cron_entries_controller_spec.rb | 23 ++++++++++++++++++ spec/test_app/config/application.rb | 4 ++++ 8 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 lib/good_job/i18n_config.rb diff --git a/app/controllers/good_job/application_controller.rb b/app/controllers/good_job/application_controller.rb index fc5a36ed1..c31670ee5 100644 --- a/app/controllers/good_job/application_controller.rb +++ b/app/controllers/good_job/application_controller.rb @@ -3,7 +3,7 @@ module GoodJob class ApplicationController < ActionController::Base protect_from_forgery with: :exception - around_action :switch_locale + around_action :use_good_job_locale content_security_policy do |policy| policy.default_src(:none) if policy.default_src(*policy.default_src).blank? @@ -30,8 +30,21 @@ def default_url_options(options = {}) { locale: I18n.locale }.merge(options) end - def switch_locale(&action) + def use_good_job_locale(&action) + @original_i18n_config = I18n.config + I18n.config = ::GoodJob::I18nConfig.new I18n.with_locale(current_locale, &action) + ensure + I18n.config = @original_i18n_config + @original_i18n_config = nil + end + + def use_original_locale + prev_config = I18n.config + I18n.config = @original_i18n_config if @original_i18n_config + yield + ensure + I18n.config = prev_config end def current_locale @@ -39,16 +52,9 @@ def current_locale request.GET['locale'] elsif params[:locale] params[:locale] - elsif good_job_available_locales.exclude?(I18n.default_locale) && I18n.available_locales.include?(:en) - :en else I18n.default_locale end end - - def good_job_available_locales - @_good_job_available_locales ||= GoodJob::Engine.root.join("config/locales").glob("*.yml").map { |path| File.basename(path, ".yml").to_sym }.uniq - end - helper_method :good_job_available_locales end end diff --git a/app/controllers/good_job/cron_entries_controller.rb b/app/controllers/good_job/cron_entries_controller.rb index ed408cd39..5f52dbb26 100644 --- a/app/controllers/good_job/cron_entries_controller.rb +++ b/app/controllers/good_job/cron_entries_controller.rb @@ -14,7 +14,7 @@ def show def enqueue @cron_entry = CronEntry.find(params[:cron_key]) - @cron_entry.enqueue(Time.current) + use_original_locale { @cron_entry.enqueue(Time.current) } redirect_back(fallback_location: cron_entries_path, notice: t(".notice")) end diff --git a/app/helpers/good_job/application_helper.rb b/app/helpers/good_job/application_helper.rb index 8ecb59569..2188f48df 100644 --- a/app/helpers/good_job/application_helper.rb +++ b/app/helpers/good_job/application_helper.rb @@ -63,7 +63,6 @@ def translate_hash(key, **options) end def translation_exists?(key, **options) - true if good_job_available_locales.include?(I18n.locale) I18n.exists?(scope_key_by_partial(key), **options) end end diff --git a/app/views/good_job/shared/_navbar.erb b/app/views/good_job/shared/_navbar.erb index bc6c218aa..94898ea8a 100644 --- a/app/views/good_job/shared/_navbar.erb +++ b/app/views/good_job/shared/_navbar.erb @@ -49,7 +49,7 @@