From 783ba47ecf08bd3f9c7c7454ea0ba75973384adc Mon Sep 17 00:00:00 2001 From: Edward Wible Date: Sat, 20 Jul 2013 20:16:20 -0300 Subject: [PATCH 1/2] Set :en as fallback locale for grape errors when translations are missing for the default locale --- README.md | 6 +++++ lib/grape/exceptions/base.rb | 4 ++- spec/grape/validations/coerce_spec.rb | 38 ++++++++++++++++++++------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 63419aa94..f8941fccb 100644 --- a/README.md +++ b/README.md @@ -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](https://github.com/intridea/grape/blob/master/lib/grape/locale/en.yml) for message keys. + + ## Headers Request headers are available through the `headers` helper or from `env` in their original form. diff --git a/lib/grape/exceptions/base.rb b/lib/grape/exceptions/base.rb index a917fa65f..71363d327 100644 --- a/lib/grape/exceptions/base.rb +++ b/lib/grape/exceptions/base.rb @@ -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 @@ -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 end diff --git a/spec/grape/validations/coerce_spec.rb b/spec/grape/validations/coerce_spec.rb index 47eb3c563..1d056e893 100644 --- a/spec/grape/validations/coerce_spec.rb +++ b/spec/grape/validations/coerce_spec.rb @@ -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 From b8088f2dda2955e56535f93539dd94cd8acf8888 Mon Sep 17 00:00:00 2001 From: Edward Wible Date: Mon, 22 Jul 2013 03:37:42 -0300 Subject: [PATCH 2/2] update readme and changelog --- CHANGELOG.md | 1 + README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 019dd335f..271e334a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index f8941fccb..0b374e676 100644 --- a/README.md +++ b/README.md @@ -433,7 +433,7 @@ 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](https://github.com/intridea/grape/blob/master/lib/grape/locale/en.yml) for message keys. +translations for the default locale have not been provided. See [en.yml](lib/grape/locale/en.yml) for message keys. ## Headers