-
Notifications
You must be signed in to change notification settings - Fork 1
[Task #1080] Add locale negotiation #52
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
51b2d28
Add locale negotiation support
djordjelacmanovic 1aeb32e
Replace hr with de locale in localization specs
djordjelacmanovic cd173b3
Use accept_language for more robust locale parsing
djordjelacmanovic 7636878
Add constraint for accept_language dependency, post review cleanup
djordjelacmanovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
rails.7.0.gemfile | ||
rails.7.1.gemfile | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
rails.7.0.gemfile.lock | ||
rails.7.1.gemfile.lock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module InfinumJsonApiSetup | ||
module JsonApi | ||
module LocaleNegotiation | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
around_action :setup_locale | ||
class_attribute :fallback_to_default_locale_if_invalid, default: false | ||
end | ||
|
||
def rescue_with_handler(*) | ||
I18n.with_locale(locale) { super } | ||
end | ||
|
||
private | ||
|
||
def setup_locale(&) | ||
return render_invalid_locale_error if locale.blank? | ||
|
||
I18n.with_locale(locale, &) | ||
end | ||
|
||
def locale | ||
return I18n.default_locale if locale_from_request.blank? | ||
|
||
parsed_locale = AcceptLanguage.parse(locale_from_request).match(*I18n.available_locales) | ||
|
||
if parsed_locale.present? | ||
parsed_locale | ||
elsif fallback_to_default_locale_if_invalid | ||
I18n.default_locale | ||
end | ||
end | ||
|
||
def locale_from_request | ||
request.env['HTTP_ACCEPT_LANGUAGE'] | ||
end | ||
|
||
def render_invalid_locale_error | ||
message = I18n.t('json_api.errors.bad_request.invalid_locale') | ||
render_error(InfinumJsonApiSetup::Error::BadRequest.new(message:)) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Api | ||
module V1 | ||
class HelloController < BaseController | ||
# GET /api/v1/hello | ||
def index | ||
message = I18n.t('hello') | ||
render json: { data: { type: 'hello', attributes: { message: message } } } | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
I18n.available_locales = [:en, :de] | ||
|
||
I18n.default_locale = :en |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
de: | ||
hello: "Hallo Welt" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
de: | ||
json_api: | ||
errors: | ||
bad_request: | ||
title: Ungültige Anfrage | ||
detail: Ungültige Anfrage | ||
invalid_locale: Ungültige Sprache | ||
not_found: | ||
title: Nicht gefunden | ||
detail: Ressource nicht gefunden | ||
forbidden: | ||
title: Verboten | ||
detail: Sie dürfen diese Aktion nicht ausführen | ||
unauthorized: | ||
title: Nicht autorisiert | ||
detail: Sie müssen angemeldet sein, um diese Aktion auszuführen | ||
unprocessable_entity: | ||
title: Nicht verarbeitbar | ||
detail: Anfrage kann nicht verarbeitet werden | ||
internal_server_error: | ||
title: Interner Fehler | ||
detail: Ein interner Fehler ist aufgetreten |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
describe 'Locale negotiation' do | ||
describe 'with localized content' do | ||
it 'returns English hello message when Accept-Language is en' do | ||
get '/api/v1/hello', headers: default_headers.merge('Accept-Language': 'en') | ||
|
||
expect(response).to have_http_status(:ok) | ||
|
||
expect(json_response['data']['attributes']['message']).to eq('Hello world') | ||
end | ||
|
||
it 'returns German hello message when Accept-Language is de' do | ||
get '/api/v1/hello', headers: default_headers.merge('Accept-Language': 'de') | ||
|
||
expect(response).to have_http_status(:ok) | ||
|
||
expect(json_response['data']['attributes']['message']).to eq('Hallo Welt') | ||
end | ||
|
||
it 'returns default English message when no Accept-Language header provided' do | ||
get '/api/v1/hello', headers: default_headers | ||
|
||
expect(response).to have_http_status(:ok) | ||
|
||
expect(json_response['data']['attributes']['message']).to eq('Hello world') | ||
end | ||
|
||
it 'prioritizes first valid locale from complex Accept-Language header' do | ||
get '/api/v1/hello', headers: default_headers.merge('Accept-Language': 'de-DE,de;q=0.9,en;q=0.8') | ||
|
||
expect(response).to have_http_status(:ok) | ||
|
||
expect(json_response['data']['attributes']['message']).to eq('Hallo Welt') | ||
end | ||
end | ||
|
||
describe 'error handling' do | ||
it 'responds with 400 Bad Request when Accept-Language header is malformed' do | ||
get '/api/v1/hello', headers: default_headers.merge('Accept-Language': '123-invalid') | ||
|
||
expect(response).to have_http_status(:bad_request) | ||
expect(response).to include_error_detail('Invalid locale') | ||
end | ||
|
||
it 'responds with 400 Bad Request and error message for invalid locale' do | ||
get '/api/v1/hello', headers: default_headers.merge('Accept-Language': 'fr') | ||
|
||
expect(response).to have_http_status(:bad_request) | ||
expect(response).to include_error_detail('Invalid locale') | ||
end | ||
|
||
context 'when fallback to default is enabled' do | ||
around do |example| | ||
Api::V1::HelloController.fallback_to_default_locale_if_invalid = true | ||
example.run | ||
Api::V1::HelloController.fallback_to_default_locale_if_invalid = false | ||
end | ||
|
||
it 'responds with 200 OK and default locale body' do | ||
get '/api/v1/hello', headers: default_headers.merge('Accept-Language': 'fr') | ||
|
||
expect(response).to have_http_status(:ok) | ||
expect(json_response['data']['attributes']['message']).to eq('Hello world') | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Symlink was broken