http_accept_language 2.0.0.pre → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source :rubygems
1
+ source "https://rubygems.org"
2
2
 
3
3
  gemspec
data/README.md CHANGED
@@ -1,45 +1,49 @@
1
1
  # HttpAcceptLanguage
2
2
 
3
- A small effort in making a plugin which helps you detect the users preferred language, as sent by the HTTP header.
3
+ A gem which helps you detect the users preferred language, as sent by the "Accept-Language" HTTP header.
4
4
 
5
- Since version 2.0, this gem is Rack middleware.
6
-
7
- ## Features
8
-
9
- * Splits the http-header into languages specified by the user
10
- * Returns empty array if header is illformed.
11
- * Corrects case to xx-XX
12
- * Sorted by priority given, as much as possible.
13
- * Gives you the most important language
14
- * Gives compatible languages
5
+ The algorithm is based on [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html), with one exception:
6
+ when a user requests "en-US" and "en" is an available language, "en" is deemed compatible with "en-US".
7
+ The RFC specifies that the requested language must either exactly match the available language or must exactly match a prefix of the available language. This means that when the user requests "en" and "en-US" is available, "en-US" would be compatible, but not the other way around. This is usually not what you're looking for.
15
8
 
16
- See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
9
+ Since version 2.0, this gem is Rack middleware.
17
10
 
18
11
  ## Example
19
12
 
20
- When using in Rails:
13
+ The `http_accept_language` method is available in any controller:
21
14
 
22
- ``` ruby
15
+ ```ruby
23
16
  class SomeController < ApplicationController
24
-
25
17
  def some_action
26
-
27
- http_accept_language.user_preferred_languages # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
28
- available = %w{en en-US nl-BE}
18
+ http_accept_language.user_preferred_languages # => %w(nl-NL nl-BE nl en-US en)
19
+ available = %w(en en-US nl-BE)
29
20
  http_accept_language.preferred_language_from(available) # => 'nl-BE'
30
21
 
31
- http_accept_language.user_preferred_languages # => [ 'en-GB']
32
- available = %w{en-US}
22
+ http_accept_language.user_preferred_languages # => %w(en-GB)
23
+ available = %w(en-US)
33
24
  http_accept_language.compatible_language_from(available) # => 'en-US'
34
25
 
26
+ http_accept_language.user_preferred_languages # => %w(nl-NL nl-BE nl en-US en)
27
+ available = %w(en nl de) # This could be from I18n.available_locales
28
+ http_accept_language.preferred_language_from(available) # => 'nl'
35
29
  end
36
-
37
30
  end
38
31
  ```
39
32
 
40
- Older versions of Rails (pre 3.0) might need to include the middleware manually.
33
+ You can easily set the locale used for i18n in a before-filter:
41
34
 
42
- Usage in any Rack application, simple add the middleware:
35
+ ```ruby
36
+ class SomeController < ApplicationController
37
+ before_filter :set_locale
38
+
39
+ private
40
+ def set_locale
41
+ I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
42
+ end
43
+ end
44
+ ```
45
+
46
+ To use the middleware in any Rack application, simply add the middleware:
43
47
 
44
48
  ``` ruby
45
49
  require 'http_accept_language'
@@ -47,15 +51,20 @@ use HttpAcceptLanguage::Middleware
47
51
  run YourAwesomeApp
48
52
  ```
49
53
 
50
- Then you can access it:
54
+ Then you can access it from `env`:
51
55
 
52
56
  ``` ruby
53
57
  class YourAwesomeApp
54
58
 
55
- def self.call(env)
59
+ def initialize(app)
60
+ @app = app
61
+ end
62
+
63
+ def call(env)
56
64
  available = %w(en en-US nl-BE)
57
65
  language = env.http_accept_language.preferred_language_from(available)
58
- [ 200, {}, ["Oh, you speak #{language}!"]]
66
+
67
+ [200, {}, ["Oh, you speak #{language}!"]]
59
68
  end
60
69
 
61
70
  end
@@ -65,15 +74,15 @@ end
65
74
 
66
75
  * **user_preferred_languages**:
67
76
  Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE, sanitized and all.
68
- * **preferred_language_from**:
77
+ * **preferred_language_from(languages)**:
69
78
  Finds the locale specifically requested by the browser
70
- * **compatible_language_from**:
79
+ * **compatible_language_from(languages)**:
71
80
  Returns the first of the user_preferred_languages that is compatible with the available locales.
