I18n returning a mixture of locales

I’m having an I18n issue where responses are rendered with a mixture of locales. This only happens in production.

It looks a lot like this bug report (fixed and closed in 2011): https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6353

Also related:

I18n config is pretty much straight out of the rails guides.

I scanned the config directory for any mention of I18n or i18n to see if something was maybe being executed early during startup that could be messing things up, but no luck.

Possibly related production.rb env:

  config.cache_classes = true
  config.eager_load = true

But these seem like they would be common production configs mixed with I18n.

The relevant bits of application.rb:

    config.i18n.available_locales = [:en, :es]
    config.i18n.default_locale = :en
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

around_filter in ApplicationController.rb:

  def switch_locale(&action)
    # use http_accept_language gem to find a compatible local from HTTP heders (if any)
    http_locale = http_accept_language.compatible_language_from(I18n.available_locales)
    # but prefer explicit locale param (if any)
    locale = params[:locale] || http_locale || I18n.default_locale
    I18n.with_locale(locale, &action)
  end

Client is not passing any header, URL param, etc. So this should default to :en locale, but I’m reliably getting a mixture of Spanish and English.

I suppose I should mention: this is happening with validation error messages in models.

For posterity, the issue was with explicitly calling I18n.t in the model instead of just passing a symbol. Class caching in production sets that translation in stone the first time it’s called.

Bad:

errors.add(:my_attribute, I18n.t('activerecord.errors.models.my_model.attributes.my_attribute.asploded'))

Good:

errors.add(:my_attribute, :asploded)