accept_language 2.2.0 → 3.0.0

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: a993b9e4d4792701b09a650afb27011ff9a94ba104362a8c542d01ee389ca5e9
4
- data.tar.gz: 129990017c1827e87e95847d8f8f42fb8c85b2d5b8146da5e6aeecb6ac7853ea
3
+ metadata.gz: 19552c2e3b6929343c09e75698eeba19519dc01ceeeccb61772dd2c4d0a7c035
4
+ data.tar.gz: 2c49770f88fc6062d35ec8a628315e88b689106fcfd78022196b8ba3c773840e
5
5
  SHA512:
6
- metadata.gz: 2cf1e95c98cf16c78b33f7db1e666ce834f62d436a1fc84ffee28df36dfbe41a1595710cc539e7cdbc63431c13d51b55065fdcf037a01114cc639451de33498d
7
- data.tar.gz: 63c161793225af35b1c5f73364dee830ba1429a2cc02e431c3422da2cf9e60edb76b9f601535f9cdb3afb982ff9d28b5f3c148a2fc8bad8320deba65115b1d23
6
+ metadata.gz: 8b6f799e3e6712c01254f1e54d6deb361f0eeeb93a62c4873e13ae290a4c04420d6a35ddcb0e14ea16997fbb64b92a450796c51de900ab1fbbce08ec3d6f6716
7
+ data.tar.gz: cc26685a4b373d06ed53314916be9fac233cec1eeab497c6d69142711abff9573e69c65c2983c27267ff680985f63fad4fe35de555644e19b1762841287cef5b
data/README.md CHANGED
@@ -1,130 +1,224 @@
1
1
  # AcceptLanguage
2
2
 
