Skip to content

Commit

Permalink
Create a custom I18n config class to set available locales
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rosa committed Dec 2, 2024
1 parent 87c7c8b commit 1edf500
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
11 changes: 11 additions & 0 deletions app/controllers/mission_control/jobs/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions app/helpers/mission_control/jobs/dates_helper.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lib/mission_control/jobs/i18n_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MissionControl::Jobs::I18nConfig < ::I18n::Config
def available_locales
[ :en ]
end
end
23 changes: 19 additions & 4 deletions test/controllers/jobs_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,16 +104,31 @@ 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

I18n.with_locale(:nl) do
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

0 comments on commit 1edf500

Please sign in to comment.