Skip to content

Commit

Permalink
Fix setting of locale via routes mount ... defaults: { locale: :en } (
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon authored Apr 12, 2023
1 parent 3794dbf commit ec385cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/controllers/good_job/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def switch_locale(&action)
end

def current_locale
if params[: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
Expand Down
21 changes: 21 additions & 0 deletions spec/app/controllers/good_job/application_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe GoodJob::ApplicationController, type: :controller do
render_views # seems required for Rails HEAD

controller do
def index
render plain: "OK"
end
end

describe "#current_locale" do
it "defers to GET queries of params (to allow setting `mount...defaults: { locale: X })`" do
allow(controller.params).to receive(:[]).with(:locale).and_return(:en)
allow(controller.request.GET).to receive(:[]).with('locale').and_return(:de)

expect(controller.send(:current_locale)).to eq(:de)
end
end
end

0 comments on commit ec385cf

Please sign in to comment.