Accept_language 2.2 – RFC 7231/4647 compliant Accept-Language parsing for Ruby

https://news.ycombinator.com/rss Hits: 3
Summary

AcceptLanguage A lightweight, thread-safe Ruby library for parsing the Accept-Language HTTP header field. This implementation conforms to: Note RFC 7231 obsoletes RFC 2616 (the original HTTP/1.1 specification). The Accept-Language header behavior defined in RFC 2616 Section 14.4 remains unchanged in RFC 7231, ensuring full backward compatibility. Installation gem "accept_language" Usage AcceptLanguage . parse ( "da, en-GB;q=0.8, en;q=0.7" ) . match ( :en , :da ) # => :da Behavior Quality values Quality values (q-values) indicate relative preference, ranging from 0 (not acceptable) to 1 (most preferred). When omitted, the default is 1 . Per RFC 7231 Section 5.3.1, valid q-values have at most three decimal places: 0 , 0.7 , 0.85 , 1.000 . Invalid q-values cause the associated language range to be ignored. parser = AcceptLanguage . parse ( "da, en-GB;q=0.8, en;q=0.7" ) parser . match ( :en , :da ) # => :da (q=1 beats q=0.8) parser . match ( :en , :"en-GB" ) # => :"en-GB" (q=0.8 beats q=0.7) parser . match ( :ja ) # => nil (no match) Declaration order When multiple languages share the same q-value, declaration order in the header determines priority—the first declared language wins: AcceptLanguage . parse ( "en;q=0.8, fr;q=0.8" ) . match ( :en , :fr ) # => :en (declared first) AcceptLanguage . parse ( "fr;q=0.8, en;q=0.8" ) . match ( :en , :fr ) # => :fr (declared first) Basic Filtering This library implements the Basic Filtering matching scheme defined in RFC 4647 Section 3.3.1. 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 - . AcceptLanguage . parse ( "de-de" ) . match ( :"de-DE-1996" ) # => :"de-DE-1996" (prefix match) AcceptLanguage . parse ( "de-de" ) . match ( :"de-Deva" ) # => nil ("de-de" is not a prefix of "de-Deva") AcceptLanguage . parse ( "de-de" ) . match ( :"de-Latn-DE" ) # => nil ("de-de" is not a pre...

First seen: 2026-01-25 10:54

Last seen: 2026-01-25 12:54