Module: Lookout::Rack::Utils::I18n

Defined in:
lib/lookout/rack/utils/i18n.rb

Instance Method Summary collapse

Instance Method Details

#accepted_languagesObject

We expect this to be called in a Rack request, but it will default to returning [] if not.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lookout/rack/utils/i18n.rb', line 30

def accepted_languages
  accepted = defined?(request.env) && request.env['HTTP_ACCEPT_LANGUAGE']
  return [] if accepted.nil?

  # parse Accept-Language
  accepted = accepted.split(',')
  accepted = accepted.map { |l| l.strip.split(";") }
  accepted = accepted.map { |l|
    # en-US -> :en
    lang = l[0].split('-').first.downcase.to_sym

    if (l.size == 2)
      # quality present
      [lang, l[1].sub(/^q=/, "").to_f ]
    else
      # no quality specified => quality == 1
      [ lang, 1.0 ]
    end
  }
  # sort by quality
  accepted.sort { |left, right| right[1] <=> left[1] }
end

#current_localeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lookout/rack/utils/i18n.rb', line 13

def current_locale
  return @locale unless @locale.nil?

  accepted_languages.each do |lang, quality|
    if configatron.locales.include?(lang)
      @locale = lang
      return @locale
    end
  end

  # Just fallback to what we have set for the default
  @locale = configatron.default_locale
  return @locale
end

#t(*args) ⇒ Object



9
10
11
# File 'lib/lookout/rack/utils/i18n.rb', line 9

def t(*args)
  ::I18n.t(*args)
end