0

I18n works as expected locally but not on heroku. It always shows english even if the browser is set to pt-BR. We set the locale in a before filter:

class ApplicationController < ActionController::Base
  before_filter :set_locale

    private

  def set_locale
    I18n.set_preferred_locale(env.http_accept_language)
  end
end

module I18n
  class << self
    def set_preferred_locale(http_accept_language)
      locale = http_accept_language.preferred_language_from(I18n.available_locales)

      if locale.present?
        I18n.locale = locale
        I18n.default_locale = locale  #added based on some stackflow answer about heroku and I18n
      end
    end
  end
end

I have confirmed through the logger that I18n.locale has the correct value (pt-BR) in views but translations are still coming in english.

I have also tried as a test just setting directly to 'pt-BR' and still get the same result:

class ApplicationController < ActionController::Base
   before_filter :set_locale

   private

   def set_locale
      I18n.locale = 'pt-BR'
   end
end
DrewB
  • 3,231
  • 24
  • 22
  • 1
    I use i18n with success on Heroku, but I don't set the locale based on user browser settings, like I get the feeling you may want to. Can you edit your question to share the code you use to set the locale? – Paul Fioravanti Feb 12 '13 at 01:35
  • Have you set `I18n.available_locales = [:en, :"pt-BR"]` in an initializer or anywhere else? – Paul Fioravanti Feb 12 '13 at 22:38

2 Answers2

3

Turns out I was setting it up fine. The issue was that there was a byte order mark in my pt-BR.yml. Apparently heroku can't load language files with them - heroku not loading language file.

I used vim to remove the marker with:

:set nobomb
:w
Community
  • 1
  • 1
DrewB
  • 3,231
  • 24
  • 22
  • Nice one. If appropriate, would you be able to edit your question or answer with the output of the error or `heroku logs` so that they can be attributed to issues with BOM? – Paul Fioravanti Feb 12 '13 at 23:14
  • Afraid there was no error message. That is what made this so difficult to take down. I figured out the file wasn't being loaded by trial and error. – DrewB Feb 13 '13 at 02:29
  • Trial and error is a tough slog :-) If all's good now, could you please mark this as the accepted answer for everyone's benefit. – Paul Fioravanti Feb 13 '13 at 03:57
  • Tried earlier. Looks like I need to wait until tomorrow before it will let me. – DrewB Feb 13 '13 at 04:08
  • Anybody know how to resolve this issue with django and compiled gettext .mo files? I tried the vim :set nobomb, but it doesn't help. No entries in the heroku logs, and my spanish translations work fine. Definitely something with the encoding of the file and heroku. – jlev Feb 16 '13 at 02:20
0

Worked for me, i have opened any file and save it with nobomb