rails_locale_detection 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +18 -4
- data/VERSION +1 -1
- data/lib/rails/locale_detection.rb +17 -2
- data/rails_locale_detection.gemspec +2 -2
- data/spec/rails_locale_detection_spec.rb +87 -17
- data/spec/support/mock.rb +14 -1
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
= rails_locale_detection
|
2
2
|
|
3
|
-
Sets the current locale of a request using a combination of params, cookies, and
|
3
|
+
Sets the current locale of a request using a combination of params, cookies, http headers, and an optional user object.
|
4
4
|
|
5
|
-
In turn, it checks the value of params[:locale], cookies[:locale] and
|
6
|
-
corresponds to the available locales, then stores the set locale in a cookie for future requests.
|
5
|
+
In turn, it checks the value of params[:locale], cookies[:locale] and HTTP_ACCEPT_LANGUAGE headers to find a locale that
|
6
|
+
corresponds to the available locales, then stores the set locale in a cookie for future requests. If a user_locale method
|
7
|
+
is provided, the return value will be used, with preference over the other locale detection methods.
|
7
8
|
|
8
9
|
== Usage
|
9
10
|
|
@@ -23,13 +24,26 @@ Call set_locale as a filter in your controllers
|
|
23
24
|
|
24
25
|
end
|
25
26
|
|
27
|
+
To support user locales, add a user_locale method
|
28
|
+
|
29
|
+
class ApplicationController < ActionController::Base
|
30
|
+
before_filter :set_locale
|
31
|
+
|
32
|
+
def user_locale
|
33
|
+
current_user.locale if current_user
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
|
26
39
|
== Configuration
|
27
40
|
|
28
|
-
|
41
|
+
The configuration options:
|
29
42
|
|
30
43
|
Rails::LocaleDetection.config do |config|
|
31
44
|
config.locale_expiry = 3.months # This sets how long the locale cookie lasts.
|
32
45
|
config.set_default_url_option = true # sets the default_url_options[:locale] to the current locale when set_locale is called
|
46
|
+
config.detection_order = [:user, :param, :cookie, :request] # set the order in which locale detection occurs. Omit values to skip those sources
|
33
47
|
end
|
34
48
|
|
35
49
|
== Contributing to rails_locale_detection
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
@@ -6,6 +6,9 @@ module Rails
|
|
6
6
|
mattr_accessor :set_default_url_option
|
7
7
|
@@set_default_url_option = true
|
8
8
|
|
9
|
+
mattr_accessor :detection_order
|
10
|
+
@@detection_order = [:user, :param, :cookie, :request]
|
11
|
+
|
9
12
|
def self.config
|
10
13
|
yield self
|
11
14
|
end
|
@@ -18,6 +21,10 @@ module Rails
|
|
18
21
|
I18n.default_locale
|
19
22
|
end
|
20
23
|
|
24
|
+
def user_locale
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
21
28
|
# returns the (symbolized) value passed if it's in the available_locales
|
22
29
|
def validate_locale(locale)
|
23
30
|
locale.to_sym if locale && available_locales.include?(locale.to_sym)
|
@@ -27,7 +34,7 @@ module Rails
|
|
27
34
|
validate_locale(params[:locale])
|
28
35
|
end
|
29
36
|
|
30
|
-
def
|
37
|
+
def locale_from_cookie
|
31
38
|
validate_locale(cookies[:locale])
|
32
39
|
end
|
33
40
|
|
@@ -35,8 +42,16 @@ module Rails
|
|
35
42
|
validate_locale(request.preferred_language_from(available_locales))
|
36
43
|
end
|
37
44
|
|
45
|
+
def locale_from_user
|
46
|
+
validate_locale(user_locale)
|
47
|
+
end
|
48
|
+
|
49
|
+
def locale_from(key)
|
50
|
+
send("locale_from_#{key}")
|
51
|
+
end
|
52
|
+
|
38
53
|
def get_locale
|
39
|
-
|
54
|
+
detection_order.inject(nil) { |result, source| result || locale_from(source) } || default_locale
|
40
55
|
end
|
41
56
|
|
42
57
|
# set I18n.locale, default_url_options[:locale] and cookies[:locale] to the value returned by
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "rails_locale_detection"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mateo Murphy"]
|
12
|
-
s.date = "2012-05
|
12
|
+
s.date = "2012-06-05"
|
13
13
|
s.description = "Sets the current locale of a request using a combination of params, cookies, and http headers"
|
14
14
|
s.email = "mateo.murphy@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -36,6 +36,10 @@ describe Rails::LocaleDetection do
|
|
36
36
|
it "returns nil if the passed locale isn't valid" do
|
37
37
|
controller.validate_locale(:es).should be_nil
|
38
38
|
end
|
39
|
+
|
40
|
+
it "returns nil if nil is passed" do
|
41
|
+
controller.validate_locale(nil).should be_nil
|
42
|
+
end
|
39
43
|
end
|
40
44
|
|
41
45
|
describe '#locale_from_param' do
|
@@ -57,16 +61,16 @@ describe Rails::LocaleDetection do
|
|
57
61
|
describe '#locale_from_cookie' do
|
58
62
|
it "returns en if the param set was valid" do
|
59
63
|
controller.cookies[:locale] = 'en'
|
60
|
-
controller.
|
64
|
+
controller.locale_from_cookie.should eq(:en)
|
61
65
|
end
|
62
66
|
|
63
67
|
it "returns nil if the param set was not" do
|
64
68
|
controller.cookies[:locale] = 'es'
|
65
|
-
controller.
|
69
|
+
controller.locale_from_cookie.should be_nil
|
66
70
|
end
|
67
71
|
|
68
72
|
it "returns nil if not locale was set" do
|
69
|
-
controller.
|
73
|
+
controller.locale_from_cookie.should be_nil
|
70
74
|
end
|
71
75
|
end
|
72
76
|
|
@@ -84,27 +88,93 @@ describe Rails::LocaleDetection do
|
|
84
88
|
it "returns nil if not locale was set" do
|
85
89
|
controller.locale_from_request.should be_nil
|
86
90
|
end
|
87
|
-
end
|
91
|
+
end
|
88
92
|
|
89
|
-
describe '#
|
90
|
-
it "returns
|
91
|
-
controller.
|
93
|
+
describe '#locale_from_user' do
|
94
|
+
it "returns the locale of the user if it's valid" do
|
95
|
+
controller.user = MockUser.new(:en)
|
96
|
+
controller.locale_from_user.should eq(:en)
|
92
97
|
end
|
93
|
-
|
94
|
-
it "returns
|
95
|
-
controller.
|
96
|
-
controller.
|
98
|
+
|
99
|
+
it "returns nil if the locale of the use isn't valid" do
|
100
|
+
controller.user = MockUser.new(:es)
|
101
|
+
controller.locale_from_user.should be_nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe '#locale_from' do
|
106
|
+
before :all do
|
107
|
+
controller.params[:locale] = 'en'
|
108
|
+
controller.cookies[:locale] = 'fr'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns the locale set in the param" do
|
112
|
+
controller.locale_from(:param).should eq(:en)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "return the locale set in the cookie" do
|
116
|
+
controller.locale_from(:cookie).should eq(:fr)
|
97
117
|
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '#get_locale' do
|
121
|
+
context "with default detection order" do
|
122
|
+
before :all do
|
123
|
+
Rails::LocaleDetection.detection_order = [:user, :param, :cookie, :request]
|
124
|
+
end
|
125
|
+
|
126
|
+
it "returns default if nothing is set" do
|
127
|
+
controller.get_locale.should eq(:en)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "returns en if the params is set to en" do
|
131
|
+
controller.params[:locale] = "en"
|
132
|
+
controller.get_locale.should eq(:en)
|
133
|
+
end
|
134
|
+
|
135
|
+
it "returns fr if the cookie is set to fr" do
|
136
|
+
controller.cookies[:locale] = "fr"
|
137
|
+
controller.get_locale.should eq(:fr)
|
138
|
+
end
|
98
139
|
|
99
|
-
|
100
|
-
|
101
|
-
|
140
|
+
it "returns en if the request is set to en" do
|
141
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,en-gb;q=0.8,en;q=0.6'
|
142
|
+
controller.get_locale.should eq(:en)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "return fr if the user locale was set to fr" do
|
146
|
+
controller.user = MockUser.new(:en)
|
147
|
+
controller.get_locale.should eq(:en)
|
148
|
+
end
|
149
|
+
|
102
150
|
end
|
151
|
+
|
152
|
+
context "with a custom detection order" do
|
153
|
+
before :all do
|
154
|
+
Rails::LocaleDetection.detection_order = [:user, :param, :request]
|
155
|
+
end
|
156
|
+
|
157
|
+
it "returns return default if nothing is set" do
|
158
|
+
controller.get_locale.should eq(:en)
|
159
|
+
end
|
103
160
|
|
104
|
-
|
105
|
-
|
106
|
-
|
161
|
+
it "returns en if the params is set to en" do
|
162
|
+
controller.params[:locale] = "en"
|
163
|
+
controller.get_locale.should eq(:en)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "skips cookie" do
|
167
|
+
controller.cookies[:locale] = "fr"
|
168
|
+
controller.get_locale.should eq(:en)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "returns en if the request is set to en" do
|
172
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,en-gb;q=0.8,en;q=0.6'
|
173
|
+
controller.get_locale.should eq(:en)
|
174
|
+
end
|
175
|
+
|
107
176
|
end
|
177
|
+
|
108
178
|
end
|
109
179
|
|
110
180
|
describe '#set_locale' do
|
data/spec/support/mock.rb
CHANGED
@@ -11,11 +11,24 @@ end
|
|
11
11
|
class MockController
|
12
12
|
include Rails::LocaleDetection
|
13
13
|
|
14
|
-
attr_accessor :request, :params, :cookies, :default_url_options
|
14
|
+
attr_accessor :request, :params, :cookies, :default_url_options, :user
|
15
15
|
|
16
16
|
def initialize(request)
|
17
17
|
@request = request
|
18
18
|
@cookies = ActionDispatch::Cookies::CookieJar.new
|
19
19
|
@default_url_options = @params = {}
|
20
20
|
end
|
21
|
+
|
22
|
+
def user_locale
|
23
|
+
return user.locale if user
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class MockUser
|
29
|
+
attr_accessor :locale
|
30
|
+
|
31
|
+
def initialize(locale)
|
32
|
+
@locale = locale
|
33
|
+
end
|
21
34
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_locale_detection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mateo Murphy
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05
|
18
|
+
date: 2012-06-05 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
type: :runtime
|