Skip to content

Set :en as fallback locale for grape errors when translations are missin... #444

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 2 commits into from
Jul 22, 2013
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 @@ -6,6 +6,7 @@ Next Release
* Grape is no longer tested against Ruby 1.8.7.
* [#442](https://github.com/intridea/grape/issues/442): Enable incrementally building on top of a previous API version - [@dblock](https://github.com/dblock).
* [#442](https://github.com/intridea/grape/issues/442): API `version` can now take an array of multiple versions - [@dblock](https://github.com/dblock).
* [#444](https://github.com/intridea/grape/issues/444): Added :en as fallback locale for I18n - [@aew](https://github.com/aew).

* Your contribution here.

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ rescue_from Grape::Exceptions::Validation do |e|
end
```

### I18n

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.


## Headers

Request headers are available through the `headers` helper or from `env` in their original form.
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/exceptions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Base < StandardError

BASE_MESSAGES_KEY = 'grape.errors.messages'
BASE_ATTRIBUTES_KEY = 'grape.errors.attributes'
FALLBACK_LOCALE = :en

attr_reader :status, :message, :headers

Expand Down Expand Up @@ -58,7 +59,8 @@ def translate_message(key, options = {})
end

def translate(key, options = {})
::I18n.translate(key, options)
message = ::I18n.translate(key, options)
message.present? ? message : ::I18n.translate(key, options.merge({:locale => FALLBACK_LOCALE}))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should write this in a more concise way:

::I18n.translate(key, options) || ::I18n.translate(key, options.merge({ ... }))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the locale is missing, an empty string is returned which does not evaluate to false, so the test doesn't pass with this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right.


end
Expand Down
38 changes: 28 additions & 10 deletions spec/grape/validations/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@
def app; subject end

describe 'coerce' do
it "i18n error on malformed input" do
I18n.load_path << File.expand_path('../zh-CN.yml',__FILE__)
I18n.reload!
I18n.locale = :'zh-CN'
subject.params { requires :age, :type => Integer }
subject.get '/single' do 'int works'; end

context "i18n" do

after :each do
I18n.locale = :en
end

it "i18n error on malformed input" do
I18n.load_path << File.expand_path('../zh-CN.yml',__FILE__)
I18n.reload!
I18n.locale = :'zh-CN'
subject.params { requires :age, :type => Integer }
subject.get '/single' do 'int works'; end

get '/single', :age => '43a'
last_response.status.should == 400
last_response.body.should == '年龄格式不正确'
end

it 'gives an english fallback error when default locale message is blank' do
I18n.locale = :'pt-BR'
subject.params { requires :age, :type => Integer }
subject.get '/single' do 'int works'; end

get '/single', :age => '43a'
last_response.status.should == 400
last_response.body.should == '年龄格式不正确'
I18n.locale = :en
get '/single', :age => '43a'
last_response.status.should == 400
last_response.body.should == 'invalid parameter: age'
end

end

it 'error on malformed input' do
subject.params { requires :int, :type => Integer }
subject.get '/single' do 'int works'; end
Expand Down