Class: RoutingFilter::Locale
- Inherits:
-
Base
- Object
- Base
- RoutingFilter::Locale
show all
- Defined in:
- lib/routing_filter/locale.rb
Constant Summary
collapse
- @@include_default_locale =
true
Instance Attribute Summary
Attributes inherited from Base
#chain, #options
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#initialize, #predecessor, #run, #run_reverse, #successor
Class Method Details
26
27
28
29
|
# File 'lib/routing_filter/locale.rb', line 26
def (path)
path.sub! self.locales_pattern, ''
$1
end
|
.include_default_locale? ⇒ Boolean
10
11
12
|
# File 'lib/routing_filter/locale.rb', line 10
def include_default_locale?
@@include_default_locale
end
|
.locales ⇒ Object
14
15
16
|
# File 'lib/routing_filter/locale.rb', line 14
def locales
@@locales ||= I18n.available_locales.map(&:to_sym)
end
|
.locales=(locales) ⇒ Object
18
19
20
|
# File 'lib/routing_filter/locale.rb', line 18
def locales=(locales)
@@locales = locales.map(&:to_sym)
end
|
.locales_pattern ⇒ Object
22
23
24
|
# File 'lib/routing_filter/locale.rb', line 22
def locales_pattern
@@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
end
|
.prepend_locale!(url, locale) ⇒ Object
31
32
33
|
# File 'lib/routing_filter/locale.rb', line 31
def prepend_locale!(url, locale)
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
end
|
Instance Method Details
#around_generate(*args, &block) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/routing_filter/locale.rb', line 43
def around_generate(*args, &block)
options = args.
locale = options.delete(:locale)
unless skip_prepend_locale? options
locale = I18n.locale if locale.nil? locale = nil unless valid_locale?(locale) returning yield do |result|
if locale && prepend_locale?(locale)
url = result.is_a?(Array) ? result.first : result
self.class.prepend_locale!(url, locale)
end
end
else
yield
end
end
|
#around_recognize(path, env, &block) ⇒ Object
36
37
38
39
40
41
|
# File 'lib/routing_filter/locale.rb', line 36
def around_recognize(path, env, &block)
locale = self.class.(path) returning yield do |params| params[:locale] = locale if locale end
end
|