summaryrefslogtreecommitdiff
path: root/lib/http_accept_language/parser.rb
blob: 0aad1a28cd995a9bdfff3daa134f7d80cdfd3314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module HttpAcceptLanguage

  class Parser

    attr_accessor :header

    def initialize(header)
      @header = header
    end

    # Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE.
    # Browsers send this HTTP header, so don't think this is holy.
    #
    # Example:
    #
    #   request.user_preferred_languages
    #   # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
    #
    def user_preferred_languages
      @user_preferred_languages ||= header.gsub(/\s+/, '').split(/,/).collect do |l|
        l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
          l.split(';q=')
      end.sort do |x,y|
        raise "Not correctly formatted" unless x.first =~ /^[a-z\-0-9]+$/i
        y.last.to_f <=> x.last.to_f
      end.collect do |l|
        l.first.downcase.gsub(/-[a-z0-9]+$/i) { |x| x.upcase }
      end
    rescue # Just rescue anything if the browser messed up badly.
      []
    end

    # Sets the user languages preference, overiding the browser
    #
    def user_preferred_languages=(languages)
      @user_preferred_languages = languages
    end

    # Finds the locale specifically requested by the browser.
    #
    # Example:
    #
    #   request.preferred_language_from I18n.available_locales
    #   # => 'nl'
    #
    def preferred_language_from(array)
      (user_preferred_languages & array.collect { |i| i.to_s }).first
    end

    # Returns the first of the user_preferred_languages that is compatible
    # with the available locales. Ignores region.
    #
    # Example:
    #
    #   request.compatible_language_from I18n.available_locales
    #
    def compatible_language_from(available_languages)
      user_preferred_languages.map do |x| #en-US
        available_languages.find do |y| # en
          y = y.to_s
          x == y || x.split('-', 2).first == y.split('-', 2).first
        end
      end.compact.first
    end

    # Returns a supplied list of available locals without any extra application info
    # that may be attached to the locale for storage in the application.
    #
    # Example:
    # [ja_JP-x1, en-US-x4, en_UK-x5, fr-FR-x3] => [ja-JP, en-US, en-UK, fr-FR]
    #
    def sanitize_available_locales(available_languages)
      available_languages.map do |avail|
        split_locale = avail.split(/[_-]/)

        split_locale.map do |e|
          e unless e.match(/x|[0-9*]/)
        end.compact.join("-")
      end
    end

    # Returns the first of the user preferred languages that is
    # also found in available languages.  Finds best fit by matching on
    # primary language first and secondarily on region.  If no matching region is
    # found, return the first language in the group matching that primary language.
    #
    # Example:
    #
    #   request.language_region_compatible(available_languages)
    #
    def language_region_compatible_from(available_languages)
      available_languages = sanitize_available_locales(available_languages)
      user_preferred_languages.map do |x| #en-US
        lang_group = available_languages.select do |y| # en
          y = y.to_s
          x.split('-', 2).first == y.split('-', 2).first
        end
        lang_group.find{|l| l == x} || lang_group.first #en-US, en-UK
      end.compact.first
    end

  end

end