http_accept_language 2.0.4 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/lib/http_accept_language.rb +2 -13
- data/lib/http_accept_language/auto_locale.rb +4 -3
- data/lib/http_accept_language/railtie.rb +1 -6
- data/lib/http_accept_language/version.rb +1 -1
- data/spec/auto_locale_spec.rb +7 -17
- data/spec/middleware_spec.rb +4 -4
- data/spec/parser_spec.rb +13 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08549bc678f764b0ee83c4520257c29165e16cab
|
4
|
+
data.tar.gz: 840fc4179bd28f157fd4eaf2d5feb4b0da2a2789
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53ce0048473fcee968b6fca1f832b7422281a0cfabdedacfdab8abb081674049e17c9c04f784e649bc78bc4d93f72a26dc5362d78379367b61e7937e9208358d
|
7
|
+
data.tar.gz: 2cc635241d1980aee9ef20e6997a6a235c9f674470e5b2c6f2b93a585fb9cd1e360e1fedc072fd1183fb40d5e72f31cc3adc2989f3dbc2e92cf1d12789b70720
|
data/README.md
CHANGED
@@ -43,10 +43,14 @@ class SomeController < ApplicationController
|
|
43
43
|
end
|
44
44
|
```
|
45
45
|
|
46
|
-
If you want to enable this behavior by default in
|
46
|
+
If you want to enable this behavior by default in your controllers, you can just include the provided concern:
|
47
47
|
|
48
48
|
```ruby
|
49
|
-
|
49
|
+
class ApplicationController < ActionController::Base
|
50
|
+
include HttpAcceptLanguage::AutoLocale
|
51
|
+
|
52
|
+
#...
|
53
|
+
end
|
50
54
|
```
|
51
55
|
|
52
56
|
To use the middleware in any Rack application, simply add the middleware:
|
data/lib/http_accept_language.rb
CHANGED
@@ -1,15 +1,4 @@
|
|
1
|
-
|
2
|
-
@@automatically_set_locale = false
|
3
|
-
|
4
|
-
def self.automatically_set_locale?
|
5
|
-
@@automatically_set_locale
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.automatically_set_locale=(set_locale)
|
9
|
-
@@automatically_set_locale = set_locale
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
1
|
+
require 'http_accept_language/auto_locale'
|
13
2
|
require 'http_accept_language/parser'
|
14
3
|
require 'http_accept_language/middleware'
|
15
|
-
require 'http_accept_language/railtie' if defined?(Rails::Railtie)
|
4
|
+
require 'http_accept_language/railtie' if defined?(Rails::Railtie)
|
@@ -1,5 +1,4 @@
|
|
1
|
-
require
|
2
|
-
require "i18n"
|
1
|
+
require 'active_support/concern'
|
3
2
|
|
4
3
|
module HttpAcceptLanguage
|
5
4
|
module AutoLocale
|
@@ -9,8 +8,10 @@ module HttpAcceptLanguage
|
|
9
8
|
before_filter :set_locale
|
10
9
|
end
|
11
10
|
|
11
|
+
private
|
12
|
+
|
12
13
|
def set_locale
|
13
14
|
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
|
14
15
|
end
|
15
16
|
end
|
16
|
-
end
|
17
|
+
end
|
@@ -1,15 +1,10 @@
|
|
1
1
|
module HttpAcceptLanguage
|
2
2
|
class Railtie < ::Rails::Railtie
|
3
|
-
initializer "http_accept_language.
|
3
|
+
initializer "http_accept_language.add_middleware" do |app|
|
4
4
|
app.middleware.use Middleware
|
5
5
|
|
6
6
|
ActiveSupport.on_load :action_controller do
|
7
7
|
include EasyAccess
|
8
|
-
|
9
|
-
if HttpAcceptLanguage.automatically_set_locale?
|
10
|
-
require "http_accept_language/auto_locale"
|
11
|
-
include AutoLocale
|
12
|
-
end
|
13
8
|
end
|
14
9
|
end
|
15
10
|
end
|
data/spec/auto_locale_spec.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
+
require 'i18n'
|
1
2
|
require 'http_accept_language/auto_locale'
|
3
|
+
require 'http_accept_language/parser'
|
4
|
+
require 'http_accept_language/middleware'
|
2
5
|
|
3
6
|
describe HttpAcceptLanguage::AutoLocale do
|
4
7
|
let(:controller_class) do
|
5
8
|
Class.new do
|
6
|
-
def self.before_filter(dummy)
|
7
|
-
|
9
|
+
def self.before_filter(dummy)
|
10
|
+
# dummy method
|
8
11
|
end
|
9
12
|
|
10
13
|
def http_accept_language
|
@@ -23,22 +26,9 @@ describe HttpAcceptLanguage::AutoLocale do
|
|
23
26
|
end
|
24
27
|
|
25
28
|
it "take a suitable locale" do
|
26
|
-
controller.set_locale
|
29
|
+
controller.send(:set_locale)
|
27
30
|
|
28
31
|
expect(I18n.locale).to eq(:ja)
|
29
32
|
end
|
30
33
|
end
|
31
|
-
|
32
|
-
context "available languages not includes accept_languages" do
|
33
|
-
before do
|
34
|
-
I18n.available_locales = [:de]
|
35
|
-
I18n.default_locale = :fr
|
36
|
-
end
|
37
|
-
|
38
|
-
it "take default_locale" do
|
39
|
-
controller.set_locale
|
40
|
-
|
41
|
-
expect(I18n.locale).to eq(:fr)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
34
|
+
end
|
data/spec/middleware_spec.rb
CHANGED
@@ -33,20 +33,20 @@ describe "Rack integration" do
|
|
33
33
|
app = lambda { |env| env }
|
34
34
|
middleware = HttpAcceptLanguage::Middleware.new(app)
|
35
35
|
middleware.call(env)
|
36
|
-
env.http_accept_language.user_preferred_languages.
|
36
|
+
expect(env.http_accept_language.user_preferred_languages).to eq %w{en}
|
37
37
|
env["HTTP_ACCEPT_LANGUAGE"] = "de"
|
38
38
|
middleware.call(env)
|
39
|
-
env.http_accept_language.user_preferred_languages.
|
39
|
+
expect(env.http_accept_language.user_preferred_languages).to eq %w{de}
|
40
40
|
end
|
41
41
|
|
42
42
|
it "decodes the HTTP_ACCEPT_LANGUAGE header" do
|
43
43
|
request_with_header 'en-us,en-gb;q=0.8,en;q=0.6,es-419'
|
44
|
-
r['user_preferred_languages'].
|
44
|
+
expect(r['user_preferred_languages']).to eq %w{en-US es-419 en-GB en}
|
45
45
|
end
|
46
46
|
|
47
47
|
it "finds the first available language" do
|
48
48
|
request_with_header 'en-us,en-gb;q=0.8,en;q=0.6,es-419', :preferred => %w(en en-GB)
|
49
|
-
r['preferred_language_from'].
|
49
|
+
expect(r['preferred_language_from']).to eq 'en-GB'
|
50
50
|
end
|
51
51
|
|
52
52
|
def request_with_header(header, params = {})
|
data/spec/parser_spec.rb
CHANGED
@@ -8,58 +8,58 @@ describe HttpAcceptLanguage::Parser do
|
|
8
8
|
|
9
9
|
it "should return empty array" do
|
10
10
|
parser.header = nil
|
11
|
-
parser.user_preferred_languages.
|
11
|
+
expect(parser.user_preferred_languages).to eq []
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should properly split" do
|
15
|
-
parser.user_preferred_languages.
|
15
|
+
expect(parser.user_preferred_languages).to eq %w{en-US es-419 en-GB en}
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should ignore jambled header" do
|
19
19
|
parser.header = 'odkhjf89fioma098jq .,.,'
|
20
|
-
parser.user_preferred_languages.
|
20
|
+
expect(parser.user_preferred_languages).to eq []
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should properly respect whitespace" do
|
24
24
|
parser.header = 'en-us, en-gb; q=0.8,en;q = 0.6,es-419'
|
25
|
-
parser.user_preferred_languages.
|
25
|
+
expect(parser.user_preferred_languages).to eq %w{en-US es-419 en-GB en}
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should find first available language" do
|
29
|
-
parser.preferred_language_from(%w{en en-GB}).
|
29
|
+
expect(parser.preferred_language_from(%w{en en-GB})).to eq "en-GB"
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should find first compatible language" do
|
33
|
-
parser.compatible_language_from(%w{en-hk}).
|
34
|
-
parser.compatible_language_from(%w{en}).
|
33
|
+
expect(parser.compatible_language_from(%w{en-hk})).to eq "en-hk"
|
34
|
+
expect(parser.compatible_language_from(%w{en})).to eq "en"
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should find first compatible from user preferred" do
|
38
38
|
parser.header = 'en-us,de-de'
|
39
|
-
parser.compatible_language_from(%w{de en}).
|
39
|
+
expect(parser.compatible_language_from(%w{de en})).to eq 'en'
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should accept symbols as available languages" do
|
43
43
|
parser.header = 'en-us'
|
44
|
-
parser.compatible_language_from([:"en-HK"]).
|
44
|
+
expect(parser.compatible_language_from([:"en-HK"])).to eq :"en-HK"
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should accept and ignore wildcards" do
|
48
48
|
parser.header = 'en-US,en,*'
|
49
|
-
parser.compatible_language_from([:"en-US"]).
|
49
|
+
expect(parser.compatible_language_from([:"en-US"])).to eq :"en-US"
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should sanitize available language names" do
|
53
|
-
parser.sanitize_available_locales(%w{en_UK-x3 en-US-x1 ja_JP-x2 pt-BR-x5 es-419-x4}).
|
53
|
+
expect(parser.sanitize_available_locales(%w{en_UK-x3 en-US-x1 ja_JP-x2 pt-BR-x5 es-419-x4})).to eq ["en-UK", "en-US", "ja-JP", "pt-BR", "es-419"]
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should accept available language names as symbols and return them as strings" do
|
57
|
-
parser.sanitize_available_locales([:en, :"en-US", :ca, :"ca-ES"]).
|
57
|
+
expect(parser.sanitize_available_locales([:en, :"en-US", :ca, :"ca-ES"])).to eq ["en", "en-US", "ca", "ca-ES"]
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should find most compatible language from user preferred" do
|
61
61
|
parser.header = 'ja,en-gb,en-us,fr-fr'
|
62
|
-
parser.language_region_compatible_from(%w{en-UK en-US ja-JP}).
|
62
|
+
expect(parser.language_region_compatible_from(%w{en-UK en-US ja-JP})).to eq "ja-JP"
|
63
63
|
end
|
64
64
|
|
65
65
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_accept_language
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- iain
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|