Class: RoutingFilter::Locale

Inherits:
Filter
  • Object
show all
Defined in:
lib/routing_filter/filters/locale.rb

Constant Summary collapse

@@include_default_locale =
true

Instance Attribute Summary

Attributes inherited from Filter

#next, #options, #previous

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Filter

#initialize, #run, #run_reverse

Constructor Details

This class inherits a constructor from RoutingFilter::Filter

Class Method Details

.include_default_locale?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/routing_filter/filters/locale.rb', line 31

def include_default_locale?
  @@include_default_locale
end

.localesObject



35
36
37
# File 'lib/routing_filter/filters/locale.rb', line 35

def locales
  @@locales ||= I18n.available_locales.map(&:to_sym)
end

.locales=(locales) ⇒ Object



39
40
41
# File 'lib/routing_filter/filters/locale.rb', line 39

def locales=(locales)
  @@locales = locales.map(&:to_sym)
end

.locales_patternObject



43
44
45
# File 'lib/routing_filter/filters/locale.rb', line 43

def locales_pattern
  @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
end

Instance Method Details

#around_generate(*args, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/routing_filter/filters/locale.rb', line 55

def around_generate(*args, &block)
  params = args.extract_options!                              # this is because we might get a call like forum_topics_path(forum, topic, :locale => :en)

  locale = params.delete(:locale)                             # extract the passed :locale option
  locale = I18n.locale if locale.nil?                         # default to I18n.locale when locale is nil (could also be false)
  locale = nil unless valid_locale?(locale)                   # reset to no locale when locale is not valid

  args << params

  yield.tap do |result|
    prepend_segment!(result, locale) if prepend_locale?(locale)
  end
end

#around_recognize(path, env, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/routing_filter/filters/locale.rb', line 48

def around_recognize(path, env, &block)
  locale = extract_segment!(self.class.locales_pattern, path) # remove the locale from the beginning of the path
  yield.tap do |params|                                       # invoke the given block (calls more filters and finally routing)
    params[:locale] = locale if locale                        # set recognized locale to the resulting params hash
  end
end