
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
http_accept_language
Advanced tools
A gem which helps you detect the users preferred language, as sent by the "Accept-Language" HTTP header.
The algorithm is based on RFC 2616, with one exception: when a user requests "en-US" and "en" is an available language, "en" is deemed compatible with "en-US". The RFC specifies that the requested language must either exactly match the available language or must exactly match a prefix of the available language. This means that when the user requests "en" and "en-US" is available, "en-US" would be compatible, but not the other way around. This is usually not what you're looking for.
Since version 2.0, this gem is Rack middleware.
The http_accept_language
method is available in any controller:
class SomeController < ApplicationController
def some_action
http_accept_language.user_preferred_languages # => %w(nl-NL nl-BE nl en-US en)
available = %w(en en-US nl-BE)
http_accept_language.preferred_language_from(available) # => 'nl-BE'
http_accept_language.user_preferred_languages # => %w(en-GB)
available = %w(en-US)
http_accept_language.compatible_language_from(available) # => 'en-US'
http_accept_language.user_preferred_languages # => %w(nl-NL nl-BE nl en-US en)
available = %w(en nl de) # This could be from I18n.available_locales
http_accept_language.preferred_language_from(available) # => 'nl'
end
end
You can easily set the locale used for i18n in a before-filter:
class SomeController < ApplicationController
before_filter :set_locale
private
def set_locale
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
end
end
If you want to enable this behavior by default in your controllers, you can just include the provided concern:
class ApplicationController < ActionController::Base
include HttpAcceptLanguage::AutoLocale
#...
end
Then set available locales in config/application.rb
:
config.i18n.available_locales = %w(en nl de fr)
To use the middleware in any Rack application, simply add the middleware:
require 'http_accept_language'
use HttpAcceptLanguage::Middleware
run YourAwesomeApp
Then you can access it from env
:
class YourAwesomeApp
def initialize(app)
@app = app
end
def call(env)
available = %w(en en-US nl-BE)
language = env.http_accept_language.preferred_language_from(available)
[200, {}, ["Oh, you speak #{language}!"]]
end
end
Install the gem http_accept_language
Add the gem to your Gemfile:
gem 'http_accept_language'
Run bundle install
to install it.
Released under the MIT license
FAQs
Unknown package
We found that http_accept_language demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.