1

I have a few tables that contain translations. In order to simplify access to all translations and have them cached, I managed, after the initialization of Rails to read the table and inject their content in the translation store.

  #store translations in the I18n store
  I18n.available_locales.each do |lang|
    storage_hash = {:text => {}, :permalink => {}}
    Translation.where(:translatable_type => self.name, :language=> lang).each do |c| 
        storage_hash[:text][c.translatable_id.to_s] = c.text
        storage_hash[:permalink][c.translatable_id.to_s] = c.permalink
    end
    I18n.backend.store_translations(lang, self.name.downcase => storage_hash)
  end

It works great, but in dev, sometimes the translation store is reset and I loose the translations I previously added.

Does it happen in production? Is there a callback I could use to repopulate my translation? Or is there a better way to do what I want?

tshepang
  • 12,111
  • 21
  • 91
  • 136
jlfenaux
  • 3,263
  • 1
  • 26
  • 33

2 Answers2

1

there is a better way of doing this. it's called I18n backends and you can read about them in the guides: http://guides.rubyonrails.org/i18n.html#using-different-backends

it's also possible to chain backends, so that you can establish fallbacks for your translations. I18n is a pretty mighty library, have a closer look at the docs https://github.com/svenfuchs/rails-i18n

phoet
  • 18,688
  • 4
  • 46
  • 74
  • If I use for example an activerecord backend, will the translations be cached in memory, as they are with the yaml files, or will each call the the backend end up with a request to the database. This is what I'm trying to avoid. – jlfenaux Feb 16 '12 at 15:11
  • i think that they will get cached in production. everything else would really really suck and i guess nobody would use it. but it should be easy to testify this. – phoet Feb 16 '12 at 16:10
0

Add your translation in a seed.rb script or only in a config/locale file.

shingara
  • 46,608
  • 11
  • 99
  • 105