Skip to content

Commit

Permalink
Releasing v1.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
npflood committed Apr 5, 2017
1 parent b84fa8b commit a3ec825
Show file tree
Hide file tree
Showing 24 changed files with 721 additions and 2 deletions.
3 changes: 2 additions & 1 deletion config/locales/client.en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
en:
js:
site_title: "ECHOcommunity: Conversations"
site_title: "ECHOcommunity: Conversations"
log_in: Log In / Register
Binary file added gems/2.3.1/cache/http_accept_language-2.0.5.gem
Binary file not shown.
7 changes: 7 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.gem
.bundle
Gemfile.lock
pkg/*
.rvmrc
log/*.log
tmp/
2 changes: 2 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
3 changes: 3 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gemspec
8 changes: 8 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby

guard 'rspec', :cli => "-fd", :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { "spec" }
watch('spec/spec_helper.rb') { "spec" }
end

119 changes: 119 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# HttpAcceptLanguage

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](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html), 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:

```ruby
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:

```ruby
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:

```ruby
class ApplicationController < ActionController::Base
include HttpAcceptLanguage::AutoLocale

#...
end
```

To use the middleware in any Rack application, simply add the middleware:

``` ruby
require 'http_accept_language'
use HttpAcceptLanguage::Middleware
run YourAwesomeApp
```

Then you can access it from `env`:

``` ruby
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:

``` ruby
gem 'http_accept_language'
```

Run `bundle install` to install it.

---

Released under the MIT license
13 changes: 13 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "bundler/gem_tasks"

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:cucumber)

Cucumber::Rake::Task.new(:wip, "Run features tagged with @wip") do |t|
t.profile = "wip"
end

task :default => [:spec, :cucumber, :wip]
2 changes: 2 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/cucumber.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default: --format progress --strict --tags ~@wip
wip: --format pretty --wip --tags @wip
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@rails
Feature: Rails Integration

To use http_accept_language inside a Rails application, just add it to your
Gemfile and run `bundle install`.

It is automatically added to your middleware.

Scenario: Installing
When I generate a new Rails app
And I add http_accept_language to my Gemfile
And I run `rake middleware`
Then the output should contain "use HttpAcceptLanguage::Middleware"

Scenario: Using
Given I have installed http_accept_language
When I generate the following controller:
"""
class LanguagesController < ApplicationController
def index
languages = http_accept_language.user_preferred_languages
render :text => "Languages: #{languages.join(' : ')}"
end
end
"""
When I access that action with the HTTP_ACCEPT_LANGUAGE header "en-us,en-gb;q=0.8,en;q=0.6,es-419"
Then the response should contain "Languages: en-US : es-419 : en-GB : en"
37 changes: 37 additions & 0 deletions gems/2.3.1/gems/http_accept_language-2.0.5/features/steps/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Before "@rails" do
@rails = RailsDriver.new
end

When /^I generate a new Rails app$/ do
@rails.generate_rails
end

When /^I add http_accept_language to my Gemfile$/ do
@rails.append_gemfile
end

Given /^I have installed http_accept_language$/ do
@rails.install_gem
end

When /^I generate the following controller:$/ do |string|
@rails.generate_controller "languages", string
end

When /^I access that action with the HTTP_ACCEPT_LANGUAGE header "(.*?)"$/ do |header|
@rails.with_rails_running do
@rails.request_with_http_accept_language_header(header, "/languages")
end
end

Then /^the response should contain "(.*?)"$/ do |output|
@rails.output_should_contain(output)
end

When /^I run `rake middleware`$/ do
@rails.bundle_exec("rake middleware")
end

Then /^the output should contain "(.*?)"$/ do |expected|
@rails.assert_partial_output(expected, @rails.all_output)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require 'aruba/api'

class RailsDriver
include Aruba::Api

def initialize
@aruba_io_wait_seconds = 10
# @announce_stdout = true
# @announce_stderr = true
# @announce_cmd = true
# @announce_dir = true
# @announce_env = true
end

def app_name
"foobar"
end

def install_gem
if app_exists?
cd app_name
else
generate_rails
append_gemfile
end
end

def app_exists?
in_current_dir do
File.exist?("#{app_name}/Gemfile")
end
end

def bundle_exec(cmd)
run_simple "bundle exec #{cmd}"
end

def generate_rails
# install rails with as few things as possible, for speed!
bundle_exec "rails new #{app_name} --force --skip-git --skip-active-record --skip-sprockets --skip-javascript --skip-test-unit --old-style-hash"
cd app_name
end

def append_gemfile
# Specifiy a path so cucumber will use the unreleased version of the gem
append_to_file "Gemfile", "gem 'http_accept_language', :path => '#{gem_path}'"
end

def gem_path
File.expand_path('../../../', __FILE__)
end

def generate_controller(name, content)
bundle_exec "rails generate resource #{name} --force"
write_file "app/controllers/#{name}_controller.rb", content
end

def request_with_http_accept_language_header(header, path)
run_simple "curl --retry 10 -H 'Accept-language: #{header}' #{File.join(host, path)} -o #{response}"
run_simple "cat out.html"
end

def host
"http://localhost:13000"
end

def with_rails_running
start_rails
yield
ensure
stop_rails
end

def start_rails
bundle_exec "rails server -p 13000 -d"
end

def stop_rails
in_current_dir do
`cat tmp/pids/server.pid | xargs kill -9`
end
end

def response
File.expand_path(File.join(current_dir, 'out.html'))
end

def output_should_contain(expected)
actual = File.open(response, 'r:utf-8').read
actual.should include expected
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "http_accept_language/version"

Gem::Specification.new do |s|
s.name = "http_accept_language"
s.version = HttpAcceptLanguage::VERSION
s.authors = ["iain"]
s.email = ["iain@iain.nl"]
s.homepage = "https://github.com/iain/http_accept_language"
s.summary = %q{Find out which locale the user preferes by reading the languages they specified in their browser}
s.description = %q{Find out which locale the user preferes by reading the languages they specified in their browser}
s.license = "MIT"

s.rubyforge_project = "http_accept_language"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'guard-rspec'
s.add_development_dependency 'rails', '>= 3.2.6'
s.add_development_dependency 'cucumber'
s.add_development_dependency 'aruba'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'http_accept_language/auto_locale'
require 'http_accept_language/parser'
require 'http_accept_language/middleware'
require 'http_accept_language/railtie' if defined?(Rails::Railtie)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'active_support/concern'

module HttpAcceptLanguage
module AutoLocale
extend ActiveSupport::Concern

included do
before_filter :set_locale
end

private

def set_locale
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
end
end
end
Loading

0 comments on commit a3ec825

Please sign in to comment.