Skip to content

Commit 3981dc6

Browse files
committed
fix i18n
1 parent ea20d31 commit 3981dc6

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

Gemfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ source "https://rubygems.org"
66
gemspec
77

88
group :development, :test do
9-
gem "actionpack"
10-
gem "bundler"
9+
gem "actionpack", require: false
1110
gem "debug"
1211
gem "rake"
1312
gem "rspec"

config/locales/en.restful_error.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
ja:
1+
en:
22
restful_error:
33
active_record/record_not_found: Requested resource is not found
4+
action_controller/routing_error: Requested resource is not found
45
can_can/unauthorized: Unauthorized
56
bad_request: Bad request #400
67
unauthorized: Unauthorized #401
78
forbidden: Forbidden #403
8-
not_found: Not found #404
9+
not_found: Page not found #404
910
method_not_allowed: Requested HTTP Method is not allowd #405
1011
gone: Gone #410
1112
unprocessable_content: Content is readable but unprocessable #422

lib/restful_error.rb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@
55
require "restful_error/status"
66
require "restful_error/version"
77

8-
I18n.load_path += Dir["#{File.expand_path("../config/locales")}/*.yml"] if defined? I18n
9-
108
module RestfulError
9+
module Helper
10+
def restful
11+
@restful ||= begin
12+
raise NotImplementedError, "http_status must be implemented by including class" unless respond_to?(:http_status)
13+
RestfulError.build_status_from_symbol_or_code(http_status)
14+
end
15+
end
16+
def response_message
17+
return @response_message unless @response_message.nil?
18+
@response_message = RestfulError.localized_phrase(self.class.name, restful)
19+
end
20+
end
21+
1122
class BaseError < StandardError
12-
attr_reader :response_message
23+
include RestfulError::Helper
1324
def initialize(message = nil)
1425
@response_message = message
1526
super
1627
end
1728
end
1829

19-
module Helper
20-
def restful
21-
raise NotImplementedError, "http_status must be implemented by including class" unless respond_to?(:http_status)
22-
23-
@restful ||= RestfulError.build_status_from_symbol_or_code(http_status)
24-
end
25-
end
26-
2730
@cache = {}
2831
class << self
2932
def [](code_like)
@@ -38,10 +41,21 @@ def const_missing(const_name)
3841
@cache[status.code] ||= build_error_class_for(status)
3942
end
4043

44+
def init_i18n
45+
return if @init_i18n
46+
I18n.load_path += Dir["#{File.expand_path("./config/locales")}/*.yml"]
47+
@init_i18n = true
48+
end
49+
def localized_phrase(class_name, status)
50+
return false unless defined?(I18n)
51+
init_i18n
52+
class_key = RestfulError::Inflector.underscore(class_name)
53+
I18n.t class_key, default: [status.symbol, false], scope: :restful_error
54+
end
55+
4156
private
4257

4358
def build_error_class_for(status)
44-
message = defined?(I18n) ? I18n.t(status.symbol, default: status.reason_phrase, scope: :restful_error) : status.reason_phrase
4559
klass = Class.new(BaseError) do
4660
define_method(:http_status) { status.code }
4761
define_method(:restful) { status }

lib/restful_error/railtie.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ def show
1414
status = RestfulError.build_status_from_symbol_or_code(code)
1515
@status_code = status.code
1616
@reason_phrase = status.reason_phrase
17-
@response_message = @exception.try(:response_message)
18-
unless @response_message
19-
class_name = @exception.class.name
20-
class_key = RestfulError::Inflector.underscore(class_name)
21-
@response_message = I18n.t class_key, default: [ status.symbol, '' ], scope: :restful_error
22-
end
17+
@response_message = @exception.try(:response_message) || RestfulError.localized_phrase(@exception.class.name, status) || nil
2318

2419
self.status = status.code
2520
render "restful_error/show", formats: request.format.symbol

spec/with_rails/exceptions_app_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# frozen_string_literal: true
22

3-
require "spec_helper"
43
require "action_controller"
5-
require "restful_error/railtie"
4+
require "i18n"
5+
require "spec_helper"
6+
require "restful_error/railtie" # require again for run after restful_error_spec
67

78
RSpec.describe RestfulError::ExceptionsController do
89
include Rack::Test::Methods
@@ -22,7 +23,7 @@ def app = RestfulError.exceptions_app
2223
context 'default message' do
2324
let(:exception) { described_class.new }
2425
it do
25-
expect(json).to eq({status_code: 404, reason_phrase: "Not Found", response_message: ""}.stringify_keys)
26+
expect(json).to eq({status_code: 404, reason_phrase: "Not Found", response_message: 'Page not found'}.stringify_keys)
2627
expect(last_response.status).to eq status_code
2728
end
2829
end
@@ -39,7 +40,7 @@ def app = RestfulError.exceptions_app
3940
let(:status_code) { 404 }
4041
include_context "json" do
4142
it do
42-
expect(json).to eq({status_code:, reason_phrase: "Not Found", response_message: ""}.stringify_keys)
43+
expect(json).to eq({status_code:, reason_phrase: "Not Found", response_message: 'Requested resource is not found'}.stringify_keys)
4344
expect(last_response.status).to eq status_code
4445
end
4546
end

0 commit comments

Comments
 (0)