From dc4f33f71832abfd7332154076356633ec057c3a Mon Sep 17 00:00:00 2001 From: "Ben Sheldon [he/him]" Date: Sun, 9 Jul 2023 20:16:26 -0700 Subject: [PATCH] Create custom subclass of `I18n::Config` to separate GoodJob-specific locales and default locale (#1001) --- README.md | 2 ++ .../good_job/application_controller.rb | 24 +++++++++++------- .../good_job/cron_entries_controller.rb | 2 +- app/helpers/good_job/application_helper.rb | 1 - app/models/good_job/i18n_config.rb | 25 +++++++++++++++++++ app/views/good_job/shared/_navbar.erb | 2 +- lib/good_job/configuration.rb | 6 +++++ spec/lib/good_job/configuration_spec.rb | 8 ++++++ .../good_job/cron_entries_controller_spec.rb | 23 +++++++++++++++++ spec/test_app/config/application.rb | 4 +++ 10 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 app/models/good_job/i18n_config.rb diff --git a/README.md b/README.md index 00691382b..e3a8fc921 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,7 @@ Rails.application.configure do config.good_job.shutdown_timeout = 25 # seconds config.good_job.enable_cron = true config.good_job.cron = { example: { cron: '0 * * * *', class: 'ExampleJob' } } + config.good_job.dashboard_default_locale = :en # ...or all at once. config.good_job = { @@ -262,6 +263,7 @@ Rails.application.configure do class: 'ExampleJob' }, }, + dashboard_default_locale: :en, } end ``` diff --git a/app/controllers/good_job/application_controller.rb b/app/controllers/good_job/application_controller.rb index ee146418a..8287010f5 100644 --- a/app/controllers/good_job/application_controller.rb +++ b/app/controllers/good_job/application_controller.rb @@ -4,7 +4,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? @@ -31,8 +31,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 @@ -40,16 +53,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 829403cbd..3b31eb2ea 100644 --- a/app/controllers/good_job/cron_entries_controller.rb +++ b/app/controllers/good_job/cron_entries_controller.rb @@ -15,7 +15,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 f88d8a587..091b5bca8 100644 --- a/app/helpers/good_job/application_helper.rb +++ b/app/helpers/good_job/application_helper.rb @@ -64,7 +64,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/models/good_job/i18n_config.rb b/app/models/good_job/i18n_config.rb new file mode 100644 index 000000000..4eb4ac4af --- /dev/null +++ b/app/models/good_job/i18n_config.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module GoodJob + class I18nConfig < ::I18n::Config + BACKEND = I18n::Backend::Simple.new + AVAILABLE_LOCALES = GoodJob::Engine.root.join("config/locales").glob("*.yml").map { |path| File.basename(path, ".yml").to_sym }.uniq + AVAILABLE_LOCALES_SET = AVAILABLE_LOCALES.inject(Set.new) { |set, locale| set << locale.to_s << locale.to_sym } + + def backend + BACKEND + end + + def available_locales + AVAILABLE_LOCALES + end + + def available_locales_set + AVAILABLE_LOCALES_SET + end + + def default_locale + GoodJob.configuration.dashboard_default_locale + end + 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 @@