Preloading Globalize view translations in production

When you've properly extracted and translated view translations from your source code, here's a snippet to preload them all before the first request served in Production.

Dispatcher.to_prepare :globalize_view_translations do
  if RAILS_ENV == 'production' 
    translator = Globalize::DbViewTranslator.instance
      #Or whichever other means used to keep track of app specific supported languages 
      SupportedLanguage.find(:all).each do |lang|
        ViewTranslation.find(:all, :conditions => ['globalize_translations.language_id = ?', lang.language_id]).each do |tr|
          translator.send(:cache_add, tr.tr_key, tr.language, tr.pluralization_index, tr.text)
        end  
    end  
  end  
end

Dispatcher.to_prepare

An addition to 1.2 and Edge for quite some time.This callback is referenced

  • before the first request in Production
  • before each request in Development

Most notably initializes Observer instances


About this entry