Skip to content
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

Crash on enforce_avialable_locales without locale :en #1778

Closed
thedarkside opened this issue Aug 7, 2018 · 3 comments
Closed

Crash on enforce_avialable_locales without locale :en #1778

thedarkside opened this issue Aug 7, 2018 · 3 comments
Labels

Comments

@thedarkside
Copy link

thedarkside commented Aug 7, 2018

My rails app is configured this way:

config.i18n.available_locales = [:de]
config.i18n.default_locale = :de
config.i18n.fallbacks = true

The enforce_available_locales is set to true by default.

When hitting the grape api with an invalid request, grape sets the locale to the FALLBACK_LOCALE which is :en and crashes due to enforce_available_locales being true like so:

I18n::InvalidLocale (:en is not a valid locale):
  
i18n (1.0.1) lib/i18n.rb:311:in `enforce_available_locales!'
i18n (1.0.1) lib/i18n.rb:179:in `translate'
grape (1.1.0) lib/grape/exceptions/base.rb:77:in `translate'
grape (1.1.0) lib/grape/exceptions/base.rb:65:in `translate_message'
grape (1.1.0) lib/grape/exceptions/validation.rb:13:in `initialize'
grape (1.1.0) lib/grape/validations/validators/presence.rb:6:in `exception'
grape (1.1.0) lib/grape/validations/validators/presence.rb:6:in `raise'

In my opinion grape should check if :en is available and if not then just return the key in the error response instead of crashing miserably

@dblock dblock added the bug? label Aug 8, 2018
@dblock
Copy link
Member

dblock commented Aug 8, 2018

You're probably right, try turning it into a spec next?

@Morred
Copy link
Contributor

Morred commented Sep 29, 2018

Hope it's ok to jump in here as there hasn't been a reply from the original reporter in a while.
I made a PR here #1796, the first commit confirms that indeed an exception is raised if available locales are enforced but don't contain :en. Second commit has a fix that checks if the available locales are enforced and fallback locale is available and if not, returns the translation string instead of crashing.

@Morred
Copy link
Contributor

Morred commented Oct 9, 2018

@dblock Fixed via #1796, let's close?

@dblock dblock closed this as completed Oct 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants