rack-user-locale 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/VERSION +1 -1
- data/lib/rack-user-locale.rb +22 -3
- data/rack-user-locale.gemspec +2 -2
- data/test/test_rack-user-locale.rb +8 -8
- metadata +3 -3
data/README.md
CHANGED
@@ -60,7 +60,7 @@ If you don't want this happen then you can use Rack::Static in your application
|
|
60
60
|
Something like this:
|
61
61
|
|
62
62
|
```
|
63
|
-
use Rack::Static, :urls => ["
|
63
|
+
use Rack::Static, :urls => ["/css", "/js"], :root => "public"
|
64
64
|
```
|
65
65
|
would serve anything in your public folder statically and wouldn't create a Rack::Request.
|
66
66
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/lib/rack-user-locale.rb
CHANGED
@@ -3,12 +3,16 @@ require "i18n"
|
|
3
3
|
module Rack
|
4
4
|
class UserLocale
|
5
5
|
|
6
|
+
# TODO: Write notes
|
7
|
+
#
|
6
8
|
def initialize(app, options = {})
|
7
9
|
@app, @options = app, {
|
8
10
|
:accepted_locales => []
|
9
11
|
}.merge(options)
|
10
12
|
end
|
11
13
|
|
14
|
+
# TODO: Write notes
|
15
|
+
#
|
12
16
|
def call(env)
|
13
17
|
@env = env
|
14
18
|
@request = Rack::Request.new(@env)
|
@@ -21,31 +25,40 @@ module Rack
|
|
21
25
|
response = Rack::Response.new(body, status, headers)
|
22
26
|
response.set_cookie("user-locale", {
|
23
27
|
:value => I18n.locale,
|
24
|
-
:path => "/"
|
25
|
-
:domain => @request.host}) if get_cookie_locale != I18n.locale.to_s
|
28
|
+
:path => "/"}) if get_cookie_locale != I18n.locale.to_s
|
26
29
|
response.finish
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
30
33
|
private
|
31
34
|
|
35
|
+
# TODO: Write notes
|
36
|
+
#
|
32
37
|
def set_locale
|
33
38
|
new_locale = check_accepted? ? accepted_locale(locale.to_sym, get_default_locale) : locale.to_sym
|
34
39
|
I18n.locale = @env["rack.locale"] = new_locale
|
35
40
|
end
|
36
41
|
|
42
|
+
# TODO: Write notes
|
43
|
+
#
|
37
44
|
def accepted_locale(locale, other_locale = nil)
|
38
45
|
locale = @options[:accepted_locales].include?(locale) ? locale : other_locale
|
39
46
|
end
|
40
47
|
|
48
|
+
# TODO: Write notes
|
49
|
+
#
|
41
50
|
def locale
|
42
51
|
get_cookie_locale || get_browser_locale || get_default_locale
|
43
52
|
end
|
44
53
|
|
54
|
+
# TODO: Write notes
|
55
|
+
#
|
45
56
|
def get_cookie_locale
|
46
57
|
@request.cookies["user-locale"]
|
47
58
|
end
|
48
59
|
|
60
|
+
# TODO: Write notes
|
61
|
+
#
|
49
62
|
def get_browser_locale
|
50
63
|
accept_lang = @env["HTTP_ACCEPT_LANGUAGE"]
|
51
64
|
return if accept_lang.nil?
|
@@ -65,16 +78,22 @@ module Rack
|
|
65
78
|
return split_lang(langs.first.first)
|
66
79
|
end
|
67
80
|
|
81
|
+
# TODO: Write notes
|
82
|
+
#
|
68
83
|
def split_lang(lang)
|
69
84
|
lang.split("-").first unless lang.nil?
|
70
85
|
end
|
71
86
|
|
87
|
+
# TODO: Write notes
|
88
|
+
#
|
72
89
|
def get_default_locale
|
73
90
|
I18n.default_locale
|
74
91
|
end
|
75
92
|
|
93
|
+
# TODO: Write notes
|
94
|
+
#
|
76
95
|
def check_accepted?
|
77
96
|
@options[:accepted_locales].count > 0
|
78
97
|
end
|
79
98
|
end
|
80
|
-
end
|
99
|
+
end
|
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.5"
|
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-09-13"
|
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 = [
|
@@ -43,7 +43,7 @@ describe "RackUserLocale" do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should set a cookie in the response" do
|
46
|
-
assert_equal "user-locale=fr;
|
46
|
+
assert_equal "user-locale=fr; path=/", last_response["Set-Cookie"]
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -59,7 +59,7 @@ describe "RackUserLocale" do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should set a cookie in the response" do
|
62
|
-
assert_equal "user-locale=no;
|
62
|
+
assert_equal "user-locale=no; path=/", last_response["Set-Cookie"]
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
@@ -89,7 +89,7 @@ describe "RackUserLocale" do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
it "should set a cookie in the response" do
|
92
|
-
assert_equal "user-locale=en;
|
92
|
+
assert_equal "user-locale=en; path=/", last_response["Set-Cookie"]
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
@@ -136,7 +136,7 @@ describe "RackUserLocale" do
|
|
136
136
|
end
|
137
137
|
|
138
138
|
it "should set a cookie in the response" do
|
139
|
-
assert_equal "user-locale=fr;
|
139
|
+
assert_equal "user-locale=fr; path=/", last_response["Set-Cookie"]
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
@@ -153,7 +153,7 @@ describe "RackUserLocale" do
|
|
153
153
|
end
|
154
154
|
|
155
155
|
it "should set a cookie in the response" do
|
156
|
-
assert_equal "user-locale=nl;
|
156
|
+
assert_equal "user-locale=nl; path=/", last_response["Set-Cookie"]
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
@@ -168,7 +168,7 @@ describe "RackUserLocale" do
|
|
168
168
|
end
|
169
169
|
|
170
170
|
it "should set a cookie in the response" do
|
171
|
-
assert_equal "user-locale=fr;
|
171
|
+
assert_equal "user-locale=fr; path=/", last_response["Set-Cookie"]
|
172
172
|
end
|
173
173
|
end
|
174
174
|
end
|
@@ -183,7 +183,7 @@ describe "RackUserLocale" do
|
|
183
183
|
end
|
184
184
|
|
185
185
|
it "should set a cookie in the response" do
|
186
|
-
assert_equal "user-locale=en;
|
186
|
+
assert_equal "user-locale=en; path=/", last_response["Set-Cookie"]
|
187
187
|
end
|
188
188
|
end
|
189
189
|
end
|
@@ -213,7 +213,7 @@ describe "RackUserLocale" do
|
|
213
213
|
end
|
214
214
|
|
215
215
|
it "should set a cookie in the response" do
|
216
|
-
assert_equal "user-locale=en;
|
216
|
+
assert_equal "user-locale=en; path=/", last_response["Set-Cookie"]
|
217
217
|
end
|
218
218
|
end
|
219
219
|
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.5
|
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-09-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rack
|
@@ -191,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
191
191
|
version: '0'
|
192
192
|
segments:
|
193
193
|
- 0
|
194
|
-
hash:
|
194
|
+
hash: 1351284199224301544
|
195
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
196
196
|
none: false
|
197
197
|
requirements:
|