-
Notifications
You must be signed in to change notification settings - Fork 7
Home
Welcome to the AcceptLanguage wiki! This gem helps you parse the Accept-Language HTTP header and match user language preferences against your application's available locales.
When a browser sends a request to your server, it includes an Accept-Language header that looks something like this:
Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5
This header tells your application: "I prefer Swiss French, then French, then English, then German, and I'll accept anything else as a last resort."
Parsing this header correctly—respecting quality values, wildcards, and prefix matching rules—is exactly what this gem does.
require "accept_language"
# Parse the header and find the best match
AcceptLanguage.parse("fr-CH, fr;q=0.9, en;q=0.8").match(:en, :fr, :"fr-CH")
# => :"fr-CH"That's it. One line to parse, one method to match.
- Standards-compliant
- Implements RFC 7231 (Accept-Language header) and RFC 4647 (Basic Filtering matching scheme).
- Quality values
- Respects user preferences from
q=1(most preferred) down toq=0(not acceptable).
- Prefix matching
- The range
encorrectly matchesen-US,en-GB, and other regional variants.
- Wildcards
- Supports
*to match any language not explicitly listed.
- Exclusions
- Handles
q=0to explicitly reject specific languages.
- Thread-safe
- Parser instances are immutable and safe to share across threads.
- Getting Started — Install the gem and write your first matching code in 5 minutes.
-
Understanding Quality Values — Learn how
q=0.8differs fromq=1and why it matters. -
Basic Filtering Explained — Understand how
enmatchesen-GBbut noteng. -
Working with Wildcards — Use
*to accept any language as a fallback. -
Excluding Languages — Reject specific languages with
q=0. - BCP 47 Language Tags — Scripts, regions, and variants demystified.
- Rails Integration Guide — Set up automatic locale detection in Ruby on Rails.
- Rack Integration Guide — Build middleware for Sinatra, Hanami, or pure Rack apps.
- Real-World Examples — Patterns for e-commerce, SaaS, APIs, and more.
This library conforms to:
| Specification | Description |
|---|---|
| RFC 7231 §5.3.5 | Accept-Language header definition |
| RFC 7231 §5.3.1 | Quality values syntax |
| RFC 4647 §3.3.1 | Basic Filtering matching scheme |
| BCP 47 | Language tag structure |
Found an issue or have a suggestion? Feel free to open an issue or submit a pull request.
Ready to get started? Head over to Getting Started →