locale_detector 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -1
- data/lib/locale_detector/filter.rb +17 -11
- data/lib/locale_detector/version.rb +1 -1
- data/spec/locale_detector/filter_spec.rb +17 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -4,7 +4,7 @@ This Rails gem makes use of the HTTP_ACCEPT_LANGUAGE http header sent by web bro
|
|
4
4
|
|
5
5
|
When this fails (for example if there is no such http header - as is the case for Google bot), it will try to determine the locale based on the toplevel domain suffix (so it will set `'de'` for the `example.de` domain).
|
6
6
|
|
7
|
-
When both fail, it will fall back to `I18n.default_locale`, which should be set in application’s `config/application.rb` file (
|
7
|
+
When both fail, it will fall back to `I18n.default_locale`, which should be set in application’s `config/application.rb` file (`'en'` by default).
|
8
8
|
|
9
9
|
The HTTP_ACCEPT_LANGUAGE header parser is based on:
|
10
10
|
https://github.com/iain/http_accept_language/blob/master/lib/http_accept_language.rb
|
@@ -47,5 +47,18 @@ $ bundle
|
|
47
47
|
```
|
48
48
|
|
49
49
|
|
50
|
+
## Turning off the language autodetection
|
51
|
+
|
52
|
+
In certain cases we might want to turn off the locale/language autodetection. For example, we might want to give the user the ability to set his preferred language in his profile and respect this setting.
|
53
|
+
|
54
|
+
In order to do that, you need to set a `:language` session key in your app:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
session[:language] = 'pl'
|
58
|
+
```
|
59
|
+
|
60
|
+
In consequence, regardless of the user browser’s language, the `I18n.locale` will be set to `'pl'`.
|
61
|
+
|
62
|
+
|
50
63
|
|
51
64
|
Copyright © 2011 Exvo.com Development BV, released under the MIT license
|
@@ -3,22 +3,28 @@ module LocaleDetector
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
|
6
|
+
append_before_filter :set_locale
|
7
7
|
end
|
8
8
|
|
9
9
|
protected
|
10
10
|
|
11
11
|
def set_locale
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
if session[:language].present?
|
13
|
+
# set locale from session
|
14
|
+
I18n.locale = session[:language]
|
15
|
+
else
|
16
|
+
# set locale from http header or request host
|
17
|
+
I18n.locale = begin
|
18
|
+
request.env['HTTP_ACCEPT_LANGUAGE'].split(/\s*,\s*/).collect do |l|
|
19
|
+
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
20
|
+
l.split(';q=')
|
21
|
+
end.sort do |x,y|
|
22
|
+
raise "Incorrect format" unless x.first =~ /^[a-z\-]+$/i
|
23
|
+
y.last.to_f <=> x.last.to_f
|
24
|
+
end.first.first.gsub(/-[a-z]+$/i, '').downcase
|
25
|
+
rescue # rescue (anything) from the malformed (or missing) accept language headers
|
26
|
+
country_to_language(request.host.split('.').last)
|
27
|
+
end
|
22
28
|
end
|
23
29
|
end
|
24
30
|
|
@@ -5,21 +5,36 @@ describe LocaleDetector::Filter do
|
|
5
5
|
def set_locale(opts = {})
|
6
6
|
filter = Object.new.extend(LocaleDetector::Filter)
|
7
7
|
|
8
|
-
request =
|
8
|
+
request =
|
9
|
+
if opts[:language].present?
|
9
10
|
double('request', :env => { 'HTTP_ACCEPT_LANGUAGE' => opts[:language] })
|
10
|
-
|
11
|
+
elsif opts[:host].present?
|
11
12
|
double('request', :env => nil, :host => opts[:host])
|
12
13
|
end
|
13
14
|
|
15
|
+
session =
|
16
|
+
if opts[:session_language].present?
|
17
|
+
double('session', :[] => opts[:session_language])
|
18
|
+
else
|
19
|
+
double('session', :[] => '')
|
20
|
+
end
|
21
|
+
|
22
|
+
filter.stub(:session).and_return(session)
|
14
23
|
filter.stub(:request).and_return(request)
|
15
24
|
filter.send(:set_locale)
|
16
25
|
end
|
17
26
|
|
27
|
+
context "session[:language] locale overwrite" do
|
28
|
+
specify { set_locale(:language => 'pt-BR', :session_language => 'pl').should eql('pl') }
|
29
|
+
end
|
30
|
+
|
18
31
|
context "http header locale setting" do
|
19
32
|
specify { set_locale(:language => 'pl').should eql('pl') }
|
20
33
|
|
21
34
|
specify { set_locale(:language => 'pl-PL').should eql('pl') }
|
22
35
|
|
36
|
+
specify { set_locale(:language => 'pt-BR').should eql('pt') }
|
37
|
+
|
23
38
|
specify { set_locale(:language => 'pl,en-us;q=0.7,en;q=0.3').should eql('pl') }
|
24
39
|
|
25
40
|
specify { set_locale(:language => 'lt,en-us;q=0.8,en;q=0.6,ru;q=0.4,pl;q=0.2').should eql('lt') }
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: locale_detector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Pawe\xC5\x82 Go\xC5\x9Bcicki"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-08-
|
18
|
+
date: 2011-08-17 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|