Skip to content

Commit d814e93

Browse files
committed
Merge pull request #444 from aew/master
Set :en as fallback locale for grape errors when translations are missin...
2 parents 2be499c + b8088f2 commit d814e93

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Next Release
66
* Grape is no longer tested against Ruby 1.8.7.
77
* [#442](https://github.com/intridea/grape/issues/442): Enable incrementally building on top of a previous API version - [@dblock](https://github.com/dblock).
88
* [#442](https://github.com/intridea/grape/issues/442): API `version` can now take an array of multiple versions - [@dblock](https://github.com/dblock).
9+
* [#444](https://github.com/intridea/grape/issues/444): Added :en as fallback locale for I18n - [@aew](https://github.com/aew).
910

1011
* Your contribution here.
1112

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ rescue_from Grape::Exceptions::Validation do |e|
430430
end
431431
```
432432

433+
### I18n
434+
435+
Grape supports I18n for parameter-related error messages, but will fallback to English if
436+
translations for the default locale have not been provided. See [en.yml](lib/grape/locale/en.yml) for message keys.
437+
438+
433439
## Headers
434440

435441
Request headers are available through the `headers` helper or from `env` in their original form.

lib/grape/exceptions/base.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Base < StandardError
44

55
BASE_MESSAGES_KEY = 'grape.errors.messages'
66
BASE_ATTRIBUTES_KEY = 'grape.errors.attributes'
7+
FALLBACK_LOCALE = :en
78

89
attr_reader :status, :message, :headers
910

@@ -58,7 +59,8 @@ def translate_message(key, options = {})
5859
end
5960

6061
def translate(key, options = {})
61-
::I18n.translate(key, options)
62+
message = ::I18n.translate(key, options)
63+
message.present? ? message : ::I18n.translate(key, options.merge({:locale => FALLBACK_LOCALE}))
6264
end
6365

6466
end

spec/grape/validations/coerce_spec.rb

+28-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,37 @@
66
def app; subject end
77

88
describe 'coerce' do
9-
it "i18n error on malformed input" do
10-
I18n.load_path << File.expand_path('../zh-CN.yml',__FILE__)
11-
I18n.reload!
12-
I18n.locale = :'zh-CN'
13-
subject.params { requires :age, :type => Integer }
14-
subject.get '/single' do 'int works'; end
9+
10+
context "i18n" do
11+
12+
after :each do
13+
I18n.locale = :en
14+
end
15+
16+
it "i18n error on malformed input" do
17+
I18n.load_path << File.expand_path('../zh-CN.yml',__FILE__)
18+
I18n.reload!
19+
I18n.locale = :'zh-CN'
20+
subject.params { requires :age, :type => Integer }
21+
subject.get '/single' do 'int works'; end
22+
23+
get '/single', :age => '43a'
24+
last_response.status.should == 400
25+
last_response.body.should == '年龄格式不正确'
26+
end
27+
28+
it 'gives an english fallback error when default locale message is blank' do
29+
I18n.locale = :'pt-BR'
30+
subject.params { requires :age, :type => Integer }
31+
subject.get '/single' do 'int works'; end
1532

16-
get '/single', :age => '43a'
17-
last_response.status.should == 400
18-
last_response.body.should == '年龄格式不正确'
19-
I18n.locale = :en
33+
get '/single', :age => '43a'
34+
last_response.status.should == 400
35+
last_response.body.should == 'invalid parameter: age'
36+
end
2037

2138
end
39+
2240
it 'error on malformed input' do
2341
subject.params { requires :int, :type => Integer }
2442
subject.get '/single' do 'int works'; end

0 commit comments

Comments
 (0)