如何在Rails中使用全球化和黑子4 [英] How to use globalize and sunspot in rails 4

查看:142
本文介绍了如何在Rails中使用全球化和黑子4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何用黑子Solr索引阿拉伯语个人资料翻译.我可以使用全球化和黑子还是应该使用其他方法?

How do i index arabic profile translations with sunspot solr. Can i use globalize and sunspot or should use some other approach?

models/profile.rb

translates :name, :description
validates :name
validates :description

searchable do
  text :name
  text :description
end

推荐答案

您可以在搜索块中为每个语言环境定义单独的字段:

You can define separate fields for each of your locales in your search block:

I18n.available_locales.each do |locale|
  # Separate name field for each locale
  text "name_#{locale}".to_sym do
    # read_Attribute is defined by Globalize.
    read_attribute(:name, locale: locale)
  end
  text "description_#{locale}".to_sym do
    read_attribute(:description, locale: locale)
  end
end

搜索时,必须指定要使用的语言环境:

When you are doing a search, you have to specify which locale you want to use:

# Search by actual locale
search = Sunspot.search(Profile) do
  fulltext "search term" do
    fields "name_#{I18n.locale}".to_sym,
           "description_#{I18n.locale}".to_sym,
  end
end

这篇关于如何在Rails中使用全球化和黑子4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