• Stars
    star
    771
  • Rank 58,926 (Top 2 %)
  • Language
    Ruby
  • Created over 16 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ruby on Rails plugin. Fishes out the Accept-Language header into an array.

HttpAcceptLanguage Build Status

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.

Example

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

Available methods

  • user_preferred_languages: Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE, sanitized and all.
  • preferred_language_from(languages): Finds the locale specifically requested by the browser
  • compatible_language_from(languages): Returns the first of the user_preferred_languages that is compatible with the available locales. Ignores region.
  • sanitize_available_locales(languages): Returns a supplied list of available locals without any extra application info that may be attached to the locale for storage in the application.
  • language_region_compatible_from(languages): Returns the first of the user preferred languages that is also found in available languages. Finds best fit by matching on primary language first and secondarily on region. If no matching region is found, return the first language in the group matching that primary language.

Installation

Without Bundler

Install the gem http_accept_language

With Bundler

Add the gem to your Gemfile:

gem 'http_accept_language'

Run bundle install to install it.


Released under the MIT license

More Repositories

1

fake_sqs

Provides a fake SQS server that you can run locally to test against
Ruby
292
star
2

roundsman

Combines Capistrano with Chef Solo
Ruby
204
star
3

elo

The Elo rating system is a method for calculating the relative skill levels of players in two-player games such as chess and Go.
Ruby
158
star
4

metrical

Run MetricFu without making it part of your projects depencies.
Ruby
126
star
5

dotfiles

My bash / zsh / vim / pry settings
Vim Script
88
star
6

i18n_label

Adds translated form labels to Rails 2.2
Ruby
55
star
7

translatable_columns

Provides proxies for localized columns, depending on the locale set by I18n
Ruby
51
star
8

status_cats

Rack middleware replacing status codes with relevant pictures of cats.
Ruby
37
star
9

simple-backend-example

The source code for the article I wrote on Backends in Rails 3.1
Ruby
34
star
10

capistrano_chef_solo

Combining the awesome powers of Capistrano and chef-solo
Ruby
20
star
11

rack-token_auth

Rack middleware for using the Authorization header with token authentication
Ruby
14
star
12

formalize-rails

Use Formalize with the asset pipeline
Ruby
11
star
13

adventures-with-ruby

My own blog
Ruby
11
star
14

not

Syntactic sugar for negating any results: @foo.not.nil?
Ruby
10
star
15

no_value_helper

A simpel helper to display a nice message when there is no value
Ruby
6
star
16

scripted

A framework for organizing scripts
Ruby
6
star
17

spec_coverage

Ruby
6
star
18

basic_named_scopes

Basic named scopes for ActiveRecord makes all find-parameters a named scope
Ruby
5
star
19

model_based_html

Rails plugin for binding html to models
Ruby
4
star
20

root_table

Easy manageable root tables for Rails
Ruby
4
star
21

view

Displaying objects automatically
Ruby
4
star
22

coffee-machine

Some scripts I use for developing with CoffeeScript
Ruby
4
star
23

coffeescript-workshop

Examples and scripts for some workshops
Shell
4
star
24

rspec-smart-formatter

Chooses a formatter for best displaying purpose
Ruby
4
star
25

rule_table

A simple implementation of a rule table with lots of metaprogramming.
Ruby
4
star
26

faker

This fork is deleted soon
Ruby
4
star
27

stots

Ruby
3
star
28

fill

Fill your database, using rake db:seed
Ruby
3
star
29

i18n-airbrake

Ruby
3
star
30

rake_svn

Some handy svn commands for Rails
2
star
31

miniture-ninja

Experiments with WebGL
CoffeeScript
2
star
32

pillory

work in progress
Ruby
2
star
33

validate_database

Rails plugin that validates ActiveRecord according to the database
2
star
34

ubuntu_settings

Personal stuff with configuration, custom scripts and aliases for Ubuntu
Vim Script
2
star
35

capistrano-blaze

Ruby
2
star
36

tally

Simple Slack Bot, using AWS Lambda, SNS, SQS, API Gateway
Ruby
2
star
37

kingsofcode

2
star
38

iain.github.com

Github pages
2
star
39

heliosphere

Sunspot extras
Ruby
2
star
40

wodan

Ruby
1
star
41

opengl

A night of hacking OpenGL in Ruby
Ruby
1
star
42

adventure

Ruby
1
star
43

cramplire

Ruby
1
star
44

test_suite

Ruby
1
star
45

bearded-robot

Ruby
1
star
46

crockford

Crockford's Base32 (not the regular one), it switches out a couple letters to reduce chances of mistyping codes
Ruby
1
star
47

blaze

A tiny Campfire gem
Ruby
1
star
48

object_scoped_i18n

Translate using i18n and scope it according to the object's place in Ruby's hierarchial structure
Ruby
1
star