Module: FriendlyId::Globalize

Defined in:
lib/friendly_id/globalize.rb,
lib/friendly_id/globalize/version.rb

Overview

== Translating Slugs Using Globalize

The Globalize module lets you use Globalize[https://github.com/globalize/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 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

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") }

=== Creating Records

When new records are created, the slug is generated for the current locale only.

=== Translating Slugs

To translate an existing record's friendly_id, use 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") }

Constant Summary collapse

VERSION =
'1.0.0.alpha4'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.advise_against_untranslated_model(model) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/friendly_id/globalize.rb', line 75

def advise_against_untranslated_model(model)
  field = model.friendly_id_config.query_field
  unless model.respond_to?('translated_attribute_names') ||
         model.translated_attribute_names.exclude?(field.to_sym)
    raise "[FriendlyId] You need to translate the '#{field}' field with " \
      "Globalize (add 'translates :#{field}' in your model '#{model.name}')"
  end
end

.included(model_class) ⇒ Object



71
72
73
# File 'lib/friendly_id/globalize.rb', line 71

def included(model_class)
  advise_against_untranslated_model(model_class)
end

.setup(model_class) ⇒ Object



67
68
69
# File 'lib/friendly_id/globalize.rb', line 67

def setup(model_class)
  model_class.friendly_id_config.use :slugged
end

Instance Method Details

#set_friendly_id(text, locale = nil) ⇒ Object



86
87
88
89
90
# File 'lib/friendly_id/globalize.rb', line 86

def set_friendly_id(text, locale = nil)
  ::Globalize.with_locale(locale || ::Globalize.locale) do
    super_set_slug normalize_friendly_id(text)
  end
end

#set_slug(normalized_slug = nil) ⇒ Object



96
97
98
99
100
# File 'lib/friendly_id/globalize.rb', line 96

def set_slug(normalized_slug = nil)
  (self.translations.map(&:locale).presence || [::Globalize.locale]).each do |locale|
    ::Globalize.with_locale(locale) { super_set_slug(normalized_slug) }
  end
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/friendly_id/globalize.rb', line 92

def should_generate_new_friendly_id?
  translation_for(::Globalize.locale).send(friendly_id_config.slug_column).nil?
end

#super_set_slug(normalized_slug = nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/friendly_id/globalize.rb', line 102

def super_set_slug(normalized_slug = nil)
  if should_generate_new_friendly_id?
    candidates = FriendlyId::Candidates.new(self, normalized_slug || send(friendly_id_config.base))
    slug = slug_generator.generate(candidates) || resolve_friendly_id_conflict(candidates)
    translation.send("#{friendly_id_config.slug_column}=", slug)
  end
end