Class: RoutingFilter::Language

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

Constant Summary collapse

@@include_default_language =
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_language?Boolean

Returns:

  • (Boolean)


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

def include_default_language?
  @@include_default_language
end

.languagesObject



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

def languages
  @@languages ||= I18n.available_locales.map{|l| l.to_sym}
end

.languages=(languages) ⇒ Object



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

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

.languages_patternObject

redefine original method to support locales like en-RU etc



44
45
46
# File 'lib/routing_filter/filters/language.rb', line 44

def languages_pattern
  @@languages_pattern ||= %r(^/(#{self.languages.map{|l| Regexp.escape(l.to_s).sub(/[A-Z]{2}$/,"[A-Z]{2}")}.join('|')})(?=/|$))
end

Instance Method Details

#around_generate(*args, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/routing_filter/filters/language.rb', line 57

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

  language = params[:language]                           # extract the passed :language option
  language = I18n.locale if language.nil?  # default to I18n.locale when language is nil (could also be false)
  language = I18n.lang_tag(language) if language
  language = nil unless valid_language?(language)                   # reset to no language when language is not valid
   
  params.delete(:language) unless configatron.prevent_deleting_language_param

  args << params

  yield.tap do |result|
    prepend_segment!(result, language) if prepend_language?(language)
  end
end

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



50
51
52
53
54
55
# File 'lib/routing_filter/filters/language.rb', line 50

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