72
81
  Ignores region.
73
- * **sanitize_available_locales**:
82
+ * **sanitize_available_locales(languages)**:
74
83
  Returns a supplied list of available locals without any extra application info
75
84
  that may be attached to the locale for storage in the application.
76
- * **language_region_compatible_from**:
85
+ * **language_region_compatible_from(languages)**:
77
86
  Returns the first of the user preferred languages that is
78
87
  also found in available languages. Finds best fit by matching on
79
88
  primary language first and secondarily on region. If no matching region is
@@ -81,7 +90,6 @@ end
81
90
 
82
91
  ## Installation
83
92
 
84
-
85
93
  ### Without Bundler
86
94
 
87
95
  Install the gem `http_accept_language`
@@ -1,5 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/http_accept_language/version', __FILE__)
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "http_accept_language/version"
3
4
 
4
5
  Gem::Specification.new do |s|
5
6
  s.name = "http_accept_language"
@@ -9,6 +10,7 @@ Gem::Specification.new do |s|
9
10
  s.homepage = "https://github.com/iain/http_accept_language"
10
11
  s.summary = %q{Find out which locale the user preferes by reading the languages they specified in their browser}
11
12
  s.description = %q{Find out which locale the user preferes by reading the languages they specified in their browser}
13
+ s.license = "MIT"
12
14
 
13
15
  s.rubyforge_project = "http_accept_language"
14
16
 
@@ -1,4 +1,3 @@
1
- require 'http_accept_language/version'
2
1
  require 'http_accept_language/parser'
3
2
  require 'http_accept_language/middleware'
4
3
  require 'http_accept_language/railtie' if defined?(Rails::Railtie)
@@ -1,16 +1,16 @@
1
1
  module HttpAcceptLanguage
2
2
  class Middleware
3
-
4
3
  def initialize(app)
5
4
  @app = app
6
5
  end
7
6
 
8
7
  def call(env)
8
+ env["http_accept_language.parser"] = Parser.new(env["HTTP_ACCEPT_LANGUAGE"])
9
9
  def env.http_accept_language
10
- @http_accept_language ||= Parser.new(self['HTTP_ACCEPT_LANGUAGE'])
10
+ self["http_accept_language.parser"]
11
11
  end
12
+
12
13
  @app.call(env)
13
14
  end
14
-
15
15
  end
16
16
  end
@@ -17,7 +17,7 @@ module HttpAcceptLanguage
17
17
  # # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
18
18
  #
19
19
  def user_preferred_languages
20
- @user_preferred_languages ||= header.split(/\s*,\s*/).collect do |l|
20
+ @user_preferred_languages ||= header.gsub(/\s+/, '').split(/,/).collect do |l|
21
21
  l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
22
22
  l.split(';q=')
23
23
  end.sort do |x,y|
@@ -1,17 +1,17 @@
1
1
  module HttpAcceptLanguage
2
2
  class Railtie < ::Rails::Railtie
3
-
4
3
  initializer "http_accept_language.add_middleware" do |app|
5
4
  app.middleware.use Middleware
6
- ApplicationController.send :include, EasyAccess
5
+
6
+ ActiveSupport.on_load :action_controller do
7
+ include EasyAccess
8
+ end
7
9
  end
8
-
9
10
  end
10
11
 
11
12
  module EasyAccess
12
13
  def http_accept_language
13
- env.http_accept_language
14
+ @http_accept_language ||= request.env["http_accept_language.parser"] || Parser.new("")
14
15
  end
15
16
  end
16
-
17
17
  end
@@ -1,3 +1,3 @@
1
1
  module HttpAcceptLanguage
2
- VERSION = '2.0.0.pre'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -28,6 +28,17 @@ describe "Rack integration" do
28
28
  end.to_app
29
29
  end
30
30
 
31
+ it "handles reuse of the env instance" do
32
+ env = { "HTTP_ACCEPT_LANGUAGE" => "en" }
33
+ app = lambda { |env| env }
34
+ middleware = HttpAcceptLanguage::Middleware.new(app)
35
+ middleware.call(env)
36
+ env.http_accept_language.user_preferred_languages.should eq %w{en}
37
+ env["HTTP_ACCEPT_LANGUAGE"] = "de"
38
+ middleware.call(env)
39
+ env.http_accept_language.user_preferred_languages.should eq %w{de}
40
+ end
41
+
31
42
  it "decodes the HTTP_ACCEPT_LANGUAGE header" do
