Rails: store translations in database

本小妞迷上赌 提交于 2019-11-27 12:10:51

i18n has built-in support for using the database as a translation backend.

Create a table using this code in a migration:

   create_table :translations do |t|
     t.string :locale
     t.string :key
     t.text   :value
     t.text   :interpolations
     t.boolean :is_proc, :default => false
   end

Then add an initializer in config/initializers/i18n.rb with contents:

   I18n.backend = I18n::Backend::ActiveRecord.new

And last... put translations in the table. Something like:

locale key      value
en     Cool     Cool
es     Cool     Frio
en     nav.Home home
es     nav.Home casa
...

As of i18n 0.5.0 I believe they moved this code out into it's own gem... I forget what that gem is called.

I finally found what I was looking for with the help of Sven Fuchs:

http://github.com/dylanz/i18n_backend_database/tree/master

This quite awesome plugin by Dylan Stamat does exactly what the name indicates and additionally handles the caching!

Sven also mentioned, that the current branch of i18n/active_record provides an ActiveRecord backend as well:

http://github.com/svenfuchs/i18n/blob/active_record/lib/i18n/backend/active_record.rb

Some discussion about this topic is going on in the goolge i18n user group:

http://groups.google.com/group/rails-i18n/browse_thread/thread/6b7ba3c401890a7e

Issue solved, thanks to Mr I18n Sven Fuchs! ;)

We had a good experience with fast_gettext

it has a DB-backed backend that comes with a controller to do the translations over the web. The caching is built-in, though we had to code pre-loading of all of the translations on boot (it is much faster then get them one-by-one with caching).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!