Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saiqulhaq/5869ac1086240e1a72a52b18d31d5cae to your computer and use it in GitHub Desktop.
Save saiqulhaq/5869ac1086240e1a72a52b18d31d5cae to your computer and use it in GitHub Desktop.
Rails 3/4 locale issue (inconsistent value)

Your App might have this issue if you use multithreading server. It's not a bug Rails, but on your app if you don't put before_filter to all controllers. Open this Pull Request for the details rails/rails#34356, rails/rails#34043, and ruby-i18n/i18n#381.

To prevent this issue, change how the way you set I18n.locale = locale code to be in around_filter block, not before_filter.

around_filter :switch_locale

def switch_locale(&action)
  locale = params[:locale] || I18n.default_locale
  I18n.with_locale(locale, &action)
end

Note: around_filter and before_filter have became around_action and before_action in Rails 4+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment