From 1edf500436a8c2def1559028a32ba4a537889679 Mon Sep 17 00:00:00 2001 From: Rosa Gutierrez Date: Mon, 2 Dec 2024 23:07:51 +0100 Subject: [PATCH] Create a custom I18n config class to set available locales Without interferring with the host application's I18n configuration. I18n's available_locales unfortunately is not thread-safe so we can't just change that, but I18n.config is thread-safe [1], so we can use our own there, that just relies on all I18n defaults except for available locales. [1] https://github.com/ruby-i18n/i18n/blob/3b65f6548245411bc9802f5a547954d370b57821/lib/i18n.rb#L56-L64 --- .../jobs/application_controller.rb | 11 +++++++++ .../mission_control/jobs/dates_helper.rb | 10 +++++--- lib/mission_control/jobs/i18n_config.rb | 5 ++++ test/controllers/jobs_controller_test.rb | 23 +++++++++++++++---- 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 lib/mission_control/jobs/i18n_config.rb diff --git a/app/controllers/mission_control/jobs/application_controller.rb b/app/controllers/mission_control/jobs/application_controller.rb index 8b9963bb..caa81256 100644 --- a/app/controllers/mission_control/jobs/application_controller.rb +++ b/app/controllers/mission_control/jobs/application_controller.rb @@ -12,8 +12,19 @@ class MissionControl::Jobs::ApplicationController < MissionControl::Jobs.base_co include MissionControl::Jobs::ApplicationScoped, MissionControl::Jobs::NotFoundRedirections include MissionControl::Jobs::AdapterFeatures + around_action :set_current_locale + private def default_url_options { server_id: MissionControl::Jobs::Current.server } end + + def set_current_locale(&block) + @previous_config = I18n.config + I18n.config = MissionControl::Jobs::I18nConfig.new + I18n.with_locale(:en, &block) + ensure + I18n.config = @previous_config + @previous_config = nil + end end diff --git a/app/helpers/mission_control/jobs/dates_helper.rb b/app/helpers/mission_control/jobs/dates_helper.rb index 115d5760..ad8a63ad 100644 --- a/app/helpers/mission_control/jobs/dates_helper.rb +++ b/app/helpers/mission_control/jobs/dates_helper.rb @@ -1,15 +1,19 @@ module MissionControl::Jobs::DatesHelper def time_distance_in_words_with_title(time) - tag.span time_ago_in_words(time, include_seconds: true), title: "Since #{time.to_fs(:long)}" + tag.span time_ago_in_words_with_default_options(time), title: "Since #{time.to_fs(:long)}" end def bidirectional_time_distance_in_words_with_title(time) time_distance = if time.past? - "#{distance_of_time_in_words_to_now(time, include_seconds: true)} ago" + "#{time_ago_in_words_with_default_options(time)} ago" else - "in #{distance_of_time_in_words_to_now(time, include_seconds: true)}" + "in #{time_ago_in_words_with_default_options(time)}" end tag.span time_distance, title: time.to_fs(:long) end + + def time_ago_in_words_with_default_options(time) + time_ago_in_words(time, include_seconds: true, locale: :en) + end end diff --git a/lib/mission_control/jobs/i18n_config.rb b/lib/mission_control/jobs/i18n_config.rb new file mode 100644 index 00000000..442d61f6 --- /dev/null +++ b/lib/mission_control/jobs/i18n_config.rb @@ -0,0 +1,5 @@ +class MissionControl::Jobs::I18nConfig < ::I18n::Config + def available_locales + [ :en ] + end +end diff --git a/test/controllers/jobs_controller_test.rb b/test/controllers/jobs_controller_test.rb index e78f999a..54a36120 100644 --- a/test/controllers/jobs_controller_test.rb +++ b/test/controllers/jobs_controller_test.rb @@ -32,7 +32,7 @@ class MissionControl::Jobs::JobsControllerTest < ActionDispatch::IntegrationTest assert_response :ok assert_select "tr.job", 2 - assert_select "tr.job", /AutoRetryingJob\s+Enqueued less than a minute ago\s+default/ + assert_select "tr.job", /AutoRetryingJob\s+Enqueued less than 5 seconds ago\s+default/ get mission_control_jobs.application_job_url(@application, job.job_id) assert_response :ok @@ -104,8 +104,8 @@ class MissionControl::Jobs::JobsControllerTest < ActionDispatch::IntegrationTest end end - test "get jobs and job details the default locale is set to another language than English" do - I18n.available_locales = %i[en nl] + test "get jobs and job details when the default locale is set to another language than English" do + previous_locales, I18n.available_locales = I18n.available_locales, %i[ en nl ] DummyJob.set(wait: 3.minutes).perform_later @@ -113,7 +113,22 @@ class MissionControl::Jobs::JobsControllerTest < ActionDispatch::IntegrationTest get mission_control_jobs.application_jobs_url(@application, :scheduled) assert_response :ok - assert_select "tr.job", /DummyJob\s+Enqueued less than a minute ago\s+queue_1\s+in 3 minutes/ + assert_select "tr.job", /DummyJob\s+Enqueued less than 5 seconds ago\s+queue_1\s+in 3 minutes/ end + ensure + I18n.available_locales = previous_locales + end + + test "get jobs and job details when English is not included among the locales" do + previous_locales, I18n.available_locales = I18n.available_locales, %i[ es nl ] + + DummyJob.set(wait: 3.minutes).perform_later + + get mission_control_jobs.application_jobs_url(@application, :scheduled) + assert_response :ok + + assert_select "tr.job", /DummyJob\s+Enqueued less than 5 seconds ago\s+queue_1\s+in 3 minutes/ + ensure + I18n.available_locales = previous_locales end end