Skip to content

Commit 1d1877c

Browse files
committed
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
1 parent cfca33a commit 1d1877c

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

app/controllers/mission_control/jobs/application_controller.rb

+11
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@ class MissionControl::Jobs::ApplicationController < MissionControl::Jobs.base_co
1212
include MissionControl::Jobs::ApplicationScoped, MissionControl::Jobs::NotFoundRedirections
1313
include MissionControl::Jobs::AdapterFeatures
1414

15+
around_action :set_current_locale
16+
1517
private
1618
def default_url_options
1719
{ server_id: MissionControl::Jobs::Current.server }
1820
end
21+
22+
def set_current_locale(&block)
23+
@previous_config = I18n.config
24+
I18n.config = MissionControl::Jobs::I18nConfig.new
25+
I18n.with_locale(:en, &block)
26+
ensure
27+
I18n.config = @previous_config
28+
@previous_config = nil
29+
end
1930
end
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
module MissionControl::Jobs::DatesHelper
22
def time_distance_in_words_with_title(time)
3-
tag.span time_ago_in_words(time, include_seconds: true), title: "Since #{time.to_fs(:long)}"
3+
tag.span time_ago_in_words_with_default_options(time), title: "Since #{time.to_fs(:long)}"
44
end
55

66
def bidirectional_time_distance_in_words_with_title(time)
77
time_distance = if time.past?
8-
"#{distance_of_time_in_words_to_now(time, include_seconds: true)} ago"
8+
"#{time_ago_in_words_with_default_options(time)} ago"
99
else
10-
"in #{distance_of_time_in_words_to_now(time, include_seconds: true)}"
10+
"in #{time_ago_in_words_with_default_options(time)}"
1111
end
1212

1313
tag.span time_distance, title: time.to_fs(:long)
1414
end
15+
16+
def time_ago_in_words_with_default_options(time)
17+
time_ago_in_words(time, include_seconds: true, locale: :en)
18+
end
1519
end
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class MissionControl::Jobs::I18nConfig < ::I18n::Config
2+
def available_locales
3+
[ :en ]
4+
end
5+
end

test/controllers/jobs_controller_test.rb

+19-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class MissionControl::Jobs::JobsControllerTest < ActionDispatch::IntegrationTest
3232
assert_response :ok
3333

3434
assert_select "tr.job", 2
35-
assert_select "tr.job", /AutoRetryingJob\s+Enqueued less than a minute ago\s+default/
35+
assert_select "tr.job", /AutoRetryingJob\s+Enqueued less than 5 seconds ago\s+default/
3636

3737
get mission_control_jobs.application_job_url(@application, job.job_id)
3838
assert_response :ok
@@ -104,16 +104,31 @@ class MissionControl::Jobs::JobsControllerTest < ActionDispatch::IntegrationTest
104104
end
105105
end
106106

107-
test "get jobs and job details the default locale is set to another language than English" do
108-
I18n.available_locales = %i[en nl]
107+
test "get jobs and job details when the default locale is set to another language than English" do
108+
previous_locales, I18n.available_locales = I18n.available_locales, %i[ en nl ]
109109

110110
DummyJob.set(wait: 3.minutes).perform_later
111111

112112
I18n.with_locale(:nl) do
113113
get mission_control_jobs.application_jobs_url(@application, :scheduled)
114114
assert_response :ok
115115

116-
assert_select "tr.job", /DummyJob\s+Enqueued less than a minute ago\s+queue_1\s+in 3 minutes/
116+
assert_select "tr.job", /DummyJob\s+Enqueued less than 5 seconds ago\s+queue_1\s+in 3 minutes/
117117
end
118+
ensure
119+
I18n.available_locales = previous_locales
120+
end
121+
122+
test "get jobs and job details when English is not included among the locales" do
123+
previous_locales, I18n.available_locales = I18n.available_locales, %i[ es nl ]
124+
125+
DummyJob.set(wait: 3.minutes).perform_later
126+
127+
get mission_control_jobs.application_jobs_url(@application, :scheduled)
128+
assert_response :ok
129+
130+
assert_select "tr.job", /DummyJob\s+Enqueued less than 5 seconds ago\s+queue_1\s+in 3 minutes/
131+
ensure
132+
I18n.available_locales = previous_locales
118133
end
119134
end

0 commit comments

Comments
 (0)