Skip to content

Conversation

djordjelacmanovic
Copy link
Contributor

@djordjelacmanovic djordjelacmanovic commented Sep 11, 2025

Related issue: #1080

@djordjelacmanovic djordjelacmanovic self-assigned this Sep 11, 2025
@djordjelacmanovic djordjelacmanovic force-pushed the feature/1080-add-locale-negotiation branch from 10e5b39 to c7cc312 Compare September 11, 2025 21:53
Copy link
Member

@vr4b4c vr4b4c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great contribution 🙇 , I'm interested to hear your thoughts on my suggestions.


def rescue_with_handler(*)
# Must render error in valid locale
valid_locale = locale_valid?(locale) ? locale : I18n.default_locale
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it odd that we check locale validity here but don't do the same in #setup_locale. Can you please elaborate the decision?

Copy link
Contributor Author

@djordjelacmanovic djordjelacmanovic Sep 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem handles I18n::InvalidLocale via rescue_from in InfinumJsonApiSetup::JsonApi::ErrorHandling:

        rescue_from I18n::InvalidLocale do |e|
          render_error(InfinumJsonApiSetup::Error::BadRequest.new(message: e.to_s))
        end

if we add localised error rendering here and render an invalid locale we get an error again.

However, in the latest commit this mixin will handle the invalid locale gracefully and respond with a HTTP 400 (default) or fallback to I18n.default_locale. Thus, the validity check was also removed from error rendering.

Comment on lines 31 to 34
accept_language = request.env['HTTP_ACCEPT_LANGUAGE']
return unless accept_language

accept_language.scan(/^[a-z]{2}/).first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check out https://github.com/cyril/accept_language.rb and see if it makes sense to integrate that dependency as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've considered this gem but I took the option of not adding another dependency, instead using the simpler approach similar to what we have elsewhere.

If we're cool with another dependency for a more feature-full parser I'm all for it 👍🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept_language seems lightweight, only other dependency is from standard library practically, bigdecimal. It gets a pass from me but I'm leaving the decision to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept_language was added

@vr4b4c vr4b4c requested a review from a team September 12, 2025 10:59
@djordjelacmanovic djordjelacmanovic force-pushed the feature/1080-add-locale-negotiation branch from c7cc312 to 13ae6e7 Compare September 17, 2025 22:58
@djordjelacmanovic djordjelacmanovic force-pushed the feature/1080-add-locale-negotiation branch from 13ae6e7 to 1aeb32e Compare September 17, 2025 23:02
@@ -1 +1 @@
rails.7.0.gemfile No newline at end of file
rails.7.1.gemfile No newline at end of file
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symlink was broken

@@ -1 +1 @@
rails.7.0.gemfile.lock No newline at end of file
rails.7.1.gemfile.lock No newline at end of file
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@djordjelacmanovic djordjelacmanovic requested review from a team and removed request for a team October 7, 2025 20:54
@djordjelacmanovic djordjelacmanovic merged commit e632863 into master Oct 8, 2025
6 checks passed
@djordjelacmanovic djordjelacmanovic deleted the feature/1080-add-locale-negotiation branch October 8, 2025 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants