Skip to content

Comments

fix: validate locale cookie before setting I18n locale#2497

Merged
mroderick merged 1 commit intocodebar:masterfrom
mroderick:fix/invalid-locale-cookie-error
Feb 21, 2026
Merged

fix: validate locale cookie before setting I18n locale#2497
mroderick merged 1 commit intocodebar:masterfrom
mroderick:fix/invalid-locale-cookie-error

Conversation

@mroderick
Copy link
Collaborator

Summary

  • Fixes I18n::InvalidLocale error when locale cookie contains invalid/empty value
  • Adds locale_value helper to validate cookie before setting I18n.locale

Analysis

The error occurred in ApplicationController#set_locale:

def set_locale
  store_locale_to_cookie(params[:locale]) if locale
  I18n.locale = cookies[:locale] || I18n.default_locale  # <-- Bug here
end

Root cause: The cookie value was used directly without validation. When a user had previously set an empty or invalid locale (e.g., ""), the line would attempt I18n.locale = "" which fails because "" is not a valid locale symbol.

Fix: Added locale_value method that validates the cookie is present AND is in the available locales before using it, otherwise falls back to default locale.

Fixes I18n::InvalidLocale error when cookie contains invalid/empty locale.

Root cause: ApplicationController#set_locale was using the cookie value
directly without validation, causing failures when users had an empty
or invalid locale stored from previous sessions.
@mroderick mroderick marked this pull request as ready for review February 20, 2026 21:49
Copy link
Contributor

@KimberleyCook KimberleyCook left a comment

Choose a reason for hiding this comment

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

🎉

@mroderick mroderick merged commit f7ea200 into codebar:master Feb 21, 2026
8 checks passed
@mroderick mroderick deleted the fix/invalid-locale-cookie-error branch February 21, 2026 13:38
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