Class: RoutingFilter::RefineryLocales

Inherits:
Filter
  • Object
show all
Defined in:
lib/refinery/i18n-filter.rb

Instance Method Summary collapse

Instance Method Details

#around_generate(params, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/refinery/i18n-filter.rb', line 22

def around_generate(params, &block)
  locale = params.delete(:locale) || ::I18n.locale

  yield.tap do |result|
    result = result.is_a?(Array) ? result.first : result
    if ::Refinery::I18n.url_filter_enabled? and
       locale != ::Refinery::I18n.default_frontend_locale and
       result !~ %r{^/(#{Refinery::Core.backend_route}|wymiframe)}
      result.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
    end
  end
end

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



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/refinery/i18n-filter.rb', line 4

def around_recognize(path, env, &block)
  if ::Refinery::I18n.url_filter_enabled?
    if path =~ %r{^/(#{::Refinery::I18n.locales.keys.join('|')})(/|$)}
      path.sub! %r(^/(([a-zA-Z\-_])*)(?=/|$)) do
        ::I18n.locale = $1
        ''
      end
      path.sub!(%r{^$}) { '/' }
    else
      ::I18n.locale = ::Refinery::I18n.default_frontend_locale
    end
  end

  yield.tap do |params|
    params[:locale] = ::I18n.locale.to_s
  end
end