rails_locale_detection 1.3.2 → 2.0.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +50 -0
- data/Gemfile +2 -14
- data/Gemfile.lock +70 -26
- data/LICENSE +21 -0
- data/README.md +26 -20
- data/Rakefile +3 -46
- data/lib/rails_locale_detection.rb +32 -3
- data/lib/rails_locale_detection/controller_methods.rb +20 -0
- data/lib/rails_locale_detection/detection_methods.rb +45 -0
- data/lib/rails_locale_detection/locale_accessors.rb +23 -0
- data/lib/rails_locale_detection/locale_detector.rb +22 -0
- data/lib/rails_locale_detection/railtie.rb +13 -0
- data/lib/rails_locale_detection/version.rb +3 -0
- data/rails_locale_detection.gemspec +26 -71
- data/spec/controllers/callback_tests_controller_spec.rb +15 -0
- data/spec/rails_locale_detection/config_spec.rb +17 -0
- data/spec/rails_locale_detection/controller_methods_spec.rb +19 -0
- data/spec/rails_locale_detection/locale_accessors_spec.rb +30 -0
- data/spec/{rails_locale_detection_spec.rb → rails_locale_detection/locale_detector_spec.rb} +141 -144
- data/spec/spec_helper.rb +7 -12
- data/spec/support/{mock.rb → mocks.rb} +41 -14
- data/spec/support/rails.rb +30 -0
- metadata +63 -49
- data/LICENSE.txt +0 -20
- data/VERSION +0 -1
- data/lib/rails/locale_detection.rb +0 -74
data/spec/spec_helper.rb
CHANGED
@@ -1,17 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require 'rspec'
|
5
|
-
require 'i18n'
|
6
|
-
require 'action_dispatch/middleware/cookies'
|
1
|
+
require 'abstract_controller'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'active_support'
|
4
|
+
require 'rspec/rails'
|
7
5
|
require 'timecop'
|
8
|
-
|
9
6
|
require 'rails_locale_detection'
|
10
7
|
|
11
|
-
# Requires supporting files with custom matchers and macros, etc,
|
12
|
-
# in ./support/ and its subdirectories.
|
13
8
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
I18n.default_locale = :en
|
11
|
+
I18n.available_locales = [:en, :fr]
|
12
|
+
Timecop.freeze
|
@@ -1,34 +1,61 @@
|
|
1
1
|
class MockRequest
|
2
2
|
include HttpAcceptLanguage
|
3
|
-
|
3
|
+
|
4
4
|
attr_accessor :env
|
5
|
-
|
5
|
+
|
6
6
|
def initialize
|
7
7
|
@env = {'HTTP_ACCEPT_LANGUAGE' => ''}
|
8
8
|
end
|
9
|
+
|
10
|
+
def host
|
11
|
+
"localhost"
|
12
|
+
end
|
13
|
+
|
14
|
+
def ssl?
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
def cookies
|
19
|
+
{}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class MockUser
|
24
|
+
attr_accessor :locale
|
25
|
+
|
26
|
+
def initialize(locale)
|
27
|
+
@locale = locale
|
28
|
+
end
|
9
29
|
end
|
10
30
|
|
11
31
|
class MockController
|
12
|
-
|
13
|
-
|
32
|
+
|
14
33
|
attr_accessor :request, :params, :cookies, :default_url_options, :user
|
15
|
-
|
34
|
+
|
16
35
|
def initialize(request)
|
17
36
|
@request = request
|
18
|
-
@cookies = ActionDispatch::Cookies::CookieJar.
|
37
|
+
@cookies = ActionDispatch::Cookies::CookieJar.build(request)
|
19
38
|
@default_url_options = @params = {}
|
20
39
|
end
|
21
|
-
|
40
|
+
|
22
41
|
def user_locale
|
23
42
|
return user.locale if user
|
24
43
|
end
|
25
|
-
|
26
|
-
end
|
27
44
|
|
28
|
-
class
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
45
|
+
class << self
|
46
|
+
attr_reader :before_filters
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.before_filter(*args)
|
50
|
+
@before_filters ||= []
|
51
|
+
@before_filters << args
|
33
52
|
end
|
53
|
+
|
54
|
+
def self.append_before_filter(*args)
|
55
|
+
@before_filters ||= []
|
56
|
+
@before_filters << args
|
57
|
+
end
|
58
|
+
|
59
|
+
include RailsLocaleDetection::ControllerMethods
|
60
|
+
|
34
61
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class MockApplication
|
2
|
+
def self.env_config
|
3
|
+
{}
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.routes
|
7
|
+
@routes ||= ActionDispatch::Routing::RouteSet.new
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
MockApplication.routes.draw do
|
12
|
+
resource :callback_tests
|
13
|
+
end
|
14
|
+
|
15
|
+
::Rails.application = MockApplication
|
16
|
+
|
17
|
+
ActionController::Base.send :include, RailsLocaleDetection::ControllerMethods
|
18
|
+
ActionController::Base.send :include, Rails.application.routes.url_helpers
|
19
|
+
|
20
|
+
class CallbackTestsController < ActionController::Base
|
21
|
+
|
22
|
+
def user_locale
|
23
|
+
:fr
|
24
|
+
end
|
25
|
+
|
26
|
+
def show
|
27
|
+
render :text => current_locale
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_locale_detection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0.pre2
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mateo Murphy
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-04-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: rails
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 3.2.13
|
22
22
|
type: :runtime
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 3.2.13
|
30
30
|
- !ruby/object:Gem::Dependency
|
@@ -32,57 +32,57 @@ dependencies:
|
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
none: false
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ~>
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
37
|
+
version: 1.0.2
|
38
38
|
type: :runtime
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
45
|
+
version: 1.0.2
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
name: i18n
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ~>
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 0.6.1
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 0.6.1
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: timecop
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ~>
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
69
|
+
version: 0.6.1
|
70
70
|
type: :development
|
71
71
|
prerelease: false
|
72
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
|
-
- -
|
75
|
+
- - ~>
|
76
76
|
- !ruby/object:Gem::Version
|
77
|
-
version:
|
77
|
+
version: 0.6.1
|
78
78
|
- !ruby/object:Gem::Dependency
|
79
|
-
name:
|
79
|
+
name: rspec-rails
|
80
80
|
requirement: !ruby/object:Gem::Requirement
|
81
81
|
none: false
|
82
82
|
requirements:
|
83
83
|
- - ~>
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
-
version:
|
85
|
+
version: 2.13.0
|
86
86
|
type: :development
|
87
87
|
prerelease: false
|
88
88
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -90,15 +90,15 @@ dependencies:
|
|
90
90
|
requirements:
|
91
91
|
- - ~>
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version:
|
93
|
+
version: 2.13.0
|
94
94
|
- !ruby/object:Gem::Dependency
|
95
|
-
name:
|
95
|
+
name: bundler
|
96
96
|
requirement: !ruby/object:Gem::Requirement
|
97
97
|
none: false
|
98
98
|
requirements:
|
99
99
|
- - ~>
|
100
100
|
- !ruby/object:Gem::Version
|
101
|
-
version:
|
101
|
+
version: 1.3.4
|
102
102
|
type: :development
|
103
103
|
prerelease: false
|
104
104
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -106,15 +106,15 @@ dependencies:
|
|
106
106
|
requirements:
|
107
107
|
- - ~>
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
109
|
+
version: 1.3.4
|
110
110
|
- !ruby/object:Gem::Dependency
|
111
|
-
name:
|
111
|
+
name: gem-release
|
112
112
|
requirement: !ruby/object:Gem::Requirement
|
113
113
|
none: false
|
114
114
|
requirements:
|
115
115
|
- - ~>
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
117
|
+
version: 0.5.3
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -122,15 +122,15 @@ dependencies:
|
|
122
122
|
requirements:
|
123
123
|
- - ~>
|
124
124
|
- !ruby/object:Gem::Version
|
125
|
-
version:
|
125
|
+
version: 0.5.3
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
127
|
+
name: rake
|
128
128
|
requirement: !ruby/object:Gem::Requirement
|
129
129
|
none: false
|
130
130
|
requirements:
|
131
131
|
- - ~>
|
132
132
|
- !ruby/object:Gem::Version
|
133
|
-
version:
|
133
|
+
version: 10.0.4
|
134
134
|
type: :development
|
135
135
|
prerelease: false
|
136
136
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -138,33 +138,42 @@ dependencies:
|
|
138
138
|
requirements:
|
139
139
|
- - ~>
|
140
140
|
- !ruby/object:Gem::Version
|
141
|
-
version:
|
141
|
+
version: 10.0.4
|
142
142
|
description: Sets the current locale of a request using a combination of params, cookies,
|
143
143
|
and http headers
|
144
|
-
email:
|
144
|
+
email:
|
145
|
+
- mateo.murphy@gmail.com
|
145
146
|
executables: []
|
146
147
|
extensions: []
|
147
|
-
extra_rdoc_files:
|
148
|
-
- LICENSE.txt
|
149
|
-
- README.md
|
148
|
+
extra_rdoc_files: []
|
150
149
|
files:
|
151
150
|
- .document
|
151
|
+
- .gitignore
|
152
152
|
- .rspec
|
153
|
+
- .rvmrc
|
153
154
|
- Gemfile
|
154
155
|
- Gemfile.lock
|
155
|
-
- LICENSE
|
156
|
+
- LICENSE
|
156
157
|
- README.md
|
157
158
|
- Rakefile
|
158
|
-
- VERSION
|
159
|
-
- lib/rails/locale_detection.rb
|
160
159
|
- lib/rails_locale_detection.rb
|
160
|
+
- lib/rails_locale_detection/controller_methods.rb
|
161
|
+
- lib/rails_locale_detection/detection_methods.rb
|
162
|
+
- lib/rails_locale_detection/locale_accessors.rb
|
163
|
+
- lib/rails_locale_detection/locale_detector.rb
|
164
|
+
- lib/rails_locale_detection/railtie.rb
|
165
|
+
- lib/rails_locale_detection/version.rb
|
161
166
|
- rails_locale_detection.gemspec
|
162
|
-
- spec/
|
167
|
+
- spec/controllers/callback_tests_controller_spec.rb
|
168
|
+
- spec/rails_locale_detection/config_spec.rb
|
169
|
+
- spec/rails_locale_detection/controller_methods_spec.rb
|
170
|
+
- spec/rails_locale_detection/locale_accessors_spec.rb
|
171
|
+
- spec/rails_locale_detection/locale_detector_spec.rb
|
163
172
|
- spec/spec_helper.rb
|
164
|
-
- spec/support/
|
165
|
-
|
166
|
-
|
167
|
-
|
173
|
+
- spec/support/mocks.rb
|
174
|
+
- spec/support/rails.rb
|
175
|
+
homepage: https://github.com/mateomurphy/rails_locale_detection
|
176
|
+
licenses: []
|
168
177
|
post_install_message:
|
169
178
|
rdoc_options: []
|
170
179
|
require_paths:
|
@@ -175,19 +184,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
184
|
- - ! '>='
|
176
185
|
- !ruby/object:Gem::Version
|
177
186
|
version: '0'
|
178
|
-
segments:
|
179
|
-
- 0
|
180
|
-
hash: -3187113159151487164
|
181
187
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
188
|
none: false
|
183
189
|
requirements:
|
184
|
-
- - ! '
|
190
|
+
- - ! '>'
|
185
191
|
- !ruby/object:Gem::Version
|
186
|
-
version:
|
192
|
+
version: 1.3.1
|
187
193
|
requirements: []
|
188
194
|
rubyforge_project:
|
189
195
|
rubygems_version: 1.8.25
|
190
196
|
signing_key:
|
191
197
|
specification_version: 3
|
192
|
-
summary:
|
193
|
-
test_files:
|
198
|
+
summary: Locale setting for rails project
|
199
|
+
test_files:
|
200
|
+
- spec/controllers/callback_tests_controller_spec.rb
|
201
|
+
- spec/rails_locale_detection/config_spec.rb
|
202
|
+
- spec/rails_locale_detection/controller_methods_spec.rb
|
203
|
+
- spec/rails_locale_detection/locale_accessors_spec.rb
|
204
|
+
- spec/rails_locale_detection/locale_detector_spec.rb
|
205
|
+
- spec/spec_helper.rb
|
206
|
+
- spec/support/mocks.rb
|
207
|
+
- spec/support/rails.rb
|
data/LICENSE.txt
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2011 Mateo Murphy
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.3.2
|
@@ -1,74 +0,0 @@
|
|
1
|
-
module Rails
|
2
|
-
module LocaleDetection
|
3
|
-
mattr_accessor :locale_expiry
|
4
|
-
@@locale_expiry = 3.months
|
5
|
-
|
6
|
-
mattr_accessor :set_default_url_option
|
7
|
-
@@set_default_url_option = :always
|
8
|
-
|
9
|
-
mattr_accessor :detection_order
|
10
|
-
@@detection_order = [:user, :param, :cookie, :request]
|
11
|
-
|
12
|
-
def self.config
|
13
|
-
yield self
|
14
|
-
end
|
15
|
-
|
16
|
-
def available_locales
|
17
|
-
I18n.available_locales
|
18
|
-
end
|
19
|
-
|
20
|
-
def default_locale
|
21
|
-
I18n.default_locale
|
22
|
-
end
|
23
|
-
|
24
|
-
def user_locale
|
25
|
-
nil
|
26
|
-
end
|
27
|
-
|
28
|
-
# returns the (symbolized) value passed if it's in the available_locales
|
29
|
-
def validate_locale(locale)
|
30
|
-
locale.to_sym if locale && available_locales.include?(locale.to_sym)
|
31
|
-
end
|
32
|
-
|
33
|
-
def locale_from_param
|
34
|
-
validate_locale(params[:locale])
|
35
|
-
end
|
36
|
-
|
37
|
-
def locale_from_cookie
|
38
|
-
validate_locale(cookies[:locale])
|
39
|
-
end
|
40
|
-
|
41
|
-
def locale_from_request
|
42
|
-
validate_locale(request.preferred_language_from(available_locales))
|
43
|
-
end
|
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
|
-
|
53
|
-
def get_locale
|
54
|
-
detection_order.inject(nil) { |result, source| result || locale_from(source) } || default_locale
|
55
|
-
end
|
56
|
-
|
57
|
-
# returns true if the default url option should be set for this request
|
58
|
-
def set_default_url_option_for_request?
|
59
|
-
set_default_url_option === true || set_default_url_option == :always || set_default_url_option == :explicitly && params[:locale].present?
|
60
|
-
end
|
61
|
-
|
62
|
-
# set I18n.locale, default_url_options[:locale] and cookies[:locale] to the value returned by
|
63
|
-
# get_locale
|
64
|
-
def set_locale
|
65
|
-
I18n.locale = get_locale
|
66
|
-
|
67
|
-
default_url_options[:locale] = I18n.locale if set_default_url_option_for_request?
|
68
|
-
|
69
|
-
cookies[:locale] = { :value => I18n.locale, :expires => locale_expiry.from_now }
|
70
|
-
|
71
|
-
I18n.locale
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|