Skip to content

Commit 10e5b39

Browse files
WIP
1 parent 6d1ea39 commit 10e5b39

File tree

13 files changed

+171
-9
lines changed

13 files changed

+171
-9
lines changed

lib/generators/infinum_json_api_setup/templates/config/locales/json_api.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ en:
44
bad_request:
55
title: Bad Request
66
detail: Bad request
7+
invalid_locale: Invalid locale
78
not_found:
89
title: Not found
910
detail: Resource not found

lib/infinum_json_api_setup.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require 'infinum_json_api_setup/json_api/error_serializer'
1515

1616
require 'infinum_json_api_setup/json_api/content_negotiation'
17+
require 'infinum_json_api_setup/json_api/locale_negotiation'
1718
require 'infinum_json_api_setup/json_api/request_parsing'
1819
require 'infinum_json_api_setup/json_api/serializer_options'
1920
require 'infinum_json_api_setup/json_api/responder'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module InfinumJsonApiSetup
2+
module JsonApi
3+
module LocaleNegotiation
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
around_action :setup_locale
8+
end
9+
10+
def rescue_with_handler(*)
11+
# Must render error in valid locale
12+
valid_locale = locale_valid?(locale) ? locale : I18n.default_locale
13+
I18n.with_locale(valid_locale) { super }
14+
end
15+
16+
private
17+
18+
def setup_locale(&action)
19+
I18n.with_locale(locale, &action)
20+
end
21+
22+
def locale
23+
(locale_from_request.presence || I18n.default_locale).to_s
24+
end
25+
26+
def locale_valid?(locale)
27+
I18n.available_locales.map(&:to_s).include?(locale)
28+
end
29+
30+
def locale_from_request
31+
accept_language = request.env['HTTP_ACCEPT_LANGUAGE']
32+
return unless accept_language
33+
34+
accept_language.scan(/^[a-z]{2}/).first
35+
end
36+
end
37+
end
38+
end

spec/dummy/app/controllers/api/v1/base_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module V1
33
class BaseController < ApplicationController
44
include InfinumJsonApiSetup::JsonApi::ErrorHandling
55
include InfinumJsonApiSetup::JsonApi::ContentNegotiation
6+
include InfinumJsonApiSetup::JsonApi::LocaleNegotiation
67

78
self.responder = InfinumJsonApiSetup::JsonApi::Responder
89
respond_to :json_api
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Api
2+
module V1
3+
class HelloController < BaseController
4+
# GET /api/v1/hello
5+
def index
6+
message = I18n.t('hello')
7+
render json: { data: { type: 'hello', attributes: { message: message } } }
8+
end
9+
end
10+
end
11+
end

spec/dummy/app/controllers/api/v1/locations_controller.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ module V1
33
class LocationsController < BaseController
44
include Pundit::Authorization
55

6-
around_action :with_locale
7-
86
# GET /api/v1/locations
97
def index
108
q = Api::V1::Locations::Query.new(Location.all, params.to_unsafe_hash)
@@ -44,10 +42,6 @@ def destroy
4442

4543
private
4644

47-
def with_locale(&block)
48-
I18n.with_locale(params.fetch(:locale, :en), &block)
49-
end
50-
5145
def permitted_params
5246
params.require(:location).permit(:latitude, :longitude)
5347
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
I18n.available_locales = [:en, :hr]
2+
3+
I18n.default_locale = :en

spec/dummy/config/locales/hr.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hr:
2+
hello: "Pozdrav svijetu"

spec/dummy/config/locales/json_api.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ en:
44
bad_request:
55
title: Bad Request
66
detail: Bad request
7+
invalid_locale: Invalid locale
78
not_found:
89
title: Not found
910
detail: Resource not found
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
hr:
2+
json_api:
3+
errors:
4+
bad_request:
5+
title: Pogrešan zahtjev
6+
detail: Pogrešan zahtjev
7+
invalid_locale: Pogrešan jezik
8+
not_found:
9+
title: Nije pronađen
10+
detail: Resurs nije pronađen
11+
forbidden:
12+
title: Zabranjeno
13+
detail: Nije Vam dozvoljeno izvršiti ovu radnju
14+
unauthorized:
15+
title: Neovlašćeno
16+
detail: Morate biti prijavljeni da biste izvršili ovu radnju
17+
unprocessable_entity:
18+
title: Neobradivo
19+
detail: Ne može se obraditi zahtjev
20+
internal_server_error:
21+
title: Interna pogreška
22+
detail: Dogodila se interna pogreška

0 commit comments

Comments
 (0)