Skip to content

Fix crash when available locales are enforced but fallback locale unavailable #1796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Fixes

* Your contribution here.
* [#1796](https://github.com/ruby-grape/grape/pull/1796): Fix crash when available locales are enforced but fallback locale unavailable - [@Morred](https://github.com/Morred).
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
* [#1787](https://github.com/ruby-grape/grape/pull/1787): Add documented but not implemented ability to `.insert` a middleware in the stack - [@michaellennox](https://github.com/michaellennox).
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,8 @@ end
Grape supports I18n for parameter-related error messages, but will fallback to English if
translations for the default locale have not been provided. See [en.yml](lib/grape/locale/en.yml) for message keys.

In case your app enforces available locales only and :en is not included in your available locales, Grape cannot fall back to English and will return the translation key for the error message. To avoid this behaviour, either provide a translation for your default locale or add :en to your available locales.

### Custom Validation messages

Grape supports custom validation messages for parameter-related and coerce-related error messages.
Expand Down
10 changes: 9 additions & 1 deletion lib/grape/exceptions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ def translate(key, **options)
options = options.dup
options[:default] &&= options[:default].to_s
message = ::I18n.translate(key, **options)
message.present? ? message : ::I18n.translate(key, locale: FALLBACK_LOCALE, **options)
message.present? ? message : fallback_message(key, **options)
end

def fallback_message(key, **options)
if ::I18n.enforce_available_locales && !::I18n.available_locales.include?(FALLBACK_LOCALE)
key
else
::I18n.translate(key, locale: FALLBACK_LOCALE, **options)
end
end
end
end
Expand Down
61 changes: 61 additions & 0 deletions spec/grape/exceptions/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'spec_helper'

describe Grape::Exceptions::Base do
describe '#compose_message' do
subject { described_class.new.send(:compose_message, key, attributes) }

let(:key) { :invalid_formatter }
let(:attributes) { { klass: String, to_format: 'xml' } }

after do
I18n.available_locales = %i[en]
I18n.default_locale = :en
end

context 'when I18n enforces available locales' do
before { I18n.enforce_available_locales = true }

context 'when the fallback locale is available' do
before do
I18n.available_locales = %i[de en]
I18n.default_locale = :de
end

it 'returns the translated message' do
expect(subject).to eq('cannot convert String to xml')
end
end

context 'when the fallback locale is not available' do
before do
I18n.available_locales = %i[de jp]
I18n.default_locale = :de
end

it 'returns the translation string' do
expect(subject).to eq("grape.errors.messages.#{key}")
end
end
end

context 'when I18n does not enforce available locales' do
before { I18n.enforce_available_locales = false }

context 'when the fallback locale is available' do
before { I18n.available_locales = %i[de en] }

it 'returns the translated message' do
expect(subject).to eq('cannot convert String to xml')
end
end

context 'when the fallback locale is not available' do
before { I18n.available_locales = %i[de jp] }

it 'returns the translated message' do
expect(subject).to eq('cannot convert String to xml')
end
end
end
end
end
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
require file
end

I18n.enforce_available_locales = false
# The default value for this setting is true in a standard Rails app,
# so it should be set to true here as well to reflect that.
I18n.enforce_available_locales = true

RSpec.configure do |config|
config.include Rack::Test::Methods
Expand Down