我有一个使用globalize3 gem (https://github.com/svenfuchs/globalize3)的网站,我目前正在添加轮胎gem来进行网站搜索。
如何根据实际区域设置为表翻译建立索引?现在,被索引的模型只使用默认的语言环境。
发布于 2012-05-13 19:23:56
你必须为所有的翻译建立索引:
class Centre < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :title_en, :as => lambda { |post| I18n.locale = :en; post.title }
indexes :title_es, :as => lambda { |post| I18n.locale = :es; post.title }
indexes :title_jp, :as => lambda { |post| I18n.locale = :jp; post.title }
end
end如果你对很多属性支持很多语言,这可能会变得很麻烦,你可能不得不求助于元编程:
class Centre < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
%w[en it jp].each do |locale|
%w[title text].each do |attribute|
class_eval<<-RUBY
indexes :#{attribute}_#{locale}, :as => lambda { |post| I18n.locale = :#{locale}; post.#{attribute} }
RUBY
end
end
end
end我没有测试上面的代码,它只是给出一个想法,所以在你的项目中使用它之前,请确保你理解它并且它可以工作,否则糟糕的事情会发生在™上。
https://stackoverflow.com/questions/9951348
复制相似问题