rack-user-locale 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -2
- data/VERSION +1 -1
- data/lib/rack-user-locale.rb +10 -1
- data/rack-user-locale.gemspec +2 -3
- data/test/test_rack-user-locale.rb +98 -22
- metadata +3 -4
- data/.rvmrc +0 -1
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
rack-user-locale
|
2
2
|
----------------
|
3
3
|
|
4
|
-
[](http://travis-ci.org/sleepingstu/rack-user-locale)
|
4
|
+
[](http://badge.fury.io/rb/rack-user-locale) [](http://travis-ci.org/sleepingstu/rack-user-locale)
|
5
5
|
|
6
6
|
A Rack module for getting and setting a user's locale via a cookie or browser default language.
|
7
7
|
|
@@ -44,7 +44,7 @@ Your application will now attempt to set the I18n.locale value based on the foll
|
|
44
44
|
There is the option to pass in an accepted array of locales, like so:
|
45
45
|
|
46
46
|
```
|
47
|
-
use Rack::UserLocale, :accepted_locales => [:en, :es, :fr, :
|
47
|
+
use Rack::UserLocale, :accepted_locales => [:de, :en, :es, :fr, :ja, :pt, :zh, (whatever codes you support)]
|
48
48
|
```
|
49
49
|
|
50
50
|
If this option is supplied the the users locale will either be set to one of the accepted locales in the array, otherwise it will be set to the I18n.default_locale value.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/rack-user-locale.rb
CHANGED
@@ -9,12 +9,21 @@ module Rack
|
|
9
9
|
}.merge(options)
|
10
10
|
end
|
11
11
|
|
12
|
+
# The "non_ruby_request" variable denotes a request which doesn't call a Ruby script.
|
13
|
+
# This might be a CSS, static HTML, image, media, or JavaScript request.
|
14
|
+
#
|
15
|
+
# This check is in place so that if you're using this gem with multiple Rack
|
16
|
+
# apps running in the same application context, you won't have static resources
|
17
|
+
# resetting the currency selection due to requests going into the main app
|
18
|
+
# instead of a sub-app.
|
19
|
+
#
|
12
20
|
def call(env)
|
13
21
|
@env = env
|
14
22
|
@request = Rack::Request.new(@env)
|
15
23
|
set_locale
|
16
24
|
|
17
|
-
|
25
|
+
non_ruby_request = (@env["SCRIPT_NAME"] == nil || @env["SCRIPT_NAME"] == "")
|
26
|
+
if @request.post? || @request.put? || @request.delete? || non_ruby_request
|
18
27
|
@app.call(env)
|
19
28
|
else
|
20
29
|
status, headers, body = @app.call(@env)
|
data/rack-user-locale.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "rack-user-locale"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Stuart Chinery", "Dave Hrycyszyn"]
|
12
|
-
s.date = "2013-
|
12
|
+
s.date = "2013-03-20"
|
13
13
|
s.description = "A Rack module for getting and setting a user's locale via a cookie or browser default language."
|
14
14
|
s.email = "stuart.chinery@headlondon.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
-
".rvmrc",
|
22
21
|
"Gemfile",
|
23
22
|
"Gemfile.lock",
|
24
23
|
"LICENSE.txt",
|
@@ -35,7 +35,7 @@ describe "RackUserLocale" do
|
|
35
35
|
describe "when from HTTP_ACCEPT_LANGUAGE headers" do
|
36
36
|
describe "with a single locale" do
|
37
37
|
before do
|
38
|
-
get "http://example.com/", {}, "HTTP_ACCEPT_LANGUAGE" => "fr-be"
|
38
|
+
get "http://example.com/", {}, { "HTTP_ACCEPT_LANGUAGE" => "fr-be", "SCRIPT_NAME" => "/" }
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should have I18n.locale set to :fr" do
|
@@ -49,8 +49,9 @@ describe "RackUserLocale" do
|
|
49
49
|
|
50
50
|
describe "with an multiple locales" do
|
51
51
|
before do
|
52
|
-
get "http://example.com/", {},
|
53
|
-
"HTTP_ACCEPT_LANGUAGE" => "de-DE;q=0.8,de;q=0.8,no-NO;q=1.0,no;q=0.7,ru-RU;q=0.7,sv-SE;q=0.4,sv;q=0.3,nl-BE;q=0.9"
|
52
|
+
get "http://example.com/", {}, {
|
53
|
+
"HTTP_ACCEPT_LANGUAGE" => "de-DE;q=0.8,de;q=0.8,no-NO;q=1.0,no;q=0.7,ru-RU;q=0.7,sv-SE;q=0.4,sv;q=0.3,nl-BE;q=0.9",
|
54
|
+
"SCRIPT_NAME" => "/" }
|
54
55
|
end
|
55
56
|
|
56
57
|
it "should have I18n.locale set to :no" do
|
@@ -65,7 +66,8 @@ describe "RackUserLocale" do
|
|
65
66
|
|
66
67
|
describe "when both a cooke and HTTP_ACCEPT_LANGUAGE headers are set" do
|
67
68
|
before do
|
68
|
-
get "http://example.com/", {}, "HTTP_COOKIE" => "user-locale=af",
|
69
|
+
get "http://example.com/", {}, { "HTTP_COOKIE" => "user-locale=af",
|
70
|
+
"HTTP_ACCEPT_LANGUAGE" => "ar-sa", "SCRIPT_NAME" => "/" }
|
69
71
|
end
|
70
72
|
|
71
73
|
it "should have I18n.locale set to :af" do
|
@@ -79,7 +81,7 @@ describe "RackUserLocale" do
|
|
79
81
|
|
80
82
|
describe "when nothing is changed" do
|
81
83
|
before do
|
82
|
-
get "http://example.com/"
|
84
|
+
get "http://example.com/", {}, { "SCRIPT_NAME" => "/" }
|
83
85
|
end
|
84
86
|
|
85
87
|
it "should have I18n.locale set to :en" do
|
@@ -90,13 +92,41 @@ describe "RackUserLocale" do
|
|
90
92
|
assert_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
|
91
93
|
end
|
92
94
|
end
|
95
|
+
|
96
|
+
describe "when SCRIPT_NAME is nil" do
|
97
|
+
before do
|
98
|
+
get "http://example.com/", {}
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should have I18n.locale set to :en" do
|
102
|
+
assert_equal :en, I18n.locale
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should set a cookie in the response" do
|
106
|
+
refute_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "when SCRIPT_NAME is blank" do
|
111
|
+
before do
|
112
|
+
get "http://example.com/", {}, { "SCRIPT_NAME" => "" }
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should have I18n.locale set to :en" do
|
116
|
+
assert_equal :en, I18n.locale
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should set a cookie in the response" do
|
120
|
+
refute_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
|
121
|
+
end
|
122
|
+
end
|
93
123
|
end
|
94
124
|
|
95
125
|
describe "with accepted_locales set" do
|
96
126
|
before do
|
97
127
|
def app
|
98
128
|
app = Rack::Builder.new {
|
99
|
-
use Rack::UserLocale, :accepted_locales => [:en, :es, :fr, :de, :
|
129
|
+
use Rack::UserLocale, :accepted_locales => [:en, :es, :fr, :de, :ja, :nl]
|
100
130
|
|
101
131
|
run BasicRackApp.new
|
102
132
|
}
|
@@ -111,7 +141,7 @@ describe "RackUserLocale" do
|
|
111
141
|
|
112
142
|
describe "when a locale cookie is set" do
|
113
143
|
before do
|
114
|
-
get "http://example.com/", {}, "HTTP_COOKIE" => "user-locale=es"
|
144
|
+
get "http://example.com/", {}, { "HTTP_COOKIE" => "user-locale=es", "SCRIPT_NAME" => "/" }
|
115
145
|
end
|
116
146
|
|
117
147
|
it "should have I18n.locale set to :es" do
|
@@ -126,7 +156,7 @@ describe "RackUserLocale" do
|
|
126
156
|
describe "when from HTTP_ACCEPT_LANGUAGE headers" do
|
127
157
|
describe "with an accepted locale" do
|
128
158
|
before do
|
129
|
-
get "http://example.com/", {}, "HTTP_ACCEPT_LANGUAGE" => "fr-be"
|
159
|
+
get "http://example.com/", {}, { "HTTP_ACCEPT_LANGUAGE" => "fr-be", "SCRIPT_NAME" => "/" }
|
130
160
|
end
|
131
161
|
|
132
162
|
it "should have I18n.locale set to :fr" do
|
@@ -139,24 +169,41 @@ describe "RackUserLocale" do
|
|
139
169
|
end
|
140
170
|
|
141
171
|
describe "with an multiple locales" do
|
142
|
-
|
143
|
-
|
144
|
-
|
172
|
+
describe "at different weights" do
|
173
|
+
before do
|
174
|
+
get "http://example.com/", {}, {
|
175
|
+
"HTTP_ACCEPT_LANGUAGE" => "de-DE;q=0.8,de;q=0.8,no-NO;q=0.7,no;q=0.7,ru-RU;q=0.7,sv-SE;q=0.4,sv;q=0.3,nl-BE;q=0.9",
|
176
|
+
"SCRIPT_NAME" => "/" }
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should have I18n.locale set to :nl" do
|
180
|
+
assert_equal :nl, I18n.locale
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should set a cookie in the response" do
|
184
|
+
assert_equal "user-locale=nl; domain=example.com; path=/", last_response["Set-Cookie"]
|
185
|
+
end
|
145
186
|
end
|
146
187
|
|
147
|
-
|
148
|
-
|
149
|
-
|
188
|
+
describe "at the same weight" do
|
189
|
+
before do
|
190
|
+
get "http://example.com/", {}, {
|
191
|
+
"HTTP_ACCEPT_LANGUAGE" => "fr,en,ja", "SCRIPT_NAME" => "/" }
|
192
|
+
end
|
150
193
|
|
151
|
-
|
152
|
-
|
194
|
+
it "should have I18n.locale set to :fr" do
|
195
|
+
assert_equal :fr, I18n.locale
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should set a cookie in the response" do
|
199
|
+
assert_equal "user-locale=fr; domain=example.com; path=/", last_response["Set-Cookie"]
|
200
|
+
end
|
153
201
|
end
|
154
202
|
end
|
155
203
|
|
156
|
-
|
157
204
|
describe "without an accepted locale" do
|
158
205
|
before do
|
159
|
-
get "http://example.com/", {}, "HTTP_ACCEPT_LANGUAGE" => "ar-sa"
|
206
|
+
get "http://example.com/", {}, { "HTTP_ACCEPT_LANGUAGE" => "ar-sa", "SCRIPT_NAME" => "/" }
|
160
207
|
end
|
161
208
|
|
162
209
|
it "should have I18n.locale set to :en" do
|
@@ -171,11 +218,12 @@ describe "RackUserLocale" do
|
|
171
218
|
|
172
219
|
describe "when both a cooke and HTTP_ACCEPT_LANGUAGE headers are set" do
|
173
220
|
before do
|
174
|
-
get "http://example.com/", {}, "HTTP_COOKIE" => "user-locale=
|
221
|
+
get "http://example.com/", {}, { "HTTP_COOKIE" => "user-locale=ja",
|
222
|
+
"HTTP_ACCEPT_LANGUAGE" => "fr-be", "SCRIPT_NAME" => "/" }
|
175
223
|
end
|
176
224
|
|
177
|
-
it "should have I18n.locale set to :
|
178
|
-
assert_equal :
|
225
|
+
it "should have I18n.locale set to :ja" do
|
226
|
+
assert_equal :ja, I18n.locale
|
179
227
|
end
|
180
228
|
|
181
229
|
it "should not set a cookie in the response" do
|
@@ -185,7 +233,7 @@ describe "RackUserLocale" do
|
|
185
233
|
|
186
234
|
describe "when nothing is changed" do
|
187
235
|
before do
|
188
|
-
get "http://example.com/"
|
236
|
+
get "http://example.com/", {}, { "SCRIPT_NAME" => "/" }
|
189
237
|
end
|
190
238
|
|
191
239
|
it "should have I18n.locale set to :en" do
|
@@ -196,5 +244,33 @@ describe "RackUserLocale" do
|
|
196
244
|
assert_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
|
197
245
|
end
|
198
246
|
end
|
247
|
+
|
248
|
+
describe "when SCRIPT_NAME is nil" do
|
249
|
+
before do
|
250
|
+
get "http://example.com/", {}
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should have I18n.locale set to :en" do
|
254
|
+
assert_equal :en, I18n.locale
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should set a cookie in the response" do
|
258
|
+
refute_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe "when SCRIPT_NAME is blank" do
|
263
|
+
before do
|
264
|
+
get "http://example.com/", {}, { "SCRIPT_NAME" => "" }
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should have I18n.locale set to :en" do
|
268
|
+
assert_equal :en, I18n.locale
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should set a cookie in the response" do
|
272
|
+
refute_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
|
273
|
+
end
|
274
|
+
end
|
199
275
|
end
|
200
276
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-user-locale
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-03-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rack
|
@@ -166,7 +166,6 @@ extra_rdoc_files:
|
|
166
166
|
- README.md
|
167
167
|
files:
|
168
168
|
- .document
|
169
|
-
- .rvmrc
|
170
169
|
- Gemfile
|
171
170
|
- Gemfile.lock
|
172
171
|
- LICENSE.txt
|
@@ -192,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
192
191
|
version: '0'
|
193
192
|
segments:
|
194
193
|
- 0
|
195
|
-
hash: -
|
194
|
+
hash: -4120119021776085754
|
196
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
196
|
none: false
|
198
197
|
requirements:
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use 1.9.3-p362@rack-user-locale
|