Execute RSpec examples with a specific locale

Mittwoch, 12. Dezember 2018, 20:50 Uhr | roberto@vasquez-angel.de |

Add a helper to support :locale as metadata:

# spec/support/i18n.rb
RSpec.configure do |config|
  config.around do |example|
    if example.metadata[:locale]
      @_original_locale = I18n.locale
      I18n.locale = example.metadata[:locale]
      example.run
      I18n.locale = @_original_locale
    else
      example.run
    end
  end
end

Use it in your specs:

RSpec.describe '/en/contact', type: :feature, locale: :en do
  # I18n.locale == :en
end