The {FriendlyId::Globalize Globalize} module lets you use Globalize to translate slugs. This module is most suitable for applications that need to be localized to many languages. If your application only needs to be localized to one or two languages, you may wish to consider the {FriendlyId::SimpleI18n SimpleI18n} module.
In order to use this module, your model's table and translation table must both have a slug column, and your model must set the slug field as translatable with Globalize:
class Post < ActiveRecord::Base translates :title, :slug extend FriendlyId friendly_id :title, :use => :globalize end
Finds will take the current locale into consideration:
I18n.locale = :it Post.find("guerre-stellari") I18n.locale = :en Post.find("star-wars")
Additionally, finds will fall back to the default locale:
I18n.locale = :it Post.find("star-wars")
To find a slug by an explicit locale, perform the find inside a block passed to I18n's with_locale method:
I18n.with_locale(:it) { Post.find("guerre-stellari") }
When new records are created, the slug is generated for the current locale only.
To translate an existing record's friendly_id, use {FriendlyId::Globalize::Model#set_friendly_id}. This will ensure that the slug you add is properly escaped, transliterated and sequenced:
post = Post.create :name => "Star Wars" post.set_friendly_id("Guerre stellari", :it)
If you don't pass in a locale argument, FriendlyId::Globalize will just use the current locale:
I18n.with_locale(:it) { post.set_friendly_id("Guerre stellari") }
# File lib/friendly_id/globalize.rb, line 66 def self.included(model_class) model_class.instance_eval do friendly_id_config.use :slugged relation_class.send :include, FinderMethods include Model # Check if slug field is enabled to be translated with Globalize unless respond_to?('translated_attribute_names') || translated_attribute_names.exclude?(friendly_id_config.query_field.to_sym) puts "\n[FriendlyId] You need to translate '#{friendly_id_config.query_field}' field with Globalize (add 'translates :#{friendly_id_config.query_field}' in your model '#{self.class.name}')\n\n" end end end
Generated with the Darkfish Rdoc Generator 2.