accept_language 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +1 -1
- data/Gemfile.lock +2 -2
- data/README.md +30 -2
- data/VERSION.semver +1 -1
- data/lib/accept_language/parser.rb +1 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af6ad773549b67a530133a79fd84c7b6de133ab571cb14642ce261eb0f9255d4
|
4
|
+
data.tar.gz: c5a11c41e67023583c9b3ece32babeee369760d6d8b5f0fb2890f4dc055da5dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f35d229b0ab907ebcbeba66f6163c7f66ef7567cca0795bb669487713ed0eb9bcf1caff663ae953ef070eca4f0a2f932ffb72a5ed4349f876ed871df3649dcae
|
7
|
+
data.tar.gz: a05701325ee77c8aab3ce9057bd26bff5e75564e937020a7694306e81257a72ffbae7530dae570027dcff72afa8f8c2af31acc29ab50005638ecaf9f04f22af6
|
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2019-06-12
|
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
|
data/Gemfile.lock
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
accept_language (0.1.
|
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.
|
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
|
-
|
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
|
-
|
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.
|
data/VERSION.semver
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
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
|