Skip to content

Commit

Permalink
Create custom subclass of I18n::Config to separate GoodJob-specific…
Browse files Browse the repository at this point in the history
… locales
bensheldon committed Jul 7, 2023
1 parent 93b2849 commit 9706979
Showing 8 changed files with 69 additions and 12 deletions.
24 changes: 15 additions & 9 deletions app/controllers/good_job/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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,25 +30,31 @@ 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
if request.GET['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
2 changes: 1 addition & 1 deletion app/controllers/good_job/cron_entries_controller.rb
Original file line number Diff line number Diff line change
@@ -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

1 change: 0 additions & 1 deletion app/helpers/good_job/application_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/views/good_job/shared/_navbar.erb
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@
</a>

<ul class="dropdown-menu" aria-labelledby="localeOptions">
<% possible_locales = I18n.enforce_available_locales ? (good_job_available_locales & I18n.available_locales) : good_job_available_locales %>
<% possible_locales = I18n.available_locales %>
<% possible_locales.reject { |locale| locale == I18n.locale }.each do |locale| %>
<li><%= link_to locale, url_for(locale: locale), class: "dropdown-item" %></li>
<% end %>
1 change: 1 addition & 0 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
require "good_job/daemon"
require "good_job/dependencies"
require "good_job/job_performer"
require "good_job/i18n_config"
require "good_job/log_subscriber"
require "good_job/multi_scheduler"
require "good_job/notifier"
24 changes: 24 additions & 0 deletions lib/good_job/i18n_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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
:en
end
end
end
23 changes: 23 additions & 0 deletions spec/requests/good_job/cron_entries_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -29,4 +29,27 @@
expect(response).to have_http_status(:found)
end
end

describe 'POST #enqueue' do
before do
allow(ExampleJob).to receive(:queue_adapter).and_return(GoodJob::Adapter.new(execution_mode: :external))
end

it 'enqueues a job' do
expect do
post good_job.enqueue_cron_entry_path(cron_key: 'example')
end.to change(GoodJob::Job, :count).by(1)
expect(response).to have_http_status(:found)
end

it 'uses the application I18n.default_locale' do
original_locale = I18n.default_locale
I18n.default_locale = :de

post good_job.enqueue_cron_entry_path(cron_key: 'example')
expect(GoodJob::Job.last.serialized_params).to include("locale" => "de")
ensure
I18n.default_locale = original_locale
end
end
end
4 changes: 4 additions & 0 deletions spec/test_app/config/application.rb
Original file line number Diff line number Diff line change
@@ -23,5 +23,9 @@ class Application < Rails::Application
config.log_level = :debug

config.action_controller.include_all_helpers = false

# Set default locale to something not yet translated for GoodJob
# config.i18n.available_locales = [:pt]
# config.i18n.default_locale = :pt
end
end

0 comments on commit 9706979

Please sign in to comment.