32
43
  request_with_header 'en-us,en-gb;q=0.8,en;q=0.6,es-419'
33
44
  r['user_preferred_languages'].should eq %w{en-US es-419 en-GB en}
data/spec/parser_spec.rb CHANGED
@@ -20,6 +20,11 @@ describe HttpAcceptLanguage::Parser do
20
20
  parser.user_preferred_languages.should eq []
21
21
  end
22
22
 
23
+ it "should properly respect whitespace" do
24
+ parser.header = 'en-us, en-gb; q=0.8,en;q = 0.6,es-419'
25
+ parser.user_preferred_languages.should eq %w{en-US es-419 en-GB en}
26
+ end
27
+
23
28
  it "should find first available language" do
24
29
  parser.preferred_language_from(%w{en en-GB}).should eq "en-GB"
25
30
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_accept_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre
5
- prerelease: 6
4
+ version: 2.0.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - iain
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-13 00:00:00.000000000 Z
12
+ date: 2013-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70157591116080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
24
+ version_requirements: *70157591116080
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rspec
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70157591115660 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: '0'
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *70157591115660
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: rack-test
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70157591115240 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ! '>='
@@ -53,15 +43,10 @@ dependencies:
53
43
  version: '0'
54
44
  type: :development
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
46
+ version_requirements: *70157591115240
62
47
  - !ruby/object:Gem::Dependency
63
48
  name: guard-rspec
64
- requirement: !ruby/object:Gem::Requirement
49
+ requirement: &70157591114820 !ruby/object:Gem::Requirement
65
50
  none: false
66
51
  requirements:
67
52
  - - ! '>='
@@ -69,15 +54,10 @@ dependencies:
69
54
  version: '0'
70
55
  type: :development
71
56
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
57
+ version_requirements: *70157591114820
78
58
  - !ruby/object:Gem::Dependency
79
59
  name: rails
80
- requirement: !ruby/object:Gem::Requirement
60
+ requirement: &70157591114320 !ruby/object:Gem::Requirement
81
61
  none: false
82
62
  requirements:
83
63
  - - ! '>='
@@ -85,15 +65,10 @@ dependencies:
85
65
  version: 3.2.6
86
66
  type: :development
87
67
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: 3.2.6
68
+ version_requirements: *70157591114320
94
69
  - !ruby/object:Gem::Dependency
95
70
  name: cucumber
96
- requirement: !ruby/object:Gem::Requirement
71
+ requirement: &70157591113900 !ruby/object:Gem::Requirement
97
72
  none: false
98
73
  requirements:
99
74
  - - ! '>='
@@ -101,15 +76,10 @@ dependencies:
101
76
  version: '0'
102
77
  type: :development
103
78
  prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
79
+ version_requirements: *70157591113900
110
80
  - !ruby/object:Gem::Dependency
111
81
  name: aruba
112
- requirement: !ruby/object:Gem::Requirement
82
+ requirement: &70157578096080 !ruby/object:Gem::Requirement
113
83
  none: false
114
84
  requirements:
115
85
  - - ! '>='
@@ -117,12 +87,7 @@ dependencies:
117
87
  version: '0'
118
88
  type: :development
119
89
  prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ! '>='
124
- - !ruby/object:Gem::Version
125
- version: '0'
90
+ version_requirements: *70157578096080
126
91
  description: Find out which locale the user preferes by reading the languages they
127
92
  specified in their browser
128
93
  email:
@@ -150,7 +115,8 @@ files:
150
115
  - spec/middleware_spec.rb
151
116
  - spec/parser_spec.rb
152
117
  homepage: https://github.com/iain/http_accept_language
153
- licenses: []
118
+ licenses:
119
+ - MIT
154
120
  post_install_message:
155
121
  rdoc_options: []
156
122
  require_paths:
@@ -164,12 +130,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
164
130
  required_rubygems_version: !ruby/object:Gem::Requirement
165
131
  none: false
166
132
  requirements:
167
- - - ! '>'
133
+ - - ! '>='
168
134
  - !ruby/object:Gem::Version
169
- version: 1.3.1
135
+ version: '0'
170
136
  requirements: []
171
137
  rubyforge_project: http_accept_language
172
- rubygems_version: 1.8.24
138
+ rubygems_version: 1.8.6
173
139
  signing_key:
174
140
  specification_version: 3
175
141
  summary: Find out which locale the user preferes by reading the languages they specified