locale_setter 0.1.1 → 0.3.0
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.
- checksums.yaml +7 -0
- data/README.md +35 -5
- data/Rakefile +5 -0
- data/lib/locale_setter/generic.rb +34 -0
- data/lib/locale_setter/http.rb +2 -2
- data/lib/locale_setter/matcher.rb +10 -5
- data/lib/locale_setter/param.rb +2 -2
- data/lib/locale_setter/rails.rb +20 -3
- data/lib/locale_setter/user.rb +14 -3
- data/lib/locale_setter/version.rb +1 -1
- data/lib/locale_setter.rb +4 -32
- data/locale_setter.gemspec +3 -0
- data/spec/locale/generic_spec.rb +69 -0
- data/spec/locale/http_spec.rb +7 -11
- data/spec/locale/matcher_spec.rb +4 -0
- data/spec/locale/rails_spec.rb +63 -0
- data/spec/locale/user_spec.rb +16 -3
- metadata +41 -12
- data/spec/locale_setter_spec.rb +0 -172
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2177a5b71ff691ac29241b327d2deaef3456e08e
|
4
|
+
data.tar.gz: 0fbf15da496596204083077d5c622441cd5dcf9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ed37ab8f9b5ec6ac8527c8a53ed535d1a4f6ac05b9fa9283c4d9ad67e02da60696fa32c7d3d968578d24523d9fb92ef97bd3c3f04cc6a173949786216efa4763
|
7
|
+
data.tar.gz: da77e1502788c59d1eb1713f0bdf53366795ef5a0f020057364b9383eb4f1be3b40470f407d7cd24a7a37e539c533bd5414fd3e10e007ead9fc161f75fc117cd
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Then execute:
|
|
18
18
|
$ bundle
|
19
19
|
```
|
20
20
|
|
21
|
-
### Application Configuration
|
21
|
+
### Rails Application Configuration
|
22
22
|
|
23
23
|
Include the module in your `app/controllers/application_controller.rb`:
|
24
24
|
|
@@ -26,11 +26,31 @@ Include the module in your `app/controllers/application_controller.rb`:
|
|
26
26
|
class ApplicationController < ActionController::Base
|
27
27
|
protect_from_forgery
|
28
28
|
|
29
|
-
include LocaleSetter
|
29
|
+
include LocaleSetter::Rails
|
30
30
|
end
|
31
31
|
```
|
32
32
|
|
33
|
-
*Note:* If you have before filters or a module that handles user authentication, have that _above_
|
33
|
+
*Note:* If you have before filters or a module that handles user authentication, have that _above_ this new `include` so it happens first.
|
34
|
+
|
35
|
+
### Non-Rails Applications
|
36
|
+
|
37
|
+
The library can be used outside of Rails by accessing `LocaleSetter::Generic` directly. You need to pass in your I18n class and the data sources, like this:
|
38
|
+
|
39
|
+
```
|
40
|
+
# Example Input Data
|
41
|
+
request = {'HTTP_ACCEPT_LANGUAGE' = 'en,es;0.6'}
|
42
|
+
params = {:locale = 'en'}
|
43
|
+
user = User.first
|
44
|
+
i18n = I18n
|
45
|
+
|
46
|
+
# Set the .locale of I18n
|
47
|
+
LocaleSetter::Generic.set_locale(i18n,
|
48
|
+
{:env => request,
|
49
|
+
:params => params,
|
50
|
+
:user => user})
|
51
|
+
```
|
52
|
+
|
53
|
+
The `i18n.locale=` will be called with the local selected from the passed data. `:env`, `:params`, and `:user` are all optional.
|
34
54
|
|
35
55
|
## How It Works
|
36
56
|
|
@@ -93,8 +113,6 @@ Note that care has been taken to prevent a symbol-table-overflow denial of servi
|
|
93
113
|
|
94
114
|
If your system has authentication, then you likely use have a `current_user` helper method available. `LocaleSetter` will call `locale` on current user, expecting to get back a string response.
|
95
115
|
|
96
|
-
It's up to you to compute / store this data.
|
97
|
-
|
98
116
|
#### Storing a User Preference
|
99
117
|
|
100
118
|
The easiest solution is to add a column to your users table:
|
@@ -114,6 +132,18 @@ Then, allow them to edit this preference wherever they edit other profile items
|
|
114
132
|
|
115
133
|
Remember that you may need to modify the `user.rb` if you're filtering mass-assignment parameters.
|
116
134
|
|
135
|
+
#### Using a Different Method / Column
|
136
|
+
|
137
|
+
`LocaleSetter::User` can be configured to call a method other than `.locale` on the user.
|
138
|
+
|
139
|
+
Anytime after the library is loaded, like in a Rails initializer, use the `locale_method=` method:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
LocaleSetter::User.locale_method = :my_locale
|
143
|
+
```
|
144
|
+
|
145
|
+
Subsequent calls to `LocaleSetter::User.for` will use the specified method.
|
146
|
+
|
117
147
|
### HTTP Headers
|
118
148
|
|
119
149
|
Every request coming into your web server includes a ton of header information. One key/value pair looks like this:
|
data/Rakefile
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
module LocaleSetter
|
2
|
+
module Generic
|
3
|
+
|
4
|
+
def self.set_locale(i18n, options = {:params => nil,
|
5
|
+
:user => nil,
|
6
|
+
:env => nil})
|
7
|
+
|
8
|
+
i18n.locale = from_params(options[:params], available(i18n)) ||
|
9
|
+
from_user(options[:user], available(i18n)) ||
|
10
|
+
from_http(options[:env], available(i18n)) ||
|
11
|
+
i18n.default_locale
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.available(i18n)
|
15
|
+
i18n.available_locales.map(&:to_s)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.from_user(user, available)
|
19
|
+
LocaleSetter::User.for(user, available)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.from_http(env, available)
|
23
|
+
if env && env[HTTP_HEADER]
|
24
|
+
LocaleSetter::HTTP.for(env[HTTP_HEADER], available)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.from_params(params, available)
|
29
|
+
if params && params[URL_PARAM]
|
30
|
+
LocaleSetter::Param.for(params[URL_PARAM], available)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/locale_setter/http.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module LocaleSetter
|
2
2
|
module HTTP
|
3
|
-
def self.for(accept_language)
|
4
|
-
LocaleSetter::Matcher.match(AcceptLanguageParser.parse(accept_language))
|
3
|
+
def self.for(accept_language, available)
|
4
|
+
LocaleSetter::Matcher.match(AcceptLanguageParser.parse(accept_language), available)
|
5
5
|
end
|
6
6
|
|
7
7
|
module AcceptLanguageParser
|
@@ -1,8 +1,11 @@
|
|
1
1
|
module LocaleSetter
|
2
2
|
module Matcher
|
3
|
-
def self.match(requested, against
|
4
|
-
|
5
|
-
matched
|
3
|
+
def self.match(requested, against)
|
4
|
+
table = generate_lookup_table(against)
|
5
|
+
matched = (sanitize(requested) & table.keys).first
|
6
|
+
if matched
|
7
|
+
table[matched].to_sym
|
8
|
+
end
|
6
9
|
end
|
7
10
|
|
8
11
|
def self.sanitize(input)
|
@@ -17,8 +20,10 @@ module LocaleSetter
|
|
17
20
|
locale.to_s.downcase.strip
|
18
21
|
end
|
19
22
|
|
20
|
-
def self.
|
21
|
-
|
23
|
+
def self.generate_lookup_table(locales)
|
24
|
+
table = {}
|
25
|
+
locales.each { |l| table[sanitize_one(l)] = l}
|
26
|
+
table
|
22
27
|
end
|
23
28
|
end
|
24
29
|
end
|
data/lib/locale_setter/param.rb
CHANGED
data/lib/locale_setter/rails.rb
CHANGED
@@ -1,17 +1,34 @@
|
|
1
1
|
module LocaleSetter
|
2
2
|
module Rails
|
3
|
-
|
3
|
+
def self.included(controller)
|
4
|
+
controller.before_filter :set_locale
|
5
|
+
end
|
4
6
|
|
5
7
|
def default_url_options(options = {})
|
6
8
|
if i18n.locale == i18n.default_locale
|
7
9
|
options
|
8
10
|
else
|
9
|
-
{
|
11
|
+
{URL_PARAM => i18n.locale}.merge(options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def set_locale
|
16
|
+
Generic.set_locale(
|
17
|
+
i18n,
|
18
|
+
{:params => params,
|
19
|
+
:user => locale_user,
|
20
|
+
:env => request.env}
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def locale_user
|
25
|
+
if respond_to?(:current_user) && current_user
|
26
|
+
current_user
|
10
27
|
end
|
11
28
|
end
|
12
29
|
|
13
30
|
def i18n
|
14
|
-
|
31
|
+
I18n
|
15
32
|
end
|
16
33
|
end
|
17
34
|
end
|
data/lib/locale_setter/user.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
module LocaleSetter
|
2
2
|
module User
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
@@user_locale_method = :locale
|
4
|
+
|
5
|
+
def self.for(user, available)
|
6
|
+
if user && user.respond_to?(locale_method) &&
|
7
|
+
user.send(locale_method) && !user.send(locale_method).empty?
|
8
|
+
LocaleSetter::Matcher.match user.send(locale_method), available
|
6
9
|
end
|
7
10
|
end
|
11
|
+
|
12
|
+
def self.locale_method
|
13
|
+
@@user_locale_method
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.locale_method=(method_name)
|
17
|
+
@@user_locale_method = method_name
|
18
|
+
end
|
8
19
|
end
|
9
20
|
end
|
data/lib/locale_setter.rb
CHANGED
@@ -4,38 +4,10 @@ require "locale_setter/rails"
|
|
4
4
|
require "locale_setter/http"
|
5
5
|
require "locale_setter/user"
|
6
6
|
require "locale_setter/param"
|
7
|
+
require "locale_setter/generic"
|
7
8
|
|
8
9
|
module LocaleSetter
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
if controller.respond_to?(:before_filter)
|
13
|
-
controller.before_filter :set_locale
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def set_locale
|
18
|
-
i18n.locale = from_params ||
|
19
|
-
from_user ||
|
20
|
-
from_http ||
|
21
|
-
i18n.default_locale
|
22
|
-
end
|
23
|
-
|
24
|
-
def from_user
|
25
|
-
if respond_to?(:current_user) && current_user
|
26
|
-
LocaleSetter::User.for(current_user)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def from_http
|
31
|
-
if respond_to?(:request) && request.env && request.env['HTTP_ACCEPT_LANGUAGE']
|
32
|
-
LocaleSetter::HTTP.for(request.env['HTTP_ACCEPT_LANGUAGE'])
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def from_params
|
37
|
-
if respond_to?(:params) && params[:locale]
|
38
|
-
LocaleSetter::Param.for(params[:locale])
|
39
|
-
end
|
40
|
-
end
|
10
|
+
HTTP_HEADER = 'HTTP_ACCEPT_LANGUAGE'
|
11
|
+
URL_PARAM = :locale
|
12
|
+
USER_METHOD = :locale
|
41
13
|
end
|
data/locale_setter.gemspec
CHANGED
@@ -16,4 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake", "~>10.0.3"
|
21
|
+
gem.add_development_dependency "rspec", "~>2.13.0"
|
19
22
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LocaleSetter::Generic do
|
4
|
+
let(:setter){ LocaleSetter::Generic }
|
5
|
+
let(:i18n){ I18n }
|
6
|
+
|
7
|
+
describe "#set_locale" do
|
8
|
+
context "with nothing" do
|
9
|
+
it "uses the default" do
|
10
|
+
setter.set_locale(i18n)
|
11
|
+
i18n.locale.should == i18n.default_locale
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "with HTTP headers" do
|
16
|
+
let(:request){ {'HTTP_ACCEPT_LANGUAGE' => "es,en"} }
|
17
|
+
|
18
|
+
it "makes use of the HTTP headers" do
|
19
|
+
i18n.available_locales = [:en, :es]
|
20
|
+
setter.set_locale(i18n, {:env => request})
|
21
|
+
i18n.locale.should == :es
|
22
|
+
end
|
23
|
+
|
24
|
+
it "only sets an available locale" do
|
25
|
+
i18n.available_locales = [:arr, :en]
|
26
|
+
setter.set_locale(i18n, {:env => request})
|
27
|
+
i18n.locale.should == :en
|
28
|
+
end
|
29
|
+
|
30
|
+
it "does nothing when HTTP_ACCEPT_LANGUAGE is missing" do
|
31
|
+
setter.set_locale(i18n, {:env => {}})
|
32
|
+
i18n.locale.should == i18n.default_locale
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with a current_user who has a locale" do
|
37
|
+
it "uses the stored locale" do
|
38
|
+
i18n.available_locales = [:en, :user_specified]
|
39
|
+
user = OpenStruct.new( {:locale => :user_specified} )
|
40
|
+
setter.set_locale(i18n, {:user => user} )
|
41
|
+
i18n.locale.should == :user_specified
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "with url parameters" do
|
46
|
+
before(:each) do
|
47
|
+
i18n.available_locales = [:en, :param_specified]
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:params){ {:locale => "param_specified"} }
|
51
|
+
|
52
|
+
it "uses the URL parameter" do
|
53
|
+
setter.set_locale(i18n, {:params => params} )
|
54
|
+
i18n.locale.should == :param_specified
|
55
|
+
end
|
56
|
+
|
57
|
+
it "only allows supported locales" do
|
58
|
+
i18n.available_locales = [:en]
|
59
|
+
setter.set_locale(i18n, {:params => params} )
|
60
|
+
i18n.locale.should == i18n.default_locale
|
61
|
+
end
|
62
|
+
|
63
|
+
it "does not pollute the symbol table when given an unsuported locale" do
|
64
|
+
expect { setter.set_locale(i18n, { :params => {:locale => "bad_param"} }) }.
|
65
|
+
to_not change{ Symbol.all_symbols.count }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/spec/locale/http_spec.rb
CHANGED
@@ -2,41 +2,37 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "LocaleSetter::HTTP" do
|
4
4
|
describe ".for" do
|
5
|
+
let(:available){ ['en', 'es'] }
|
6
|
+
|
5
7
|
context "when the first choice is supported" do
|
6
|
-
before(:each){ I18n.available_locales = [:en, :es] }
|
7
|
-
|
8
8
|
context "given 'en'" do
|
9
9
|
it "returns :en" do
|
10
|
-
LocaleSetter::HTTP.for("en").should == :en
|
10
|
+
LocaleSetter::HTTP.for("en", available).should == :en
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
context "given two acceptable locales" do
|
15
15
|
it "returns :en" do
|
16
|
-
LocaleSetter::HTTP.for("en,es").should == :en
|
16
|
+
LocaleSetter::HTTP.for("en,es", available).should == :en
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
context "when the first choice is not supported" do
|
22
|
-
before(:each){ I18n.available_locales = [:es] }
|
23
|
-
|
24
22
|
context "given 'en,es'" do
|
25
23
|
it "returns :es" do
|
26
|
-
LocaleSetter::HTTP.for("
|
24
|
+
LocaleSetter::HTTP.for("de,es", available).should == :es
|
27
25
|
end
|
28
26
|
end
|
29
27
|
end
|
30
28
|
|
31
29
|
context "when using preference weightings" do
|
32
|
-
before(:each){ I18n.available_locales = [:en, :es] }
|
33
|
-
|
34
30
|
it "returns :en" do
|
35
|
-
LocaleSetter::HTTP.for("en;1,es;0.8").should == :en
|
31
|
+
LocaleSetter::HTTP.for("en;1,es;0.8", available).should == :en
|
36
32
|
end
|
37
33
|
|
38
34
|
it "handles misordered preferences" do
|
39
|
-
LocaleSetter::HTTP.for("es;0.8,en;1").should == :en
|
35
|
+
LocaleSetter::HTTP.for("es;0.8,en;1", available).should == :en
|
40
36
|
end
|
41
37
|
end
|
42
38
|
end
|
data/spec/locale/matcher_spec.rb
CHANGED
@@ -5,6 +5,10 @@ describe "LocaleSetter::Matcher" do
|
|
5
5
|
LocaleSetter::Matcher.match(['EN-US'],['en-us']).should == :'en-us'
|
6
6
|
end
|
7
7
|
|
8
|
+
it "can properly match when the available locales are not all lower case" do
|
9
|
+
LocaleSetter::Matcher.match(['es-CL'],['es-CL']).should == :'es-CL'
|
10
|
+
end
|
11
|
+
|
8
12
|
it "can match using a single string" do
|
9
13
|
LocaleSetter::Matcher.match('en', ['en']).should == :en
|
10
14
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LocaleSetter::Rails do
|
4
|
+
it "exists" do
|
5
|
+
expect{ LocaleSetter::Rails }.to_not raise_error
|
6
|
+
end
|
7
|
+
|
8
|
+
class BareController
|
9
|
+
def self.before_filter(name); end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe ".included" do
|
13
|
+
it "sets a before filter" do
|
14
|
+
BareController.should_receive(:before_filter).with(:set_locale)
|
15
|
+
BareController.send(:include, LocaleSetter::Rails)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "skips setting the before_filter if not supported" do
|
19
|
+
expect{ BareController.send(:include, LocaleSetter::Rails) }.to_not raise_error
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Controller < BareController
|
24
|
+
include LocaleSetter::Rails
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:controller){ Controller.new }
|
28
|
+
|
29
|
+
before(:each) do
|
30
|
+
controller.i18n.locale = :es
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#default_url_options" do
|
34
|
+
it "adds a :locale key" do
|
35
|
+
controller.default_url_options({})[:locale].should be
|
36
|
+
end
|
37
|
+
|
38
|
+
it "does not require a parameter" do
|
39
|
+
expect{ controller.default_url_options }.to_not raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it "builds on passed in options" do
|
43
|
+
result = controller.default_url_options({:test => true})
|
44
|
+
result[:test].should be
|
45
|
+
result[:locale].should be
|
46
|
+
end
|
47
|
+
|
48
|
+
it "defers to a passed in locale" do
|
49
|
+
result = controller.default_url_options({:locale => 'abc'})
|
50
|
+
result[:locale].should == 'abc'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "doesn't appent a locale if it's the default" do
|
54
|
+
controller.i18n.locale = controller.i18n.default_locale
|
55
|
+
controller.default_url_options({})[:locale].should_not be
|
56
|
+
end
|
57
|
+
|
58
|
+
it "appends a locale when not the default" do
|
59
|
+
controller.i18n.locale = :sample
|
60
|
+
controller.default_url_options({})[:locale].should == :sample
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/locale/user_spec.rb
CHANGED
@@ -4,17 +4,30 @@ describe 'LocaleSetter::User' do
|
|
4
4
|
describe '#for' do
|
5
5
|
it "ignores a blank stored locale" do
|
6
6
|
blank = OpenStruct.new({:locale => ""})
|
7
|
-
LocaleSetter::User.for(blank).should_not be
|
7
|
+
LocaleSetter::User.for(blank, ["default"]).should_not be
|
8
8
|
end
|
9
9
|
|
10
10
|
it "ignores a stored locale that is not available" do
|
11
11
|
invalid = OpenStruct.new({:locale => "woof"})
|
12
|
-
LocaleSetter::User.for(invalid).should_not be
|
12
|
+
LocaleSetter::User.for(invalid, ["default"]).should_not be
|
13
13
|
end
|
14
14
|
|
15
15
|
it "only tries current_user if it offers a locale" do
|
16
16
|
class NoLocaleUser; end
|
17
|
-
LocaleSetter::User.for(NoLocaleUser.new).should_not be
|
17
|
+
LocaleSetter::User.for(NoLocaleUser.new, ["default"]).should_not be
|
18
|
+
end
|
19
|
+
|
20
|
+
it "uses a configurable field name" do
|
21
|
+
class MyLocaleUser
|
22
|
+
def my_locale
|
23
|
+
"arr"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
LocaleSetter::User.locale_method = :my_locale
|
28
|
+
|
29
|
+
user = MyLocaleUser.new
|
30
|
+
LocaleSetter::User.for(user, ["arr"]).should == :arr
|
18
31
|
end
|
19
32
|
end
|
20
33
|
end
|
metadata
CHANGED
@@ -1,16 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: locale_setter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Jeff Casimir
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
11
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 10.0.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 10.0.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.13.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.13.0
|
14
41
|
description: Automatically set per-request locale in Rails applications
|
15
42
|
email:
|
16
43
|
- jeff@casimircreative.com
|
@@ -25,6 +52,7 @@ files:
|
|
25
52
|
- README.md
|
26
53
|
- Rakefile
|
27
54
|
- lib/locale_setter.rb
|
55
|
+
- lib/locale_setter/generic.rb
|
28
56
|
- lib/locale_setter/http.rb
|
29
57
|
- lib/locale_setter/matcher.rb
|
30
58
|
- lib/locale_setter/param.rb
|
@@ -32,40 +60,41 @@ files:
|
|
32
60
|
- lib/locale_setter/user.rb
|
33
61
|
- lib/locale_setter/version.rb
|
34
62
|
- locale_setter.gemspec
|
63
|
+
- spec/locale/generic_spec.rb
|
35
64
|
- spec/locale/http_spec.rb
|
36
65
|
- spec/locale/matcher_spec.rb
|
66
|
+
- spec/locale/rails_spec.rb
|
37
67
|
- spec/locale/user_spec.rb
|
38
|
-
- spec/locale_setter_spec.rb
|
39
68
|
- spec/spec_helper.rb
|
40
69
|
- spec/support/i18n.rb
|
41
70
|
homepage: http://github.com/jcasimir/locale_setter
|
42
71
|
licenses: []
|
72
|
+
metadata: {}
|
43
73
|
post_install_message:
|
44
74
|
rdoc_options: []
|
45
75
|
require_paths:
|
46
76
|
- lib
|
47
77
|
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
-
none: false
|
49
78
|
requirements:
|
50
|
-
- -
|
79
|
+
- - '>='
|
51
80
|
- !ruby/object:Gem::Version
|
52
81
|
version: '0'
|
53
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
83
|
requirements:
|
56
|
-
- -
|
84
|
+
- - '>='
|
57
85
|
- !ruby/object:Gem::Version
|
58
86
|
version: '0'
|
59
87
|
requirements: []
|
60
88
|
rubyforge_project:
|
61
|
-
rubygems_version:
|
89
|
+
rubygems_version: 2.0.0
|
62
90
|
signing_key:
|
63
|
-
specification_version:
|
91
|
+
specification_version: 4
|
64
92
|
summary: Automatically set per-request locale in Rails applications
|
65
93
|
test_files:
|
94
|
+
- spec/locale/generic_spec.rb
|
66
95
|
- spec/locale/http_spec.rb
|
67
96
|
- spec/locale/matcher_spec.rb
|
97
|
+
- spec/locale/rails_spec.rb
|
68
98
|
- spec/locale/user_spec.rb
|
69
|
-
- spec/locale_setter_spec.rb
|
70
99
|
- spec/spec_helper.rb
|
71
100
|
- spec/support/i18n.rb
|
data/spec/locale_setter_spec.rb
DELETED
@@ -1,172 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe LocaleSetter do
|
4
|
-
it "exists" do
|
5
|
-
expect{ LocaleSetter }.to_not raise_error
|
6
|
-
end
|
7
|
-
|
8
|
-
class BareController; end
|
9
|
-
|
10
|
-
describe ".included" do
|
11
|
-
it "sets a before filter" do
|
12
|
-
BareController.should_receive(:before_filter).with(:set_locale)
|
13
|
-
BareController.send(:include, LocaleSetter)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "skips setting the before_filter if not supported" do
|
17
|
-
expect{ BareController.send(:include, LocaleSetter) }.to_not raise_error
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class Controller
|
22
|
-
include LocaleSetter
|
23
|
-
end
|
24
|
-
|
25
|
-
let(:controller){ Controller.new }
|
26
|
-
|
27
|
-
describe "#default_url_options" do
|
28
|
-
it "adds a :locale key" do
|
29
|
-
controller.default_url_options({})[:locale].should be
|
30
|
-
end
|
31
|
-
|
32
|
-
it "does not require a parameter" do
|
33
|
-
expect{ controller.default_url_options }.to_not raise_error
|
34
|
-
end
|
35
|
-
|
36
|
-
it "builds on passed in options" do
|
37
|
-
result = controller.default_url_options({:test => true})
|
38
|
-
result[:test].should be
|
39
|
-
result[:locale].should be
|
40
|
-
end
|
41
|
-
|
42
|
-
it "defers to a passed in locale" do
|
43
|
-
result = controller.default_url_options({:locale => 'abc'})
|
44
|
-
result[:locale].should == 'abc'
|
45
|
-
end
|
46
|
-
|
47
|
-
it "doesn't appent a locale if it's the default" do
|
48
|
-
controller.i18n.locale = controller.i18n.default_locale
|
49
|
-
controller.default_url_options({})[:locale].should_not be
|
50
|
-
end
|
51
|
-
|
52
|
-
it "appends a locale when not the default" do
|
53
|
-
controller.i18n.locale = :sample
|
54
|
-
controller.default_url_options({})[:locale].should == :sample
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe "#set_locale" do
|
59
|
-
context "with nothing" do
|
60
|
-
let(:controller){ Controller.new }
|
61
|
-
|
62
|
-
it "uses the default" do
|
63
|
-
controller.set_locale
|
64
|
-
controller.i18n.locale.should == controller.i18n.default_locale
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context "with HTTP headers" do
|
69
|
-
let(:controller){ HTTPController.new }
|
70
|
-
|
71
|
-
class HTTPController < Controller
|
72
|
-
def request
|
73
|
-
OpenStruct.new(:env => {'HTTP_ACCEPT_LANGUAGE' => "es,en"})
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
it "makes use of the HTTP headers" do
|
78
|
-
controller.set_locale
|
79
|
-
controller.i18n.locale.should == :es
|
80
|
-
end
|
81
|
-
|
82
|
-
it "only sets an available locale" do
|
83
|
-
I18n.available_locales = [:arr, :en]
|
84
|
-
controller.set_locale
|
85
|
-
controller.i18n.locale.should == :en
|
86
|
-
end
|
87
|
-
|
88
|
-
it "does nothing when HTTP_ACCEPT_LANGUAGE is missing" do
|
89
|
-
class BlankHTTPController < Controller
|
90
|
-
def request
|
91
|
-
OpenStruct.new(:env => {})
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
blank = BlankHTTPController.new
|
96
|
-
blank.set_locale.should == blank.i18n.default_locale
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
context "with a current_user who has a locale" do
|
101
|
-
let(:controller){ UserController.new }
|
102
|
-
|
103
|
-
before(:each) do
|
104
|
-
controller.i18n.available_locales = [:en, :user_specified]
|
105
|
-
end
|
106
|
-
|
107
|
-
class UserController < HTTPController
|
108
|
-
def current_user
|
109
|
-
OpenStruct.new({:locale => :user_specified})
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
it "prioritizes the current_user preference over HTTP" do
|
114
|
-
LocaleSetter::HTTP.should_not_receive(:for)
|
115
|
-
controller.set_locale
|
116
|
-
end
|
117
|
-
|
118
|
-
it "uses the stored locale" do
|
119
|
-
controller.set_locale
|
120
|
-
controller.i18n.locale.should == :user_specified
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
context "with url parameters" do
|
125
|
-
let(:controller){ ParamController.new }
|
126
|
-
|
127
|
-
before(:each) do
|
128
|
-
controller.i18n.available_locales = [:en, :param_specified]
|
129
|
-
end
|
130
|
-
|
131
|
-
class ParamController < Controller
|
132
|
-
def params
|
133
|
-
{:locale => "param_specified"}
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
it "uses the URL parameter" do
|
138
|
-
controller.set_locale
|
139
|
-
controller.i18n.locale.should == :param_specified
|
140
|
-
end
|
141
|
-
|
142
|
-
it "only allows supported locales" do
|
143
|
-
controller.i18n.available_locales = [:en]
|
144
|
-
controller.set_locale
|
145
|
-
controller.i18n.locale.should == controller.i18n.default_locale
|
146
|
-
end
|
147
|
-
|
148
|
-
it "does not pollute the symbol table when given an unsuported locale" do
|
149
|
-
class BadParamController < Controller
|
150
|
-
def params
|
151
|
-
{:locale => "bad_param"}
|
152
|
-
end
|
153
|
-
end
|
154
|
-
expect { BadParamController.new.set_locale }.to_not change{ Symbol.all_symbols.count }
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
describe "#i18n" do
|
160
|
-
it "uses the pre-set i18n library" do
|
161
|
-
stand_in = OpenStruct
|
162
|
-
controller.i18n = stand_in
|
163
|
-
controller.i18n.should == stand_in
|
164
|
-
end
|
165
|
-
|
166
|
-
class I18n; end
|
167
|
-
|
168
|
-
it "uses the default I18n library when not overridden" do
|
169
|
-
controller.i18n.should == I18n
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|