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 +4 -4
- data/README.md +200 -70
- data/lib/accept_language/matcher.rb +81 -364
- data/lib/accept_language/parser.rb +83 -424
- data/lib/accept_language.rb +86 -248
- metadata +6 -5
data/lib/accept_language.rb
CHANGED
|
@@ -1,270 +1,108 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
# +Accept-Language+ HTTP header field
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
# == Purpose
|
|
10
|
-
#
|
|
11
|
-
# The +Accept-Language+ request-header field is sent by user agents to indicate
|
|
12
|
-
# the set of natural languages that are preferred as a response to the request.
|
|
13
|
-
# This library parses that header and matches the user's language preferences
|
|
14
|
-
# against your application's available languages, respecting quality values,
|
|
15
|
-
# wildcards, exclusions, and prefix matching rules defined by the HTTP/1.1
|
|
16
|
-
# specification.
|
|
17
|
-
#
|
|
18
|
-
# == Standards Compliance
|
|
19
|
-
#
|
|
20
|
-
# This implementation conforms to:
|
|
21
|
-
#
|
|
22
|
-
# - {RFC 2616 Section 14.4}[https://tools.ietf.org/html/rfc2616#section-14.4] -
|
|
23
|
-
# Accept-Language header field definition
|
|
24
|
-
# - {RFC 2616 Section 3.9}[https://tools.ietf.org/html/rfc2616#section-3.9] -
|
|
25
|
-
# Quality values (qvalues) syntax
|
|
26
|
-
# - {RFC 2616 Section 3.10}[https://tools.ietf.org/html/rfc2616#section-3.10] -
|
|
27
|
-
# Language tags reference
|
|
28
|
-
# - {BCP 47 / RFC 5646}[https://tools.ietf.org/html/bcp47] -
|
|
29
|
-
# Tags for Identifying Languages (modern standard for language tags)
|
|
30
|
-
#
|
|
31
|
-
# == Basic Usage
|
|
3
|
+
require_relative "accept_language/matcher"
|
|
4
|
+
require_relative "accept_language/parser"
|
|
5
|
+
|
|
6
|
+
# Negotiates a language from the +Accept-Language+ HTTP header field
|
|
7
|
+
# ({RFC 9110 Section 12.5.4}[https://www.rfc-editor.org/rfc/rfc9110#section-12.5.4]).
|
|
32
8
|
#
|
|
33
|
-
#
|
|
9
|
+
# {AcceptLanguage.negotiate} takes the value of the header and the languages
|
|
10
|
+
# your application supports, and returns the supported language the user
|
|
11
|
+
# prefers:
|
|
34
12
|
#
|
|
35
|
-
#
|
|
36
|
-
# AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7").match(:en, :da)
|
|
13
|
+
# AcceptLanguage.negotiate("da, en-GB;q=0.8, en;q=0.7", %i[en da])
|
|
37
14
|
# # => :da
|
|
38
15
|
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
# # => :"fr-CH"
|
|
42
|
-
#
|
|
43
|
-
# # When no match is found
|
|
44
|
-
# AcceptLanguage.parse("ja, zh;q=0.9").match(:en, :fr)
|
|
45
|
-
# # => nil
|
|
46
|
-
#
|
|
47
|
-
# == Quality Values
|
|
48
|
-
#
|
|
49
|
-
# Quality values (q-values) express relative preference, ranging from +0+
|
|
50
|
-
# (explicitly unacceptable) to +1+ (most preferred). When omitted, the
|
|
51
|
-
# default quality value is +1+.
|
|
52
|
-
#
|
|
53
|
-
# Per RFC 2616 Section 3.9, valid q-values have at most three decimal places.
|
|
54
|
-
# Invalid q-values cause the associated language tag to be ignored.
|
|
55
|
-
#
|
|
56
|
-
# parser = AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7")
|
|
57
|
-
#
|
|
58
|
-
# parser.match(:en, :da) # => :da (q=1 beats q=0.8)
|
|
59
|
-
# parser.match(:en, :"en-GB") # => :"en-GB" (q=0.8 beats q=0.7)
|
|
60
|
-
#
|
|
61
|
-
# == Prefix Matching
|
|
16
|
+
# AcceptLanguage.negotiate("da, en-GB;q=0.8, en;q=0.7", %w[en fr])
|
|
17
|
+
# # => "en"
|
|
62
18
|
#
|
|
63
|
-
#
|
|
64
|
-
# exactly equals the tag, or if it exactly equals a prefix of the tag such
|
|
65
|
-
# that the first character following the prefix is a hyphen (+"-"+).
|
|
66
|
-
#
|
|
67
|
-
# # "zh" matches "zh-TW" (prefix match)
|
|
68
|
-
# AcceptLanguage.parse("zh").match(:"zh-TW")
|
|
69
|
-
# # => :"zh-TW"
|
|
70
|
-
#
|
|
71
|
-
# # "zh-TW" does NOT match "zh" (more specific cannot match less specific)
|
|
72
|
-
# AcceptLanguage.parse("zh-TW").match(:zh)
|
|
73
|
-
# # => nil
|
|
74
|
-
#
|
|
75
|
-
# # Prefix matching respects hyphen boundaries
|
|
76
|
-
# AcceptLanguage.parse("zh").match(:zhx)
|
|
77
|
-
# # => nil (zhx is a different language code, not a subtag of zh)
|
|
78
|
-
#
|
|
79
|
-
# == Wildcards
|
|
80
|
-
#
|
|
81
|
-
# The wildcard character +*+ matches any language not explicitly matched by
|
|
82
|
-
# another language-range in the header:
|
|
83
|
-
#
|
|
84
|
-
# # Wildcard matches any language
|
|
85
|
-
# AcceptLanguage.parse("de, *;q=0.5").match(:ja)
|
|
86
|
-
# # => :ja
|
|
87
|
-
#
|
|
88
|
-
# # Explicit matches take precedence over wildcard
|
|
89
|
-
# AcceptLanguage.parse("de, *;q=0.5").match(:de, :ja)
|
|
90
|
-
# # => :de
|
|
91
|
-
#
|
|
92
|
-
# == Exclusions
|
|
93
|
-
#
|
|
94
|
-
# A quality value of +0+ explicitly marks a language as unacceptable:
|
|
95
|
-
#
|
|
96
|
-
# # English is excluded despite wildcard
|
|
97
|
-
# AcceptLanguage.parse("*, en;q=0").match(:en)
|
|
19
|
+
# AcceptLanguage.negotiate("ja, zh;q=0.9", %i[en fr])
|
|
98
20
|
# # => nil
|
|
99
21
|
#
|
|
100
|
-
#
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
#
|
|
104
|
-
#
|
|
105
|
-
#
|
|
106
|
-
#
|
|
107
|
-
#
|
|
108
|
-
#
|
|
109
|
-
#
|
|
110
|
-
#
|
|
111
|
-
#
|
|
112
|
-
#
|
|
113
|
-
#
|
|
114
|
-
#
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
#
|
|
118
|
-
#
|
|
119
|
-
#
|
|
120
|
-
#
|
|
121
|
-
#
|
|
122
|
-
#
|
|
123
|
-
#
|
|
124
|
-
#
|
|
125
|
-
#
|
|
126
|
-
#
|
|
127
|
-
#
|
|
128
|
-
#
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
#
|
|
132
|
-
#
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
#
|
|
137
|
-
#
|
|
138
|
-
#
|
|
139
|
-
#
|
|
140
|
-
#
|
|
141
|
-
#
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
145
|
-
#
|
|
146
|
-
#
|
|
147
|
-
#
|
|
148
|
-
# == Rack Integration Example
|
|
149
|
-
#
|
|
150
|
-
# # config.ru
|
|
151
|
-
# class LocaleMiddleware
|
|
152
|
-
# def initialize(app, available_locales:, default_locale:)
|
|
153
|
-
# @app = app
|
|
154
|
-
# @available_locales = available_locales
|
|
155
|
-
# @default_locale = default_locale
|
|
156
|
-
# end
|
|
157
|
-
#
|
|
158
|
-
# def call(env)
|
|
159
|
-
# locale = detect_locale(env) || @default_locale
|
|
160
|
-
# env["rack.locale"] = locale
|
|
161
|
-
# @app.call(env)
|
|
162
|
-
# end
|
|
163
|
-
#
|
|
164
|
-
# private
|
|
165
|
-
#
|
|
166
|
-
# def detect_locale(env)
|
|
167
|
-
# header = env["HTTP_ACCEPT_LANGUAGE"]
|
|
168
|
-
# AcceptLanguage.parse(header).match(*@available_locales)
|
|
169
|
-
# end
|
|
170
|
-
# end
|
|
171
|
-
#
|
|
172
|
-
# == Rails Integration Example
|
|
173
|
-
#
|
|
174
|
-
# # app/controllers/application_controller.rb
|
|
175
|
-
# class ApplicationController < ActionController::Base
|
|
176
|
-
# before_action :best_locale_from_request!
|
|
177
|
-
#
|
|
178
|
-
# def best_locale_from_request!
|
|
179
|
-
# I18n.locale = best_locale_from_request
|
|
180
|
-
# end
|
|
181
|
-
#
|
|
182
|
-
# def best_locale_from_request
|
|
183
|
-
# # HTTP_ACCEPT_LANGUAGE is the standardized key for the Accept-Language header in Rack/Rails
|
|
184
|
-
# return I18n.default_locale unless request.headers.key?("HTTP_ACCEPT_LANGUAGE")
|
|
185
|
-
#
|
|
186
|
-
# string = request.headers.fetch("HTTP_ACCEPT_LANGUAGE")
|
|
187
|
-
# locale = AcceptLanguage.parse(string).match(*I18n.available_locales)
|
|
188
|
-
#
|
|
189
|
-
# # If the server cannot serve any matching language,
|
|
190
|
-
# # it can theoretically send back a 406 (Not Acceptable) error code.
|
|
191
|
-
# # But, for a better user experience, this is rarely done and more
|
|
192
|
-
# # common way is to ignore the Accept-Language header in this case.
|
|
193
|
-
# return I18n.default_locale if locale.nil?
|
|
194
|
-
#
|
|
195
|
-
# locale
|
|
196
|
-
# end
|
|
197
|
-
# end
|
|
198
|
-
#
|
|
199
|
-
# @see Parser
|
|
200
|
-
# @see https://tools.ietf.org/html/rfc2616#section-14.4 RFC 2616 Section 14.4
|
|
201
|
-
# @see https://tools.ietf.org/html/bcp47 BCP 47
|
|
202
|
-
# @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language MDN Accept-Language
|
|
203
|
-
#
|
|
204
|
-
# @author Cyril Kato
|
|
205
|
-
# @since 1.0.0
|
|
22
|
+
# The library has no dependencies, keeps no state, and never turns a part of
|
|
23
|
+
# the header into a Symbol. It is safe to call from any thread.
|
|
24
|
+
#
|
|
25
|
+
# == Rules
|
|
26
|
+
#
|
|
27
|
+
# [Matching]
|
|
28
|
+
# A language range matches a tag it equals, or that it prefixes at a
|
|
29
|
+
# hyphen: +en+ matches +en+ and +en-US+, not +eng+ (Basic Filtering,
|
|
30
|
+
# {RFC 4647 Section 3.3.1}[https://www.rfc-editor.org/rfc/rfc4647#section-3.3.1]).
|
|
31
|
+
# The comparison ignores case. <tt>*</tt> matches every tag.
|
|
32
|
+
# [Quality]
|
|
33
|
+
# When several ranges match a tag, the longest one decides its quality
|
|
34
|
+
# value, so that a specific range overrides a general one. <tt>*</tt> is the
|
|
35
|
+
# shortest range.
|
|
36
|
+
# [Exclusion]
|
|
37
|
+
# A tag with a quality of +0+, or that no range matches, is never returned.
|
|
38
|
+
# [Selection]
|
|
39
|
+
# The tag with the highest quality wins. Ties go to the range declared
|
|
40
|
+
# first in the header, then to the tag listed first in the available
|
|
41
|
+
# languages.
|
|
42
|
+
#
|
|
43
|
+
# For example:
|
|
44
|
+
#
|
|
45
|
+
# AcceptLanguage.negotiate("en-US, en;q=0", %i[en-GB en-US])
|
|
46
|
+
# # => :"en-US"
|
|
47
|
+
#
|
|
48
|
+
# AcceptLanguage.negotiate("*, en;q=0", %i[en fr])
|
|
49
|
+
# # => :fr
|
|
50
|
+
#
|
|
51
|
+
# AcceptLanguage.negotiate("fr;q=0.8, en;q=0.8", %i[en fr])
|
|
52
|
+
# # => :fr
|
|
53
|
+
#
|
|
54
|
+
# == Robustness
|
|
55
|
+
#
|
|
56
|
+
# The header is sent by the client, so it is handled as untrusted bytes:
|
|
57
|
+
#
|
|
58
|
+
# - an element that does not follow the grammar (an invalid range, an invalid
|
|
59
|
+
# quality value, whitespace where none is allowed) is ignored, and the rest
|
|
60
|
+
# of the header is still used;
|
|
61
|
+
# - a header longer than 4096 bytes is treated as absent;
|
|
62
|
+
# - only the first 50 elements of a header are considered.
|
|
63
|
+
#
|
|
64
|
+
# The complete behavior, including repeated ranges and the exact tie-breaking
|
|
65
|
+
# rule, is defined in the
|
|
66
|
+
# {specification}[https://github.com/cyril/accept_language.rb/blob/main/SPECIFICATION.md]
|
|
67
|
+
# shared with the
|
|
68
|
+
# {Elixir implementation}[https://github.com/cyril/accept_language.ex].
|
|
206
69
|
module AcceptLanguage
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
#
|
|
210
|
-
#
|
|
211
|
-
# - Quality values (+q=0+ to +q=1+, default +1+ when omitted)
|
|
212
|
-
# - Language tag validation per BCP 47
|
|
213
|
-
# - Wildcards (+*+)
|
|
214
|
-
# - Case normalization (matching is case-insensitive)
|
|
70
|
+
private_constant :Matcher, :Parser
|
|
71
|
+
|
|
72
|
+
# Returns the element of +available_langtags+ that best matches the
|
|
73
|
+
# +Accept-Language+ header value +field+, or +nil+ when none is acceptable.
|
|
215
74
|
#
|
|
216
|
-
#
|
|
217
|
-
#
|
|
218
|
-
# that may not strictly conform to specifications.
|
|
75
|
+
# The arguments are checked on every call, including when +field+ is +nil+,
|
|
76
|
+
# so that a misuse shows up on the first request.
|
|
219
77
|
#
|
|
220
|
-
# @param field [String, nil] the
|
|
221
|
-
#
|
|
222
|
-
#
|
|
223
|
-
#
|
|
224
|
-
# resulting in a parser that matches no languages.
|
|
78
|
+
# @param field [String, nil] the value of the header, or +nil+ when the
|
|
79
|
+
# request has none. With Rack, that is <tt>env["HTTP_ACCEPT_LANGUAGE"]</tt>.
|
|
80
|
+
# @param available_langtags [Array<Symbol, String>] the languages the
|
|
81
|
+
# application supports, such as +I18n.available_locales+
|
|
225
82
|
#
|
|
226
|
-
# @return [
|
|
227
|
-
#
|
|
228
|
-
# matching language from your available options.
|
|
83
|
+
# @return [Symbol, String, nil] one of the elements of +available_langtags+,
|
|
84
|
+
# exactly as given, or +nil+
|
|
229
85
|
#
|
|
230
86
|
# @raise [TypeError] if +field+ is neither a String nor +nil+
|
|
87
|
+
# @raise [TypeError] if +available_langtags+ is not an Array of Symbols or
|
|
88
|
+
# Strings
|
|
231
89
|
#
|
|
232
|
-
# @example
|
|
233
|
-
#
|
|
234
|
-
# parser.match(:en, :"en-GB", :fr)
|
|
90
|
+
# @example The result keeps the case of the available tag
|
|
91
|
+
# AcceptLanguage.negotiate("EN-gb", %i[en-GB fr])
|
|
235
92
|
# # => :"en-GB"
|
|
236
93
|
#
|
|
237
|
-
# @example
|
|
238
|
-
# AcceptLanguage.
|
|
239
|
-
# # =>
|
|
240
|
-
#
|
|
241
|
-
# AcceptLanguage.parse(nil).match(:en, :fr)
|
|
242
|
-
# # => nil
|
|
243
|
-
#
|
|
244
|
-
# @example Reusing a parser instance
|
|
245
|
-
# # Parse once, match multiple times
|
|
246
|
-
# user_prefs = AcceptLanguage.parse(header_value)
|
|
94
|
+
# @example A specific range is preferred to a general one
|
|
95
|
+
# AcceptLanguage.negotiate("fr-CH, fr;q=0.9", %i[fr fr-CH])
|
|
96
|
+
# # => :"fr-CH"
|
|
247
97
|
#
|
|
248
|
-
#
|
|
249
|
-
#
|
|
98
|
+
# @example The wildcard matches any other language
|
|
99
|
+
# AcceptLanguage.negotiate("de, *;q=0.5", %i[ja])
|
|
100
|
+
# # => :ja
|
|
250
101
|
#
|
|
251
|
-
#
|
|
252
|
-
#
|
|
253
|
-
#
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
# AcceptLanguage.parse("en;q=2.0, fr;q=0.8").match(:en, :fr)
|
|
257
|
-
# # => :fr (en is ignored due to invalid q-value > 1)
|
|
258
|
-
#
|
|
259
|
-
# # Invalid language tags are ignored
|
|
260
|
-
# AcceptLanguage.parse("123invalid, fr;q=0.8").match(:fr)
|
|
261
|
-
# # => :fr
|
|
262
|
-
#
|
|
263
|
-
# @see Parser#match
|
|
264
|
-
# @see https://tools.ietf.org/html/rfc2616#section-14.4 RFC 2616 Section 14.4
|
|
265
|
-
def self.parse(field)
|
|
266
|
-
Parser.new(field)
|
|
102
|
+
# @example Falling back to a default language
|
|
103
|
+
# AcceptLanguage.negotiate(nil, %i[en fr]) || :en
|
|
104
|
+
# # => :en
|
|
105
|
+
def self.negotiate(field, available_langtags)
|
|
106
|
+
Matcher.call(Parser.parse(field), available_langtags)
|
|
267
107
|
end
|
|
268
108
|
end
|
|
269
|
-
|
|
270
|
-
require_relative File.join("accept_language", "parser")
|
metadata
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: accept_language
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Cyril Kato
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A lightweight, thread-safe Ruby library for parsing the Accept-Language
|
|
14
|
-
HTTP header as defined in RFC
|
|
14
|
+
HTTP header as defined in RFC 9110, with full support for BCP 47 language tags.
|
|
15
15
|
email: contact@cyril.email
|
|
16
16
|
executables: []
|
|
17
17
|
extensions: []
|
|
@@ -30,6 +30,7 @@ metadata:
|
|
|
30
30
|
source_code_uri: https://github.com/cyril/accept_language.rb
|
|
31
31
|
documentation_uri: https://rubydoc.info/github/cyril/accept_language.rb/main
|
|
32
32
|
bug_tracker_uri: https://github.com/cyril/accept_language.rb/issues
|
|
33
|
+
changelog_uri: https://github.com/cyril/accept_language.rb/blob/main/CHANGELOG.md
|
|
33
34
|
wiki_uri: https://github.com/cyril/accept_language.rb/wiki
|
|
34
35
|
post_install_message:
|
|
35
36
|
rdoc_options: []
|
|
@@ -39,14 +40,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
39
40
|
requirements:
|
|
40
41
|
- - ">="
|
|
41
42
|
- !ruby/object:Gem::Version
|
|
42
|
-
version: 3.
|
|
43
|
+
version: 3.3.0
|
|
43
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
45
|
requirements:
|
|
45
46
|
- - ">="
|
|
46
47
|
- !ruby/object:Gem::Version
|
|
47
48
|
version: '0'
|
|
48
49
|
requirements: []
|
|
49
|
-
rubygems_version: 3.
|
|
50
|
+
rubygems_version: 3.5.22
|
|
50
51
|
signing_key:
|
|
51
52
|
specification_version: 4
|
|
52
53
|
summary: Parser for Accept-Language request HTTP header
|