i18n_strategy 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/i18n_strategy/initializer.rb +1 -1
- data/lib/i18n_strategy/strategy.rb +10 -12
- data/lib/i18n_strategy/version.rb +1 -1
- data/spec/controllers/locale_spec.rb +57 -29
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9fc1d2a94f64ccd3870735db85194c5f9e5a8a3
|
4
|
+
data.tar.gz: 7df0d69f52325135fb136c69293c932a249032b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cebe8451beb95867607626687ff5c5c97c1cc6b6985d3e6b3ae0fac6af432c473670fe92bef5ab0147eb60cd0bb5d2a80c833afbf889f6f05e80c31d1a5151e4
|
7
|
+
data.tar.gz: ceda354a65cec73626d7f62cf85e50949bc750c11b0e5b07df8744b142a8147dcf7bffeafb422f9b62769ac4cf1b742ac00945d5cfabfa72765fc3d7ee0cd1da
|
@@ -3,7 +3,7 @@ module I18nStrategy
|
|
3
3
|
|
4
4
|
class Initializer
|
5
5
|
def self.init(app)
|
6
|
-
I18nStrategy.strategy ||= I18nStrategy::Strategy
|
6
|
+
I18nStrategy.strategy ||= I18nStrategy::Strategy
|
7
7
|
|
8
8
|
ActiveSupport.on_load(:action_controller) do
|
9
9
|
ActionController::Base.send(:include, Filter)
|
@@ -2,20 +2,18 @@ require 'http_accept_language/parser'
|
|
2
2
|
|
3
3
|
module I18nStrategy
|
4
4
|
module Strategy
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
available = I18nStrategy.available_languages || []
|
5
|
+
def detect_locale
|
6
|
+
lang = nil
|
7
|
+
available = I18nStrategy.available_languages || []
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
lang || I18n.default_locale
|
9
|
+
if params[:locale] && available.include?(params[:locale])
|
10
|
+
lang = params[:locale]
|
11
|
+
else
|
12
|
+
parser = HttpAcceptLanguage::Parser.new(request.env['HTTP_ACCEPT_LANGUAGE'])
|
13
|
+
lang = parser.preferred_language_from(available)
|
18
14
|
end
|
15
|
+
|
16
|
+
lang || I18n.default_locale
|
19
17
|
end
|
20
18
|
end
|
21
19
|
end
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe RootController do
|
4
4
|
before {
|
5
|
-
I18nStrategy.strategy = I18nStrategy::Strategy
|
5
|
+
I18nStrategy.strategy = I18nStrategy::Strategy
|
6
6
|
I18nStrategy.available_languages = %w[en ja fr]
|
7
7
|
}
|
8
8
|
|
@@ -15,43 +15,69 @@ describe RootController do
|
|
15
15
|
}
|
16
16
|
end
|
17
17
|
|
18
|
-
context 'with preferred language' do
|
19
|
-
context '
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
context 'with preferred language from HTTP header' do
|
19
|
+
context 'and it is valid' do
|
20
|
+
context 'en (default language)' do
|
21
|
+
before {
|
22
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en'
|
23
|
+
get :index
|
24
|
+
}
|
25
|
+
|
26
|
+
it {
|
27
|
+
expect(response.body).to be == 'en'
|
28
|
+
}
|
29
|
+
end
|
24
30
|
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
context 'ja' do
|
32
|
+
before {
|
33
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja'
|
34
|
+
get :index
|
35
|
+
}
|
36
|
+
|
37
|
+
it {
|
38
|
+
expect(response.body).to be == 'ja'
|
39
|
+
}
|
40
|
+
end
|
28
41
|
end
|
29
42
|
|
30
|
-
context '
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
43
|
+
context 'and it is valid' do
|
44
|
+
before {
|
45
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'no_such_language'
|
46
|
+
get :index
|
47
|
+
}
|
35
48
|
|
36
|
-
|
37
|
-
|
38
|
-
|
49
|
+
it {
|
50
|
+
expect(response.body).to be == 'en'
|
51
|
+
}
|
39
52
|
end
|
40
53
|
end
|
41
54
|
|
42
55
|
context 'with preferred language from query param' do
|
43
|
-
context '
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
56
|
+
context 'and it is valid' do
|
57
|
+
context 'en (default language)' do
|
58
|
+
before {
|
59
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja'
|
60
|
+
get :index, :locale => 'en'
|
61
|
+
}
|
62
|
+
|
63
|
+
it {
|
64
|
+
expect(response.body).to be == 'en'
|
65
|
+
}
|
66
|
+
end
|
48
67
|
|
49
|
-
|
50
|
-
|
51
|
-
|
68
|
+
context 'fr' do
|
69
|
+
before {
|
70
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja'
|
71
|
+
get :index, :locale => 'fr'
|
72
|
+
}
|
73
|
+
|
74
|
+
it {
|
75
|
+
expect(response.body).to be == 'fr'
|
76
|
+
}
|
77
|
+
end
|
52
78
|
end
|
53
79
|
|
54
|
-
context '
|
80
|
+
context 'and it is invalid' do
|
55
81
|
before {
|
56
82
|
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja'
|
57
83
|
get :index, :locale => 'no_such_language'
|
@@ -74,9 +100,12 @@ describe RootController do
|
|
74
100
|
end
|
75
101
|
end
|
76
102
|
|
103
|
+
before {
|
104
|
+
I18nStrategy.strategy = MyStrategy
|
105
|
+
}
|
106
|
+
|
77
107
|
context 'with no preferred method' do
|
78
108
|
before {
|
79
|
-
I18nStrategy.strategy = MyStrategy
|
80
109
|
get :index
|
81
110
|
}
|
82
111
|
|
@@ -87,7 +116,6 @@ describe RootController do
|
|
87
116
|
|
88
117
|
context 'with preferred method' do
|
89
118
|
before {
|
90
|
-
I18nStrategy.strategy = MyStrategy
|
91
119
|
I18nStrategy.method_to_detect_locale = :another_detect_locale
|
92
120
|
get :index
|
93
121
|
}
|