Rails I18n 后端与 MongoDB/Mongoid
有谁知道如何实现这一点(http://railscasts.com/episodes/256- i18n-后端)与 MongoDB/Mongoid?我的问题主要是关于initializer.rb 文件。
github 上的 Mongo-I18n 的文档建议使用其“MongoI18n::Store.new”进行以下操作method:
collection = Mongo::Connection.new['my_app_related_db'].collection('i18n')
I18n.backend = I18n::Backend::KeyValue.new(MongoI18n::Store.new(collection)
但是如果你不想使用他们的插件怎么办?有类似 Mongo::Store 方法的东西吗?
Does anyone have an idea on how to implement this (http://railscasts.com/episodes/256-i18n-backends) with MongoDB/Mongoid? My question is primarily about the initializer.rb file.
The docs of Mongo-I18n on github suggests the following using its 'MongoI18n::Store.new' method:
collection = Mongo::Connection.new['my_app_related_db'].collection('i18n')
I18n.backend = I18n::Backend::KeyValue.new(MongoI18n::Store.new(collection)
But how to do this if you don't want to use their plugin? Is there something like a Mongo::Store method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是做了同样的事情,只是我在安装 Mongo-I18n 时遇到了麻烦,因为它依赖于非常旧的 MongoDB 版本。
为了解决这个问题,我从 此处复制了代码进入
lib/mongo_i18n.rb
。不过,如果您使用的是 Mongoid,那么您的初始化程序就在正确的轨道上 - 最好的前进方法是这样做:
这告诉 I18n 后端使用新的集合(称为 i18n),但与其余的在同一个数据库中您的申请。
确保从 gemfile 中删除 Mongo_I18n gem 并在再次启动服务器之前运行
bundle
。您可以直接使用以下方式访问您的商店:
但为了使其更简洁,我将此方法添加到我的 I18n 库中:
这样我就可以直接使用以下方式访问商店:
I just did this exact same thing, except that I had trouble installing Mongo-I18n, because it has a dependency on a very old version of MongoDB.
To get around this, I copied the code from here into
lib/mongo_i18n.rb
.You were on the right track with your initializer though, if you're using Mongoid - the best way forward is to do this:
Which tells the I18n backend to use a new collection (called i18n), but in the same database as the rest of your application.
Make sure you delete the Mongo_I18n gem out of your gemfile and run
bundle
before starting your server again.You can access your store directly using:
But to make it a little cleaner, I added this method to my I18n library:
So that I can access the store directly with:
我所做的与 theTRON 所说的完全一样,只是我没有将 require 'mongo_i18n' 添加到整个类 MongoI18n::Store 定义中,而是将 Mongo_i18n gem 直接添加到 mongo 初始值设定项。这没什么大不了的,因为整个 MongoI18n::Store 有 41 行长。 看这里,为什么要依赖41 行宝石 ?
I did exactly like theTRON said, except that instead of require 'mongo_i18n' I added whole class MongoI18n::Store definition from Mongo_i18n gem directly to mongo initializer. It not such a big deal, because whole MongoI18n::Store is 41 lines long. Look here, why make dependancy from 41 lines gem ?