3
- A lightweight, thread-safe Ruby library for parsing the `Accept-Language` HTTP header as defined in [RFC 2616](https://tools.ietf.org/html/rfc2616#section-14.4), with full support for [BCP 47](https://tools.ietf.org/html/bcp47) language tags.
4
-
5
3
  [![Version](https://img.shields.io/github/v/tag/cyril/accept_language.rb?label=Version&logo=github)](https://github.com/cyril/accept_language.rb/tags)
6
4
  [![Yard documentation](https://img.shields.io/badge/Yard-documentation-blue.svg?logo=github)](https://rubydoc.info/github/cyril/accept_language.rb/main)
7
5
  ![Ruby](https://github.com/cyril/accept_language.rb/actions/workflows/ruby.yml/badge.svg?branch=main)
8
6
  ![RuboCop](https://github.com/cyril/accept_language.rb/actions/workflows/rubocop.yml/badge.svg?branch=main)
9
7
  [![License](https://img.shields.io/github/license/cyril/accept_language.rb?label=License&logo=github)](https://github.com/cyril/accept_language.rb/raw/main/LICENSE.md)
10
8
 
9
+ A lightweight, thread-safe Ruby library for parsing the `Accept-Language` HTTP header field.
10
+
11
+ This implementation conforms to:
12
+
13
+ - [RFC 9110 Section 12.5.4](https://www.rfc-editor.org/rfc/rfc9110#section-12.5.4) — Accept-Language header field definition
14
+ - [RFC 9110 Section 12.4.2](https://www.rfc-editor.org/rfc/rfc9110#section-12.4.2) — Quality values syntax
15
+ - [RFC 4647 Section 3.3.1](https://www.rfc-editor.org/rfc/rfc4647#section-3.3.1) — Basic Filtering matching scheme
16
+ - [BCP 47](https://www.rfc-editor.org/info/bcp47) — Tags for Identifying Languages
17
+
18
+ Its behavior is defined by a [specification](SPECIFICATION.md) and a set of test vectors shared with the [Elixir implementation](https://github.com/cyril/accept_language.ex): both return the same result for the same input.
19
+
11
20
  ## Installation
12
21
 
13
22
  ```ruby
14
- gem "accept_language"
23
+ gem "accept_language", "~> 3.0"
15
24
  ```
16
25
 
26
+ Ruby 3.3 or later is required.
27
+
28
+ Version 3 replaces `AcceptLanguage.parse(header).match(*locales)` with `AcceptLanguage.negotiate(header, locales)`. See [Upgrading from 2.x](#upgrading-from-2x).
29
+
17
30
  ## Usage
18
31
 
32
+ `AcceptLanguage.negotiate` takes the value of the header and the languages your application supports. It returns the supported language the user prefers, or `nil` when none is acceptable:
33
+
19
34
  ```ruby
20
- AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7").match(:en, :da)
35
+ AcceptLanguage.negotiate("da, en-GB;q=0.8, en;q=0.7", %i[en da])
21
36
  # => :da
37
+
38
+ AcceptLanguage.negotiate("da, en-GB;q=0.8, en;q=0.7", %i[fr de])
39
+ # => nil
40
+
41
+ AcceptLanguage.negotiate(nil, %i[en da]) || :en
42
+ # => :en (no header: fall back to a default)
22
43
  ```
23
44
 
45
+ The supported languages are an Array of Symbols or Strings, such as `I18n.available_locales`. The result is one of them, exactly as given:
46
+
47
+ ```ruby
48
+ AcceptLanguage.negotiate("da, en-GB;q=0.8, en;q=0.7", %w[en da])
49
+ # => "da"
50
+ ```
51
+
52
+ The library keeps no state and is safe to call from any thread. No part of the header is ever turned into a Symbol.
53
+
24
54
  ## Behavior
25
55
 
26
56
  ### Quality values
27
57
 
28
- Quality values (q-values) express relative preference, ranging from `0` (unacceptable) to `1` (most preferred). When omitted, the default is `1`.
58
+ Quality values (q-values) indicate relative preference, ranging from `0` (not acceptable) to `1` (most preferred). When omitted, the default is `1`.
29
59
 
30
60
  ```ruby
31
- parser = AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7")
61
+ AcceptLanguage.negotiate("da, en-GB;q=0.8, en;q=0.7", %i[en da])
62
+ # => :da (q=1 beats q=0.8)
32
63
 
33
- parser.match(:en, :da) # => :da (q=1 > q=0.8)
34
- parser.match(:en, :"en-GB") # => :"en-GB" (q=0.8 > q=0.7)
35
- parser.match(:ja) # => nil (no match)
64
+ AcceptLanguage.negotiate("da, en-GB;q=0.8, en;q=0.7", %i[en en-GB])
65
+ # => :"en-GB" (q=0.8 beats q=0.7)
36
66
  ```
37
67
 
38
- Per RFC 2616 Section 3.9, valid q-values have at most three decimal places: `0`, `0.7`, `0.85`, `1.000`. Invalid q-values are ignored.
68
+ Per RFC 9110 Section 12.4.2, a q-value has at most three decimal places: `0`, `0.7`, `0.85`, `1.000`. A trailing dot without digits (`0.`, `1.`) is allowed by the grammar and accepted.
39
69
 
40
- ### Identical quality values
70
+ ### Basic Filtering
41
71
 
42
- When multiple languages share the same q-value, the order of declaration in the header determines priority—the first declared language is preferred:
72
+ A language range matches a language tag if, in a case-insensitive comparison, it exactly equals the tag, or if it exactly equals a prefix of the tag such that the first character following the prefix is `-` (RFC 4647 Section 3.3.1).
43
73
 
44
74
  ```ruby
45
- AcceptLanguage.parse("en;q=0.8, fr;q=0.8").match(:en, :fr)
46
- # => :en (declared first)
75
+ AcceptLanguage.negotiate("zh", %i[zh-TW])
76
+ # => :"zh-TW" ("zh" matches "zh-TW")
47
77
 
48
- AcceptLanguage.parse("fr;q=0.8, en;q=0.8").match(:en, :fr)
49
- # => :fr (declared first)
78
+ AcceptLanguage.negotiate("zh", %i[zhx])
79
+ # => nil ("zh" does not match "zhx", a different language)
80
+
81
+ AcceptLanguage.negotiate("zh-TW", %i[zh])
82
+ # => nil (a specific range does not match a less specific tag)
83
+
84
+ AcceptLanguage.negotiate("de-de", %i[de-DE-1996])
85
+ # => :"de-DE-1996"
86
+
87
+ AcceptLanguage.negotiate("de-de", %i[de-Latn-DE])
88
+ # => nil ("de-de" is not a prefix of "de-Latn-DE")
50
89
  ```
51
90
 
52
- ### Prefix matching
91
+ ### Longest match
53
92
 
54
- Per RFC 2616 Section 14.4, a language-range matches any language-tag that exactly equals the range or begins with the range followed by `-`:
93
+ When several ranges match the same tag, the longest one decides the tag's q-value, so that a specific range overrides a general one.
55
94
 
56
95
  ```ruby
57
- AcceptLanguage.parse("zh").match(:"zh-TW")
58
- # => :"zh-TW" ("zh" matches "zh-TW")
96
+ AcceptLanguage.negotiate("en-US, en;q=0", %i[en-GB en-US])
97
+ # => :"en-US" ("en-US" takes q=1 from its own range, "en-GB" takes q=0 from "en")
59
98
 
60
- AcceptLanguage.parse("zh-TW").match(:zh)
61
- # => nil ("zh-TW" does not match "zh")
99
+ AcceptLanguage.negotiate("en;q=0.9, en-US;q=0.5", %i[en-US en-GB])
100
+ # => :"en-GB" ("en-US" takes q=0.5 from its own range, "en-GB" takes q=0.9 from "en")
62
101
  ```
63
102
 
64
- Note that prefix matching follows hyphen boundaries—`zh` does not match `zhx`:
103
+ This is the rule RFC 2616 Section 14.4 spelled out ("the quality value of the longest language-range in the field that matches the language-tag"). Later revisions of HTTP dropped the sentence without replacing it.
104
+
105
+ ### Wildcards
106
+
107
+ The wildcard `*` matches every tag and counts as the shortest range: it only speaks for the tags no other range matches.
65
108
 
66
109
  ```ruby
67
- AcceptLanguage.parse("zh").match(:zhx)
68
- # => nil ("zhx" is a different language code)
110
+ AcceptLanguage.negotiate("de, *;q=0.5", %i[ja])
111
+ # => :ja (matched by the wildcard)
112
+
113
+ AcceptLanguage.negotiate("de, *;q=0.5", %i[ja de])
114
+ # => :de ("de" has q=1, "ja" has q=0.5)
69
115
  ```
70
116
 
71
- ### Wildcards
117
+ ### Exclusions
72
118
 
73
- The wildcard `*` matches any language not matched by another range:
119
+ A q-value of `0` marks a language as not acceptable. It applies to the tags the range matches, unless a longer range claims them (see [Longest match](#longest-match)).
74
120
 
75
121
  ```ruby
76
- AcceptLanguage.parse("de, *;q=0.5").match(:ja)
77
- # => :ja (matched by wildcard)
122
+ AcceptLanguage.negotiate("*, en;q=0", %i[en en-GB])
123
+ # => nil (English is excluded, with its variants)
78
124
 
79
- AcceptLanguage.parse("de, *;q=0.5").match(:de, :ja)
80
- # => :de (explicit match preferred over wildcard)
125
+ AcceptLanguage.negotiate("*, en;q=0", %i[en ja])
126
+ # => :ja
127
+
128
+ AcceptLanguage.negotiate("*, en;q=0, en-GB", %i[en en-GB])
129
+ # => :"en-GB" ("en-GB" is the longest match and takes q=1)
81
130
  ```
82
131
 
83
- ### Exclusions
132
+ ### Ties
84
133
 
85
- A q-value of `0` explicitly excludes a language:
134
+ Among acceptable tags, the highest q-value wins. When several tags share it, the one whose range comes first in the header wins:
86
135
 
87
136
  ```ruby
88
- AcceptLanguage.parse("*, en;q=0").match(:en)
89
- # => nil (English excluded)
137
+ AcceptLanguage.negotiate("en;q=0.8, fr;q=0.8", %i[fr en])
138
+ # => :en (declared first)
90
139
 
91
- AcceptLanguage.parse("*, en;q=0").match(:ja)
92
- # => :ja (matched by wildcard)
140
+ AcceptLanguage.negotiate("fr;q=0.8, en;q=0.8", %i[en fr])
141
+ # => :fr (declared first)
93
142
  ```
94
143
 
95
- Exclusions apply to prefix matches:
144
+ A tag is ranked by the earliest range that matches it with its q-value, so adding a more specific range for a language the header already prefers never demotes that language. If a tie remains, the order of the available languages decides:
96
145
 
97
146
  ```ruby
98
- AcceptLanguage.parse("*, en;q=0").match(:"en-GB")
99
- # => nil (en-GB excluded via "en" prefix)
147
+ AcceptLanguage.negotiate("en, fr, en-US", %i[fr en-US])
148
+ # => :"en-US" (q=1 was stated for "en-US" by "en", first in the header)
149
+
150
+ AcceptLanguage.negotiate("en", %i[en-GB en-US])
151
+ # => :"en-GB" (same range, same q-value: first available)
100
152
  ```
101
153
 
102
154
  ### Case insensitivity
103
155
 
104
- Matching is case-insensitive per RFC 2616, but the original case of available language tags is preserved:
156
+ Matching ignores case, and the result keeps the case of the available tag:
105
157
 
106
158
  ```ruby
107
- AcceptLanguage.parse("EN-GB").match(:"en-gb")
159
+ AcceptLanguage.negotiate("EN-GB", %i[en-gb])
108
160
  # => :"en-gb"
109
161
 
110
- AcceptLanguage.parse("en-gb").match(:"EN-GB")
111
- # => :"EN-GB"
162
+ AcceptLanguage.negotiate("en-gb", %w[EN-GB])
163
+ # => "EN-GB"
112
164
  ```
113
165
 
166
+ ### Repeated ranges
167
+
168
+ HTTP gives no meaning to a range listed more than once. A single occurrence is retained: an exclusion (`q=0`) if there is one, otherwise the highest q-value.
169
+
170
+ ```ruby
171
+ AcceptLanguage.negotiate("en;q=0, fr, en", %i[en fr])
172
+ # => :fr (the exclusion of "en" wins)
173
+
174
+ AcceptLanguage.negotiate("en;q=0.5, fr;q=0.8, en;q=0.9", %i[en fr])
175
+ # => :en ("en" is considered at q=0.9)
176
+
177
+ AcceptLanguage.negotiate("*, *;q=0", %i[fr])
178
+ # => nil
179
+ ```
180
+
181
+ ### Malformed elements
182
+
183
+ The header comes from the client and is handled as untrusted bytes, whatever the encoding of the String. An element that does not follow the grammar is ignored, and the rest of the header is still used. This is the case when:
184
+
185
+ - its language range is not a valid basic language range (`en_US`, `123`, `toolongprimary`, any non-ASCII character)
186
+ - its q-value is out of range, has more than three decimals, or is abbreviated (`1.5`, `0.1234`, `.5`)
187
+ - its q-value is empty (`en;q=`), or it carries anything but a single weight (`en;q=0.8;q=0.1`, `en;level=1`)
188
+ - it contains whitespace where the grammar allows none (`e n`, `en;q= 0.8`); spaces and tabs are only allowed around `,` and `;`
189
+
190
+ ```ruby
191
+ AcceptLanguage.negotiate("en;q=, fr;q=0.5", %i[en fr])
192
+ # => :fr ("en;q=" is ignored; it does not default to q=1)
193
+ ```
194
+
195
+ ### Limits
196
+
197
+ Two limits bound the work done for a single header. They are far above legitimate usage (browsers send 2 to 10 ranges in under 200 bytes):
198
+
199
+ - a header longer than 4096 bytes is treated as absent, and `negotiate` returns `nil`
200
+ - only the first 50 elements of a header are considered
201
+
114
202
  ### BCP 47 language tags
115
203
 
116
- Full support for [BCP 47](https://tools.ietf.org/html/bcp47) language tags:
204
+ Script, region and variant subtags are supported:
117
205
 
118
206
  ```ruby
119
207
  # Script subtags
120
- AcceptLanguage.parse("zh-Hant").match(:"zh-Hant-TW", :"zh-Hans-CN")
208
+ AcceptLanguage.negotiate("zh-Hant, zh-Hans;q=0.9", %i[zh-Hans-CN zh-Hant-TW])
121
209
  # => :"zh-Hant-TW"
122
210
 
123
211
  # Variant subtags
124
- AcceptLanguage.parse("de-1996, de;q=0.9").match(:"de-CH-1996", :"de-CH")
212
+ AcceptLanguage.negotiate("de-CH-1996, de-CH;q=0.9", %i[de-CH de-CH-1996])
125
213
  # => :"de-CH-1996"
126
214
  ```
127
215
 
216
+ The available tags are not validated: they are compared as they are. A tag such as `:pt_BR`, which no language range can equal or prefix, can only be matched by `*`.
217
+
218
+ ### Errors
219
+
220
+ The arguments are checked on every call, including when the header is `nil`, so that a misuse shows up on the first request. `negotiate` raises `TypeError` when the header is neither a String nor `nil`, or when the available languages are not an Array of Symbols or Strings.
221
+
128
222
  ## Integration examples
129
223
 
130
224
  ### Rack
@@ -139,18 +233,9 @@ class LocaleMiddleware
139
233
  end
140
234
 
141
235
  def call(env)
142
- locale = detect_locale(env) || @default_locale
143
- env["rack.locale"] = locale
144
- @app.call(env)
145
- end
146
-
147
- private
148
-
149
- def detect_locale(env)
150
236
  header = env["HTTP_ACCEPT_LANGUAGE"]
151
- return unless header
152
-
153
- AcceptLanguage.parse(header).match(*@available_locales)
237
+ env["rack.locale"] = AcceptLanguage.negotiate(header, @available_locales) || @default_locale
238
+ @app.call(env)
154
239
  end
155
240
  end
156
241
  ```
@@ -167,30 +252,75 @@ class ApplicationController < ActionController::Base
167
252
  end
168
253
 
169
254
  def best_locale_from_request
170
- # HTTP_ACCEPT_LANGUAGE is the standardized key for the Accept-Language header in Rack/Rails
171
- return I18n.default_locale unless request.headers.key?("HTTP_ACCEPT_LANGUAGE")
172
-
173
- string = request.headers.fetch("HTTP_ACCEPT_LANGUAGE")
174
- locale = AcceptLanguage.parse(string).match(*I18n.available_locales)
255
+ # HTTP_ACCEPT_LANGUAGE is the standardized key for the Accept-Language header in Rack/Rails.
256
+ # Its value is nil when the request has no such header.
257
+ header = request.headers["HTTP_ACCEPT_LANGUAGE"]
175
258
 
176
259
  # If the server cannot serve any matching language,
177
260
  # it can theoretically send back a 406 (Not Acceptable) error code.
178
261
  # But, for a better user experience, this is rarely done and more
179
262
  # common way is to ignore the Accept-Language header in this case.
180
- return I18n.default_locale if locale.nil?
181
-
182
- locale
263
+ AcceptLanguage.negotiate(header, I18n.available_locales) || I18n.default_locale
183
264
  end
184
265
  end
185
266
  ```
186
267
 
268
+ ## Upgrading from 2.x
269
+
270
+ Replace the two-step call with a single one, and pass the available languages as an Array:
271
+
272
+ ```ruby
273
+ # 2.x
274
+ AcceptLanguage.parse(header).match(*I18n.available_locales)
275
+
276
+ # 3.x
277
+ AcceptLanguage.negotiate(header, I18n.available_locales)
278
+ ```
279
+
280
+ `AcceptLanguage.parse`, `AcceptLanguage::Parser` and `AcceptLanguage::Matcher` are no longer part of the public interface.
281
+
282
+ A few results change, all of them for headers that browsers do not send by default. The [changelog](CHANGELOG.md) lists them; the main ones are:
283
+
284
+ - a specific range now overrides a general one: `en-US, en;q=0` accepts `en-US` (it accepted no English at all)
285
+ - `en;q=` is ignored (it was read as `en`), and so is `e n` (it was read as `en`)
286
+ - a tab is accepted wherever a space is (the element next to it was ignored)
287
+ - a header longer than 4096 bytes is treated as absent, and elements after the 50th are ignored
288
+
289
+ ## Standards compliance
290
+
291
+ ### Supported specifications
292
+
293
+ | Specification | Description | Status |
294
+ |---------------|-------------|--------|
295
+ | RFC 9110 §12.5.4 | Accept-Language header field | ✅ Supported |
296
+ | RFC 9110 §12.4.2 | Quality values (qvalues) | ✅ Supported |
297
+ | RFC 9110 §5.6.3 | Optional whitespace (OWS) | ✅ Supported |
298
+ | RFC 4647 §2.1 | Basic Language Range syntax | ✅ Supported |
299
+ | RFC 4647 §3.3.1 | Basic Filtering scheme | ✅ Supported |
300
+ | RFC 2616 §14.4 | Longest matching range decides a tag's q-value | ✅ Followed (the rule was dropped from later HTTP revisions without replacement) |
301
+ | BCP 47 | Language tag structure | ✅ Supported |
302
+
303
+ RFC 9110 obsoletes [RFC 7231](https://www.rfc-editor.org/rfc/rfc7231), which itself obsoleted [RFC 2616](https://www.rfc-editor.org/rfc/rfc2616). The header syntax is unchanged across these revisions.
304
+
305
+ ### Not implemented
306
+
307
+ | Specification | Description | Reason |
308
+ |---------------|-------------|--------|
309
+ | RFC 4647 §2.2 | Extended Language Range | Not used by HTTP |
310
+ | RFC 4647 §3.3.2 | Extended Filtering | Not used by HTTP |
311
+ | RFC 4647 §3.4 | Lookup scheme | Design choice — Basic Filtering is the scheme HTTP has always described |
312
+
187
313
  ## Documentation
188
314
 
189
315
  - [API documentation](https://rubydoc.info/github/cyril/accept_language.rb/main)
190
- - [RFC 2616 Section 14.4](https://tools.ietf.org/html/rfc2616#section-14.4)
191
- - [BCP 47](https://tools.ietf.org/html/bcp47)
192
- - [Language negotiation with Ruby](https://dev.to/cyri_/language-negotiation-with-ruby-5166)
193
- - [Rubyで言語ネゴシエーション](https://qiita.com/cyril/items/45dc233edb7be9d614e7)
316
+ - [Specification](SPECIFICATION.md) and [test vectors](spec/vectors.tsv) shared with the Elixir implementation
317
+ - [RFC 9110 — HTTP Semantics](https://www.rfc-editor.org/rfc/rfc9110)
318
+ - [RFC 4647 — Matching of Language Tags](https://www.rfc-editor.org/rfc/rfc4647)
319
+ - [BCP 47 — Tags for Identifying Languages](https://www.rfc-editor.org/info/bcp47)
320
+
321
+ ## See also
322
+
323
+ - [accept_language.ex](https://github.com/cyril/accept_language.ex) — Elixir equivalent of this library
194
324
 
195
325
  ## Versioning
196
326