rails_locale_detection 1.3.2 → 2.0.0.pre2
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/.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/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
18
|
+
#
|
19
|
+
# * Create a file at ~/.gitignore
|
20
|
+
# * Include files you want ignored
|
21
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
22
|
+
#
|
23
|
+
# After doing this, these files will be ignored in all your git projects,
|
24
|
+
# saving you from having to 'pollute' every project you touch with them
|
25
|
+
#
|
26
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
27
|
+
#
|
28
|
+
# For MacOS:
|
29
|
+
#
|
30
|
+
#.DS_Store
|
31
|
+
|
32
|
+
# For TextMate
|
33
|
+
#*.tmproj
|
34
|
+
#tmtags
|
35
|
+
|
36
|
+
# For emacs:
|
37
|
+
#*~
|
38
|
+
#\#*
|
39
|
+
#.\#*
|
40
|
+
|
41
|
+
# For vim:
|
42
|
+
#*.swp
|
43
|
+
|
44
|
+
# For redcar:
|
45
|
+
#.redcar
|
46
|
+
|
47
|
+
# For rubinius:
|
48
|
+
#*.rbc
|
49
|
+
|
50
|
+
/.rvmrc
|
data/Gemfile
CHANGED
@@ -1,15 +1,3 @@
|
|
1
|
-
source "
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
|
-
|
4
|
-
gem 'http_accept_language'
|
5
|
-
|
6
|
-
# Add dependencies to develop your gem here.
|
7
|
-
# Include everything needed to run rake, tests, features, etc.
|
8
|
-
group :development do
|
9
|
-
gem 'i18n'
|
10
|
-
gem 'timecop'
|
11
|
-
gem 'actionpack', '~> 3.2.13'
|
12
|
-
gem "rspec", "~> 2.12.0"
|
13
|
-
gem "bundler", "~> 1.3.4"
|
14
|
-
gem "jeweler", "~> 1.8.4"
|
15
|
-
end
|
3
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,6 +1,16 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails_locale_detection (2.0.0.pre1)
|
5
|
+
http_accept_language (~> 1.0.2)
|
6
|
+
rails (>= 3.2.13)
|
7
|
+
|
1
8
|
GEM
|
2
|
-
remote:
|
9
|
+
remote: https://rubygems.org/
|
3
10
|
specs:
|
11
|
+
actionmailer (3.2.13)
|
12
|
+
actionpack (= 3.2.13)
|
13
|
+
mail (~> 2.5.3)
|
4
14
|
actionpack (3.2.13)
|
5
15
|
activemodel (= 3.2.13)
|
6
16
|
activesupport (= 3.2.13)
|
@@ -14,57 +24,91 @@ GEM
|
|
14
24
|
activemodel (3.2.13)
|
15
25
|
activesupport (= 3.2.13)
|
16
26
|
builder (~> 3.0.0)
|
27
|
+
activerecord (3.2.13)
|
28
|
+
activemodel (= 3.2.13)
|
29
|
+
activesupport (= 3.2.13)
|
30
|
+
arel (~> 3.0.2)
|
31
|
+
tzinfo (~> 0.3.29)
|
32
|
+
activeresource (3.2.13)
|
33
|
+
activemodel (= 3.2.13)
|
34
|
+
activesupport (= 3.2.13)
|
17
35
|
activesupport (3.2.13)
|
18
36
|
i18n (= 0.6.1)
|
19
37
|
multi_json (~> 1.0)
|
38
|
+
arel (3.0.2)
|
20
39
|
builder (3.0.4)
|
21
|
-
diff-lcs (1.1
|
40
|
+
diff-lcs (1.2.1)
|
22
41
|
erubis (2.7.0)
|
23
|
-
|
42
|
+
gem-release (0.5.3)
|
24
43
|
hike (1.2.1)
|
25
44
|
http_accept_language (1.0.2)
|
26
45
|
i18n (0.6.1)
|
27
|
-
jeweler (1.8.4)
|
28
|
-
bundler (~> 1.0)
|
29
|
-
git (>= 1.2.5)
|
30
|
-
rake
|
31
|
-
rdoc
|
32
46
|
journey (1.0.4)
|
33
47
|
json (1.7.7)
|
34
|
-
|
48
|
+
mail (2.5.3)
|
49
|
+
i18n (>= 0.4.0)
|
50
|
+
mime-types (~> 1.16)
|
51
|
+
treetop (~> 1.4.8)
|
52
|
+
mime-types (1.21)
|
53
|
+
multi_json (1.7.2)
|
54
|
+
polyglot (0.3.3)
|
35
55
|
rack (1.4.5)
|
36
56
|
rack-cache (1.2)
|
37
57
|
rack (>= 0.4)
|
58
|
+
rack-ssl (1.3.3)
|
59
|
+
rack
|
38
60
|
rack-test (0.6.2)
|
39
61
|
rack (>= 1.0)
|
40
|
-
|
41
|
-
|
62
|
+
rails (3.2.13)
|
63
|
+
actionmailer (= 3.2.13)
|
64
|
+
actionpack (= 3.2.13)
|
65
|
+
activerecord (= 3.2.13)
|
66
|
+
activeresource (= 3.2.13)
|
67
|
+
activesupport (= 3.2.13)
|
68
|
+
bundler (~> 1.0)
|
69
|
+
railties (= 3.2.13)
|
70
|
+
railties (3.2.13)
|
71
|
+
actionpack (= 3.2.13)
|
72
|
+
activesupport (= 3.2.13)
|
73
|
+
rack-ssl (~> 1.3.2)
|
74
|
+
rake (>= 0.8.7)
|
75
|
+
rdoc (~> 3.4)
|
76
|
+
thor (>= 0.14.6, < 2.0)
|
77
|
+
rake (10.0.4)
|
78
|
+
rdoc (3.12.2)
|
42
79
|
json (~> 1.4)
|
43
|
-
rspec (2.
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
rspec-
|
48
|
-
|
49
|
-
|
50
|
-
|
80
|
+
rspec-core (2.13.1)
|
81
|
+
rspec-expectations (2.13.0)
|
82
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
83
|
+
rspec-mocks (2.13.0)
|
84
|
+
rspec-rails (2.13.0)
|
85
|
+
actionpack (>= 3.0)
|
86
|
+
activesupport (>= 3.0)
|
87
|
+
railties (>= 3.0)
|
88
|
+
rspec-core (~> 2.13.0)
|
89
|
+
rspec-expectations (~> 2.13.0)
|
90
|
+
rspec-mocks (~> 2.13.0)
|
51
91
|
sprockets (2.2.2)
|
52
92
|
hike (~> 1.2)
|
53
93
|
multi_json (~> 1.0)
|
54
94
|
rack (~> 1.0)
|
55
95
|
tilt (~> 1.1, != 1.3.0)
|
96
|
+
thor (0.18.1)
|
56
97
|
tilt (1.3.6)
|
57
98
|
timecop (0.6.1)
|
99
|
+
treetop (1.4.12)
|
100
|
+
polyglot
|
101
|
+
polyglot (>= 0.3.1)
|
102
|
+
tzinfo (0.3.37)
|
58
103
|
|
59
104
|
PLATFORMS
|
60
105
|
ruby
|
61
106
|
|
62
107
|
DEPENDENCIES
|
63
|
-
actionpack (~> 3.2.13)
|
64
|
-
activesupport (~> 3.2.13)
|
65
108
|
bundler (~> 1.3.4)
|
66
|
-
|
67
|
-
i18n
|
68
|
-
|
69
|
-
|
70
|
-
|
109
|
+
gem-release (~> 0.5.3)
|
110
|
+
i18n (~> 0.6.1)
|
111
|
+
rails_locale_detection!
|
112
|
+
rake (~> 10.0.4)
|
113
|
+
rspec-rails (~> 2.13.0)
|
114
|
+
timecop (~> 0.6.1)
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT LICENSE
|
2
|
+
|
3
|
+
Copyright (c) 2013 Mateo Murphy <mateo.murphy@gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -3,50 +3,53 @@
|
|
3
3
|
[](https://travis-ci.org/mateomurphy/rails_locale_detection)
|
4
4
|
[](https://gemnasium.com/mateomurphy/rails_locale_detection)
|
5
5
|
|
6
|
-
Sets the current locale of a request using a combination of params, cookies, http headers, and an optional user object.
|
6
|
+
Sets the current locale of a request using a combination of params, cookies, http headers, and an optional user object.
|
7
7
|
|
8
|
-
In turn, it checks the value of params[:locale]
|
9
|
-
corresponds to the available locales, then stores the
|
10
|
-
is provided,
|
8
|
+
In turn, it checks the value of `params[:locale]`, `cookies[:locale]` and `HTTP_ACCEPT_LANGUAGE` headers to find a locale that
|
9
|
+
corresponds to the available locales, then stores the locale in a cookie for future requests. If a `user_locale` method
|
10
|
+
is provided, any valid return value will be used, with preference over the other locale detection methods except for `params[:locale]`.
|
11
|
+
|
12
|
+
|
13
|
+
## Changes to version 2
|
14
|
+
|
15
|
+
* Rails 4 compatibility
|
16
|
+
* Namespace has been changed from Rails::LocaleDetection to RailsLocaleDetection,
|
17
|
+
although the previous namespace is still supported for legacy config files
|
18
|
+
* Locale detection has been moved to a seperate object, to avoid polluting controllers with extra methods.
|
19
|
+
* Detection order has been changed to `param` first, as this make more sense in most contexts.
|
20
|
+
* Shortcut locale accessors are now mixed into views as well
|
11
21
|
|
12
22
|
## Usage
|
13
23
|
|
14
|
-
Include the gem in your Gemfile
|
24
|
+
Include the gem in your Gemfile
|
15
25
|
|
16
26
|
gem 'rails_locale_detection'
|
17
|
-
|
27
|
+
|
18
28
|
Set your default and available locales
|
19
29
|
|
20
30
|
I18n.default_locale = :en
|
21
31
|
I18n.available_locales = [:en, :fr]
|
22
|
-
|
23
|
-
Call `set_locale` as a filter in your controllers
|
24
|
-
|
25
|
-
class ApplicationController < ActionController::Base
|
26
|
-
before_filter :set_locale
|
27
|
-
|
28
|
-
end
|
29
32
|
|
30
33
|
To support user locales, add a `user_locale` method
|
31
34
|
|
32
35
|
class ApplicationController < ActionController::Base
|
33
|
-
before_filter :set_locale
|
34
|
-
|
35
36
|
def user_locale
|
36
37
|
current_user.locale if current_user
|
37
38
|
end
|
38
|
-
|
39
39
|
end
|
40
40
|
|
41
|
+
Methods for accessing `current_locale`, `available_locales`, and `alternate_locales` are available in controller and views
|
41
42
|
|
42
43
|
## Configuration
|
43
44
|
|
44
45
|
Configuration is done via a block that can be added as an initializer:
|
45
46
|
|
46
|
-
|
47
|
-
config.locale_expiry = 3.months
|
47
|
+
RailsLocaleDetection.config do |config|
|
48
|
+
config.locale_expiry = 3.months
|
48
49
|
config.set_default_url_option = :always # valid values are true, false, :always, :never and :explicity
|
49
|
-
config.detection_order = [:
|
50
|
+
config.detection_order = [:param, :user, :cookie, :request]
|
51
|
+
config.automatically_detect = true
|
52
|
+
config.locale_key = :locale
|
50
53
|
end
|
51
54
|
|
52
55
|
The configuration options:
|
@@ -57,9 +60,12 @@ The configuration options:
|
|
57
60
|
* `false` or `:never` never sets the option
|
58
61
|
* `:explicity` sets the option only when a param[:locale] is present
|
59
62
|
* `detection_order` set the order in which locale detection occurs. Omit values to skip those sources
|
63
|
+
* `automatically_detect` configures automatic inclusion of the detection callback.
|
64
|
+
You can set this to false and include the callback yourself by calling `detect_locale` in your controller as required.
|
65
|
+
* `locale_key` configures the key used for `param` and `cookie`
|
60
66
|
|
61
67
|
## Contributing to rails_locale_detection
|
62
|
-
|
68
|
+
|
63
69
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
64
70
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
65
71
|
* Fork the project
|
data/Rakefile
CHANGED
@@ -1,49 +1,6 @@
|
|
1
|
-
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
|
3
|
-
require 'rubygems'
|
4
|
-
require 'bundler'
|
5
|
-
begin
|
6
|
-
Bundler.setup(:default, :development)
|
7
|
-
rescue Bundler::BundlerError => e
|
8
|
-
$stderr.puts e.message
|
9
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
-
exit e.status_code
|
11
|
-
end
|
12
|
-
require 'rake'
|
13
|
-
|
14
|
-
require 'jeweler'
|
15
|
-
Jeweler::Tasks.new do |gem|
|
16
|
-
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
-
gem.name = "rails_locale_detection"
|
18
|
-
gem.homepage = "http://github.com/mateomurphy/rails_locale_detection"
|
19
|
-
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{locale setting for rails project}
|
21
|
-
gem.description = %Q{Sets the current locale of a request using a combination of params, cookies, and http headers}
|
22
|
-
gem.email = "mateo.murphy@gmail.com"
|
23
|
-
gem.authors = ["Mateo Murphy"]
|
24
|
-
# dependencies defined in Gemfile
|
25
|
-
end
|
26
|
-
Jeweler::RubygemsDotOrgTasks.new
|
27
|
-
|
28
|
-
require 'rspec/core'
|
29
3
|
require 'rspec/core/rake_task'
|
30
|
-
RSpec::Core::RakeTask.new(:spec)
|
31
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
-
end
|
33
|
-
|
34
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
-
spec.rcov = true
|
37
|
-
end
|
38
|
-
|
39
|
-
task :default => :spec
|
40
|
-
|
41
|
-
require 'rdoc/task'
|
42
|
-
RDoc::Task.new do |rdoc|
|
43
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
44
5
|
|
45
|
-
|
46
|
-
rdoc.title = "rails_locale_detection #{version}"
|
47
|
-
rdoc.rdoc_files.include('README*')
|
48
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
-
end
|
6
|
+
task :default => [:spec]
|
@@ -1,5 +1,34 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'i18n'
|
1
3
|
require 'http_accept_language'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
+
require 'rails_locale_detection/detection_methods'
|
5
|
+
require 'rails_locale_detection/locale_accessors'
|
6
|
+
require 'rails_locale_detection/locale_detector'
|
7
|
+
require 'rails_locale_detection/controller_methods'
|
4
8
|
|
5
|
-
|
9
|
+
module RailsLocaleDetection
|
10
|
+
require 'rails_locale_detection/railtie' if defined?(Rails)
|
11
|
+
|
12
|
+
mattr_accessor :locale_expiry
|
13
|
+
@@locale_expiry = 3.months
|
14
|
+
|
15
|
+
mattr_accessor :set_default_url_option
|
16
|
+
@@set_default_url_option = :always
|
17
|
+
|
18
|
+
mattr_accessor :detection_order
|
19
|
+
@@detection_order = [:param, :user, :cookie, :request]
|
20
|
+
|
21
|
+
mattr_accessor :automatically_detect
|
22
|
+
@@automatically_detect = true
|
23
|
+
|
24
|
+
mattr_accessor :locale_key
|
25
|
+
@@locale_key = :locale
|
26
|
+
|
27
|
+
def self.config
|
28
|
+
yield self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module Rails
|
33
|
+
LocaleDetection = ::RailsLocaleDetection
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RailsLocaleDetection
|
2
|
+
module ControllerMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include LocaleAccessors
|
5
|
+
|
6
|
+
included do
|
7
|
+
detect_locale if RailsLocaleDetection.automatically_detect
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def detect_locale
|
12
|
+
append_before_filter LocaleDetector
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def user_locale
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module RailsLocaleDetection
|
2
|
+
module DetectionMethods
|
3
|
+
# returns the (symbolized) value passed if it's in the available_locales
|
4
|
+
def validate_locale(locale)
|
5
|
+
locale.to_sym if locale && available_locales.include?(locale.to_sym)
|
6
|
+
end
|
7
|
+
|
8
|
+
def locale_from_param
|
9
|
+
validate_locale(params[locale_key])
|
10
|
+
end
|
11
|
+
|
12
|
+
def locale_from_cookie
|
13
|
+
validate_locale(cookies[locale_key])
|
14
|
+
end
|
15
|
+
|
16
|
+
def locale_from_request
|
17
|
+
validate_locale(request.preferred_language_from(available_locales))
|
18
|
+
end
|
19
|
+
|
20
|
+
def locale_from_user
|
21
|
+
validate_locale(user_locale)
|
22
|
+
end
|
23
|
+
|
24
|
+
def locale_from(key)
|
25
|
+
send("locale_from_#{key}")
|
26
|
+
end
|
27
|
+
|
28
|
+
def detect_locale
|
29
|
+
RailsLocaleDetection.detection_order.inject(nil) { |result, source| result || locale_from(source) } || default_locale
|
30
|
+
end
|
31
|
+
|
32
|
+
# set I18n.locale, default_url_options[:locale] and cookies[:locale] to the value returned by
|
33
|
+
# detect_locale
|
34
|
+
def set_locale
|
35
|
+
self.current_locale = detect_locale
|
36
|
+
default_url_options[locale_key] = current_locale if set_default_url_option_for_request?
|
37
|
+
cookies[locale_key] = { :value => current_locale, :expires => RailsLocaleDetection.locale_expiry.from_now }
|
38
|
+
end
|
39
|
+
|
40
|
+
# returns true if the default url option should be set for this request
|
41
|
+
def set_default_url_option_for_request?
|
42
|
+
RailsLocaleDetection.set_default_url_option === true || RailsLocaleDetection.set_default_url_option == :always || RailsLocaleDetection.set_default_url_option == :explicitly && params[locale_key].present?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|