rack-user-locale 0.0.1 → 0.0.2

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 CHANGED
@@ -1,6 +1,8 @@
1
1
  rack-user-locale
2
2
  ----------------
3
3
 
4
+ [![Build Status](https://secure.travis-ci.org/sleepingstu/rack-user-locale.png)](http://travis-ci.org/sleepingstu/rack-user-locale)
5
+
4
6
  A Rack module for getting and setting a user's locale via a cookie or browser default language.
5
7
 
6
8
  Credit to these gems for pointing me in the direction I wanted to go in...
@@ -36,10 +38,19 @@ and thats it!!
36
38
  Your application will now attempt to set the I18n.locale value based on the following:
37
39
 
38
40
  * Whether a user has a "user-locale" cookie set
39
- * The "HTTP_ACCEPT_LANGUAGE" value
41
+ * The "HTTP_ACCEPT_LANGUAGE" value. If there are multiple values it will attempt to set the one ranked highest
40
42
  * The I18n.default_locale value (basically a fallback)
41
43
 
42
- Should you wish to overwrite a users locale value then simple rewrite the "user-locale" cookie.
44
+ There is the option to pass in an accepted array of locales, like so:
45
+
46
+ ```
47
+ use Rack::UserLocale, :accepted_locales => [:en, :es, :fr, :de, :jp, (whatever codes you support)]
48
+ ```
49
+
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.
51
+
52
+
53
+ Should you wish to overwrite a users locale value at any point in your application, like for changing language prefs, then simple rewrite the "user-locale" cookie with a new value.
43
54
 
44
55
  Contributing to rack-user-locale
45
56
  =======================
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -3,8 +3,10 @@ require "i18n"
3
3
  module Rack
4
4
  class UserLocale
5
5
 
6
- def initialize(app)
7
- @app = app
6
+ def initialize(app, options = {})
7
+ @app, @options = app, {
8
+ :accepted_locales => []
9
+ }.merge(options)
8
10
  end
9
11
 
10
12
  def call(env)
@@ -28,7 +30,12 @@ module Rack
28
30
  private
29
31
 
30
32
  def set_locale
31
- I18n.locale = @env["rack.locale"] = locale.to_sym
33
+ new_locale = check_accepted? ? accepted_locale(locale.to_sym, get_default_locale) : locale.to_sym
34
+ I18n.locale = @env["rack.locale"] = new_locale
35
+ end
36
+
37
+ def accepted_locale(locale, other_locale = nil)
38
+ locale = @options[:accepted_locales].include?(locale) ? locale : other_locale
32
39
  end
33
40
 
34
41
  def locale
@@ -40,18 +47,34 @@ module Rack
40
47
  end
41
48
 
42
49
  def get_browser_locale
43
- accept_langs = @env["HTTP_ACCEPT_LANGUAGE"]
44
- return if accept_langs.nil?
50
+ accept_lang = @env["HTTP_ACCEPT_LANGUAGE"]
51
+ return if accept_lang.nil?
52
+
53
+ langs = accept_lang.split(",").map { |l|
54
+ l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
55
+ l.split(';q=')
56
+ }.sort { |a, b| b[1] <=> a[1] }
57
+
58
+ if check_accepted?
59
+ langs.each do |lang|
60
+ l = accepted_locale(split_lang(lang.first).to_sym)
61
+ return l unless l.nil?
62
+ end
63
+ end
45
64
 
46
- lang = accept_langs.split(",").map { |l|
47
- l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
48
- l.split(';q=')
49
- }.first
50
- browser_locale = lang.first.split("-").first
65
+ return split_lang(langs.first.first)
66
+ end
67
+
68
+ def split_lang(lang)
69
+ lang.split("-").first unless lang.nil?
51
70
  end
52
71
 
53
72
  def get_default_locale
54
73
  I18n.default_locale
55
74
  end
75
+
76
+ def check_accepted?
77
+ @options[:accepted_locales].count > 0
78
+ end
56
79
  end
57
80
  end
@@ -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.1"
8
+ s.version = "0.0.2"
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-02-04"
12
+ s.date = "2013-02-05"
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 = [
@@ -19,14 +19,6 @@ begin; require 'turn/autorun'; rescue LoadError; end
19
19
 
20
20
  class MiniTest::Unit::TestCase
21
21
  include Rack::Test::Methods
22
-
23
- def app
24
- app = Rack::Builder.new {
25
- use Rack::UserLocale
26
-
27
- run BasicRackApp.new
28
- }
29
- end
30
22
  end
31
23
 
32
24
  class BasicRackApp
@@ -1,67 +1,200 @@
1
- require 'helper'
1
+ require "helper"
2
2
 
3
3
  describe "RackUserLocale" do
4
- before do
5
- I18n.default_locale = :en
6
- end
4
+ describe "without accepted_locales set" do
5
+ before do
6
+ def app
7
+ app = Rack::Builder.new {
8
+ use Rack::UserLocale
7
9
 
8
- it "should have I18n.locale set to :en initially" do
9
- assert_equal :en, I18n.locale
10
- end
10
+ run BasicRackApp.new
11
+ }
12
+ end
11
13
 
12
- describe "when a locale cookie is set" do
13
- before do
14
- get 'http://example.com/', {}, 'HTTP_COOKIE' => 'user-locale=es'
14
+ I18n.default_locale = :en
15
15
  end
16
16
 
17
- it "should have I18n.locale set to :es" do
18
- assert_equal :es, I18n.locale
17
+ it "should have I18n.locale set to :en initially" do
18
+ assert_equal :en, I18n.locale
19
19
  end
20
20
 
21
- it "should not set a cookie in the response" do
22
- assert_equal nil, last_response["Set-Cookie"]
21
+ describe "when a locale cookie is set" do
22
+ before do
23
+ get "http://example.com/", {}, "HTTP_COOKIE" => "user-locale=be"
24
+ end
25
+
26
+ it "should have I18n.locale set to :be" do
27
+ assert_equal :be, I18n.locale
28
+ end
29
+
30
+ it "should not set a cookie in the response" do
31
+ assert_equal nil, last_response["Set-Cookie"]
32
+ end
23
33
  end
24
- end
25
34
 
26
- describe "when from HTTP_ACCEPT_LANGUAGE headers" do
27
- before do
28
- get 'http://example.com/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'ar-tu'
35
+ describe "when from HTTP_ACCEPT_LANGUAGE headers" do
36
+ describe "with a single locale" do
37
+ before do
38
+ get "http://example.com/", {}, "HTTP_ACCEPT_LANGUAGE" => "fr-be"
39
+ end
40
+
41
+ it "should have I18n.locale set to :fr" do
42
+ assert_equal :fr, I18n.locale
43
+ end
44
+
45
+ it "should set a cookie in the response" do
46
+ assert_equal "user-locale=fr; domain=example.com; path=/", last_response["Set-Cookie"]
47
+ end
48
+ end
49
+
50
+ describe "with an multiple locales" do
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"
54
+ end
55
+
56
+ it "should have I18n.locale set to :no" do
57
+ assert_equal :no, I18n.locale
58
+ end
59
+
60
+ it "should set a cookie in the response" do
61
+ assert_equal "user-locale=no; domain=example.com; path=/", last_response["Set-Cookie"]
62
+ end
63
+ end
29
64
  end
30
65
 
31
- it "should have I18n.locale set to :ar" do
32
- assert_equal :ar, I18n.locale
66
+ describe "when both a cooke and HTTP_ACCEPT_LANGUAGE headers are set" do
67
+ before do
68
+ get "http://example.com/", {}, "HTTP_COOKIE" => "user-locale=af", "HTTP_ACCEPT_LANGUAGE" => "ar-sa"
69
+ end
70
+
71
+ it "should have I18n.locale set to :af" do
72
+ assert_equal :af, I18n.locale
73
+ end
74
+
75
+ it "should not set a cookie in the response" do
76
+ assert_equal nil, last_response["Set-Cookie"]
77
+ end
33
78
  end
34
79
 
35
- it "should set a cookie in the response" do
36
- assert_equal "user-locale=ar; domain=example.com; path=/", last_response["Set-Cookie"]
80
+ describe "when nothing is changed" do
81
+ before do
82
+ get "http://example.com/"
83
+ end
84
+
85
+ it "should have I18n.locale set to :en" do
86
+ assert_equal :en, I18n.locale
87
+ end
88
+
89
+ it "should set a cookie in the response" do
90
+ assert_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
91
+ end
37
92
  end
38
93
  end
39
94
 
40
- describe "when both a cooke and HTTP_ACCEPT_LANGUAGE headers are set" do
95
+ describe "with accepted_locales set" do
41
96
  before do
42
- get 'http://example.com/', {}, 'HTTP_COOKIE' => 'user-locale=jp', 'HTTP_ACCEPT_LANGUAGE' => 'fr-be'
97
+ def app
98
+ app = Rack::Builder.new {
99
+ use Rack::UserLocale, :accepted_locales => [:en, :es, :fr, :de, :jp, :nl]
100
+
101
+ run BasicRackApp.new
102
+ }
103
+ end
104
+
105
+ I18n.default_locale = :en
43
106
  end
44
107
 
45
- it "should have I18n.locale set to :jp" do
46
- assert_equal :jp, I18n.locale
108
+ it "should have I18n.locale set to :en initially" do
109
+ assert_equal :en, I18n.locale
47
110
  end
48
111
 
49
- it "should not set a cookie in the response" do
50
- assert_equal nil, last_response["Set-Cookie"]
112
+ describe "when a locale cookie is set" do
113
+ before do
114
+ get "http://example.com/", {}, "HTTP_COOKIE" => "user-locale=es"
115
+ end
116
+
117
+ it "should have I18n.locale set to :es" do
118
+ assert_equal :es, I18n.locale
119
+ end
120
+
121
+ it "should not set a cookie in the response" do
122
+ assert_equal nil, last_response["Set-Cookie"]
123
+ end
51
124
  end
52
- end
53
125
 
54
- describe "when nothing is changed" do
55
- before do
56
- get 'http://example.com/'
126
+ describe "when from HTTP_ACCEPT_LANGUAGE headers" do
127
+ describe "with an accepted locale" do
128
+ before do
129
+ get "http://example.com/", {}, "HTTP_ACCEPT_LANGUAGE" => "fr-be"
130
+ end
131
+
132
+ it "should have I18n.locale set to :fr" do
133
+ assert_equal :fr, I18n.locale
134
+ end
135
+
136
+ it "should set a cookie in the response" do
137
+ assert_equal "user-locale=fr; domain=example.com; path=/", last_response["Set-Cookie"]
138
+ end
139
+ end
140
+
141
+ describe "with an multiple locales" do
142
+ before do
143
+ get "http://example.com/", {},
144
+ "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"
145
+ end
146
+
147
+ it "should have I18n.locale set to :nl" do
148
+ assert_equal :nl, I18n.locale
149
+ end
150
+
151
+ it "should set a cookie in the response" do
152
+ assert_equal "user-locale=nl; domain=example.com; path=/", last_response["Set-Cookie"]
153
+ end
154
+ end
155
+
156
+
157
+ describe "without an accepted locale" do
158
+ before do
159
+ get "http://example.com/", {}, "HTTP_ACCEPT_LANGUAGE" => "ar-sa"
160
+ end
161
+
162
+ it "should have I18n.locale set to :en" do
163
+ assert_equal :en, I18n.locale
164
+ end
165
+
166
+ it "should set a cookie in the response" do
167
+ assert_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
168
+ end
169
+ end
57
170
  end
58
171
 
59
- it "should have I18n.locale set to :en" do
60
- assert_equal :en, I18n.locale
172
+ describe "when both a cooke and HTTP_ACCEPT_LANGUAGE headers are set" do
173
+ before do
174
+ get "http://example.com/", {}, "HTTP_COOKIE" => "user-locale=jp", "HTTP_ACCEPT_LANGUAGE" => "fr-be"
175
+ end
176
+
177
+ it "should have I18n.locale set to :jp" do
178
+ assert_equal :jp, I18n.locale
179
+ end
180
+
181
+ it "should not set a cookie in the response" do
182
+ assert_equal nil, last_response["Set-Cookie"]
183
+ end
61
184
  end
62
185
 
63
- it "should set a cookie in the response" do
64
- assert_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
186
+ describe "when nothing is changed" do
187
+ before do
188
+ get "http://example.com/"
189
+ end
190
+
191
+ it "should have I18n.locale set to :en" do
192
+ assert_equal :en, I18n.locale
193
+ end
194
+
195
+ it "should set a cookie in the response" do
196
+ assert_equal "user-locale=en; domain=example.com; path=/", last_response["Set-Cookie"]
197
+ end
65
198
  end
66
199
  end
67
200
  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.1
4
+ version: 0.0.2
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-02-04 00:00:00.000000000 Z
13
+ date: 2013-02-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -192,7 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  segments:
194
194
  - 0
195
- hash: 3303822793188182499
195
+ hash: -1318168900759359089
196
196
  required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  none: false
198
198
  requirements: