accept_language 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d350dc26f69d89636ac4794c3a9d678cdf44f3f849c1f640b38c959da3637f9
4
- data.tar.gz: ce3bd0c7254ebc9d3dbad9ca6d7503fd6c2dcf396232c0641a8de56872bd97fa
3
+ metadata.gz: af6ad773549b67a530133a79fd84c7b6de133ab571cb14642ce261eb0f9255d4
4
+ data.tar.gz: c5a11c41e67023583c9b3ece32babeee369760d6d8b5f0fb2890f4dc055da5dc
5
5
  SHA512:
6
- metadata.gz: 8a2c7f1ca2b56dc774aa2c62dc1ab58cf0ed25dc2dd02e55f9a5c7fafe162480855068222a1cc73aeeefe4ec594abb72bebdcf7adbf179b786015837ca9abb27
7
- data.tar.gz: 177e781389b91cf7e9a2bc572e318abef16fd4d6267fb83a61cc2d0f6609106b39a56f85ea0f8a5d6070fb924c49d69c0262e828b983b275fc069a725ba1885d
6
+ metadata.gz: f35d229b0ab907ebcbeba66f6163c7f66ef7567cca0795bb669487713ed0eb9bcf1caff663ae953ef070eca4f0a2f932ffb72a5ed4349f876ed871df3649dcae
7
+ data.tar.gz: a05701325ee77c8aab3ce9057bd26bff5e75564e937020a7694306e81257a72ffbae7530dae570027dcff72afa8f8c2af31acc29ab50005638ecaf9f04f22af6
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-06-12 08:15:50 +0200 using RuboCop version 0.71.0.
3
+ # on 2019-06-12 23:02:50 +0200 using RuboCop version 0.71.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- accept_language (0.1.2)
4
+ accept_language (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.0)
10
10
  diff-lcs (1.3)
11
- docile (1.3.1)
11
+ docile (1.3.2)
12
12
  jaro_winkler (1.5.2)
13
13
  json (2.2.0)
14
14
  parallel (1.17.0)
data/README.md CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
 
29
29
  It's intended to be used in a Web server that supports some level of internationalization (i18n), but can be used anytime an `Accept-Language` header string is available.
30
30
 
31
- ### Examples
31
+ Examples:
32
32
 
33
33
  ```ruby
34
34
  AcceptLanguage.parse('da, en-gb;q=0.8, en;q=0.7') # => [:da, :"en-gb", :en]
@@ -37,7 +37,7 @@ AcceptLanguage.parse('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5') # => [:"fr-
37
37
 
38
38
  In order to help facilitate better i18n, a method is provided to return the intersection of the languages the user prefers and the languages your application supports.
39
39
 
40
- ### Examples
40
+ Examples:
41
41
 
42
42
  ```ruby
43
43
  AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :ar, :da, :ja, :ro) # => :da
@@ -50,6 +50,34 @@ AcceptLanguage.intersection('*;q=0.5, zh;q=0.4', :ja, :zh) # => :ja
50
50
  AcceptLanguage.intersection('fr;q=0, zh;q=0.4', :fr) # => nil
51
51
  ```
52
52
 
53
+ ### Rails integration example
54
+
55
+ ```ruby
56
+ # app/controllers/application_controller.rb
57
+ class ApplicationController < ActionController::Base
58
+ before_action :best_locale_from_request!
59
+
60
+ def best_locale_from_request!
61
+ I18n.locale = best_locale_from_request
62
+ end
63
+
64
+ def best_locale_from_request
65
+ return I18n.default_locale unless request.headers.key?('HTTP_ACCEPT_LANGUAGE')
66
+
67
+ string = request.headers.fetch('HTTP_ACCEPT_LANGUAGE')
68
+ locale = AcceptLanguage.intersection(string, I18n.default_locale, *I18n.available_locales)
69
+
70
+ # If the server cannot serve any matching language,
71
+ # it can theoretically send back a 406 (Not Acceptable) error code.
72
+ # But, for a better user experience, this is rarely done and more
73
+ # common way is to ignore the Accept-Language header in this case.
74
+ return I18n.default_locale if locale.nil?
75
+
76
+ locale
77
+ end
78
+ end
79
+ ```
80
+
53
81
  ## Contributing
54
82
 
55
83
  Bug reports and pull requests are welcome on GitHub at https://github.com/cyril/accept_language.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -21,13 +21,10 @@ module AcceptLanguage
21
21
  def preferences
22
22
  @string.delete(' ').split(',').inject({}) do |result, language|
23
23
  tag, quality = language.split(/;q=/i)
24
-
25
- tag = tag.downcase
26
24
  quality = quality.nil? ? 1.0 : quality.to_f
27
-
28
25
  next result if quality.zero?
29
26
 
30
- result.merge(tag.to_sym => quality)
27
+ result.merge(tag.downcase.to_sym => quality)
31
28
  end
32
29
  end
33
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: accept_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Kato