small medium large xlarge

03 Jan 2009, 02:04
Mark Bush (8 posts)

Hey everyone,

I am getting a bit frustrated with the chapter on Internationalization. On page 200 it seems that I should be getting an error in the flash when I try to switch languages before I start making those language files in the config/locales directory . . . but this isn’t happening! I can’t understand what I am doing wrong.

When I used the depot_s code from the site(with my own database) and I omitted the es-ES file, instead of getting an error in the flash, the app threw an exception: ` No such file or directory - /Users/myUserName/code/depot_s/config/locales/es-ES.yml ` I just wanted to see if everyone is experiencing this behavior? Or if anyone has a clue on where I can begin investigating where my code is going wrong?

Thanks so much ahead of time!


EDIT: Continued investigation:

Aright, I am really missing something here. I decided to soldier on with the code, on the off chance that I had it all correct, but there was something else awry.

A little history on my code: I have been following the AWDWR book precisely, and had no problems at all! It’s been great. I was in the middle of the Testing chapter when I got the updated beta book on the 26th of december. Today I moved from that chapter and started working through the i18n chapter.

In addition to what I wrote above, I continued on to page 201 and 202 writing the code from the book as I went along. In case of session issues, after every server restart I have been doing a rake db:sessions:clear. Every time I attempt to load the index page I now get this error:

ActionView::TemplateError (You have a nil object when you didn’t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.inject) on line #21 of app/views/layouts/store.html.erb: 18: <% end %> 19: 20: <%= image_tag(“logo.png”) %> 21: <%= @page_title || I18n.t(‘layout.title’) %> 22: 23: </div> 24: <div id="columns">

The code, depot_s, from the book works correctly and functions as it should. I have attempted to replace all of the code I have written with the working code . . . substituting the three files created/altered with the working copy in the depot_s code. The error persists.

On the off chance that the code in the previous chapters had changed since the last iteration of the beta book, I went ahead and used an diff app called “Changes” to compare my code with depot_s, and I can’t seem to find any major differences that would be causing this problem.

I am wondering what my next investigative step should be . . . anyone have an idea of my next place to hunt down this bug I am having?

Once again, thanks for helping me track it down. I would rather find the problem with my own code, then go back and start again . . . and I am usually really good at tracking down bugs, but this one is REALLY eluding me!

03 Jan 2009, 02:17
Sam Ruby (633 posts)


~~~ def set_locale session[:locale] = params[:locale] if params[:locale] I18n.locale = session[:locale] || I18n.default_locale

I18n.load_path = "#{LOCALES_DIRECTORY}#{I18n.locale}.yml"
rescue Exception => err
  logger.error err
  flash[:notice] = "#{I18n.locale} translation not available"
  I18n.locale = I18n.default_locale
end   end ~~~

Explanation: there was a “late change”: to Rails 2.2.2 which I only partially adjusted for. I got the API right, but didn’t adequately retest the error paths. Instead of the error being thrown early (which I would then catch), I18N is now evaluated lazily meaning that the error would be thrown at some later point.

Sorry for not catching this myself.

03 Jan 2009, 03:08
Mark Bush (8 posts)

Hey Sam!

You’re brilliant . . . primarily because your book is my bible, but also because you fixed my second problem!

I am now able to get back to working on my code! Thank you so much! (And on another note . . . I can’t wait until I understand HALF of what that late change was!)

A few things I noticed(in case they help in some way later): The application still throws an exception if the file does not exist. I pulled out the spanish file and I still get:

No such file or directory - /Users/myUserFolder/Sites/rails/depot/config/locales/es-ES.yml

I fixed this by repeating myself (I know BOO, but I bet you will come up with a better way) and placing the ` I18n.load_path = “#{LOCALES_DIRECTORY}#{I18n.locale}.yml” ` in the rescue as well.

When I change the URI to en-US, the flash DOES show up this time. For two refreshes! (changed it to

Also, because the page simply reloads, the session never changes to the correct locale, causing the the flash to appear while navigating the rest of the app. I went ahead and put a session re-assignment in the rescue as well.

Thanks again for the help! Can’t express how much your work, and the work of your colleagues, inspires me!

03 Jan 2009, 05:41
Craig Campbell (1 post)

Thank you so much for this very fast response as I was also stuck on this section. Keep up the good work! I look forward to the final version of this book!

03 Jan 2009, 12:39
Sam Ruby (633 posts)

OK, so a cleaned up and optimised (only setting load_path if parms[:locale] is set) version follows:

  def set_locale
    return unless params[:locale]

    I18n.locale = session[:locale] = params[:locale]
    I18n.load_path = "#{LOCALES_DIRECTORY}#{I18n.locale}.yml"

  rescue Exception => err
    logger.error err[:notice] = "#{I18n.locale} translation not available"

    I18n.locale = session[:locale] = I18n.default_locale
    I18n.load_path = "#{LOCALES_DIRECTORY}#{I18n.locale}.yml"


06 Jan 2009, 13:39
Kalle Pyörälä (1 post)

Why do I get error like this in checkout form:

“translation missing: en-US, activerecord, errors, template, body”

Is it because default language file for English is en.yml and we change it or have I mistyped something?


And further on, more errors in unit test part:

test_unique_title(ProductTest) [test/unit/product_test.rb:61:in test_unique_title' /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in send’ /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run’]:

expected but was <"translation missing: en-US, activerecord, errors, models, product, attributes, title, taken">.
06 Jan 2009, 20:48
Sam Ruby (633 posts)

There are two problems, the first is that the default language is en, and the second is that replacing the load path (as my post above does) removes the rails defaults from the search path.

The next beta will use en (go with the flow…).

You must be logged in to comment