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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8f322e914fee571f93b3f8c009a4132f9263f21
4
- data.tar.gz: 50a48cb1200a9120866d829d812d91a3d407bb34
3
+ metadata.gz: 08549bc678f764b0ee83c4520257c29165e16cab
4
+ data.tar.gz: 840fc4179bd28f157fd4eaf2d5feb4b0da2a2789
5
5
  SHA512:
6
- metadata.gz: 89a7c19b3a2df5f32995ee264df668fca0e98865be74e74441b5882e096f4e9862f0e2f4266c3d4fd199a78a0a68afcd54dfbeddc7253a3f1fe35deaea03b2da
7
- data.tar.gz: 0f3be427948e4b2a74a1727b70d84a6b51b025e6c0cef8360df8a9ccaab9cd429aceed6d65e93b20dd3c0a6efc539ddf46c70ee4c927f7064e185c1b64047cb1
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 all of your controllers, add the following in an initializer (`config/initializers/http_accept_language.rb`, for example):
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
- HttpAcceptLanguage.automatically_set_locale = true
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:
@@ -1,15 +1,4 @@
1
- module HttpAcceptLanguage
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 "active_support/concern"
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.setup" do |app|
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
@@ -1,3 +1,3 @@
1
1
  module HttpAcceptLanguage
2
- VERSION = '2.0.4'
2
+ VERSION = '2.0.5'
3
3
  end
@@ -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
@@ -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.should eq %w{en}
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.should eq %w{de}
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'].should eq %w{en-US es-419 en-GB en}
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'].should eq 'en-GB'
49
+ expect(r['preferred_language_from']).to eq 'en-GB'
50
50
  end
51
51
 
52
52
  def request_with_header(header, params = {})
@@ -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.should eq []
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.should eq %w{en-US es-419 en-GB en}
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.should eq []
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.should eq %w{en-US es-419 en-GB en}
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}).should eq "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}).should eq "en-hk"
34
- parser.compatible_language_from(%w{en}).should eq "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}).should eq '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"]).should eq :"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"]).should eq :"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}).should eq ["en-UK", "en-US", "ja-JP", "pt-BR", "es-419"]
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"]).should eq ["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}).should eq "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
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-23 00:00:00.000000000 Z
11
+ date: 2015-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake