Switch to using http_accept_language as a gem
authorTom Hughes <tom@compton.nu>
Sat, 21 Jan 2012 11:20:39 +0000 (11:20 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 15 Feb 2012 17:48:15 +0000 (17:48 +0000)
Gemfile
Gemfile.lock
config/initializers/http_accept_language.rb [new file with mode: 0644]
vendor/plugins/http_accept_language/README.rdoc [deleted file]
vendor/plugins/http_accept_language/Rakefile [deleted file]
vendor/plugins/http_accept_language/VERSION [deleted file]
vendor/plugins/http_accept_language/init.rb [deleted file]
vendor/plugins/http_accept_language/lib/http_accept_language.rb [deleted file]
vendor/plugins/http_accept_language/test/http_accept_language_test.rb [deleted file]

diff --git a/Gemfile b/Gemfile
index 4556a6d..a580bce 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -18,6 +18,7 @@ gem 'oauth-plugin', '>= 0.4.0.pre7'
 gem 'open_id_authentication', '>= 1.1.0'
 gem 'validates_email_format_of', '>= 1.5.1'
 gem 'composite_primary_keys', '>= 4.1.2'
+gem 'http_accept_language', '>= 1.0.2'
 
 # Character conversion support for ruby 1.8
 gem 'iconv', :platforms => :ruby_18
index 60f7ba6..75248b9 100644 (file)
@@ -50,6 +50,7 @@ GEM
       multipart-post (~> 1.1)
       rack (~> 1.1)
     hike (1.2.1)
+    http_accept_language (1.0.2)
     httpclient (2.2.4)
     i18n (0.6.0)
     iconv (0.1)
@@ -151,6 +152,7 @@ DEPENDENCIES
   coffee-rails (~> 3.2.1)
   composite_primary_keys (>= 4.1.2)
   dynamic_form
+  http_accept_language (>= 1.0.2)
   httpclient
   iconv
   jquery-rails
diff --git a/config/initializers/http_accept_language.rb b/config/initializers/http_accept_language.rb
new file mode 100644 (file)
index 0000000..04c3dfa
--- /dev/null
@@ -0,0 +1,12 @@
+#
+# Monkey patch HttpAcceptLanguage pending integration of
+# https://github.com/iain/http_accept_language/pull/6
+#
+module HttpAcceptLanguage
+  def compatible_language_from(available_languages)
+    user_preferred_languages.find do |x|
+      available_languages.find { |y| y.to_s == x.to_s } ||
+      available_languages.find { |y| y.to_s =~ /^#{Regexp.escape(x.to_s)}-/ }
+    end
+  end
+end
diff --git a/vendor/plugins/http_accept_language/README.rdoc b/vendor/plugins/http_accept_language/README.rdoc
deleted file mode 100644 (file)
index e644183..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-= HttpAcceptLanguage
-
-A small effort in making a plugin which helps you detect the users preferred language, as sent by the HTTP header.
-
-== Features
-
-* Splits the http-header into languages specified by the user
-* Returns empty array if header is illformed.
-* Corrects case to xx-XX
-* Sorted by priority given, as much as possible.
-* Gives you the most important language
-* Gives compatible languages
-See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
-== Example
-
-  class SomeController < ApplicationController
-    def some_action
-      
-      request.user_preferred_languages
-      # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
-      
-      available = %w{en en-US nl-BE}
-      request.preferred_language_from(available)
-      # => 'nl-BE'
-      
-      request.user_preferred_languages
-      # => [ 'en-GB']
-      available = %w{en-US}
-      request.compatible_language_from(available)
-      # => 'en-US'
-    end
-  end
-
-== Installation
-
-Install the gem <tt>http_accept_language</tt>, require it in your Rails app.
-
-== Changelog
-
-* 2010-01-05: Gem release
-* 2009-03-12: Rails 2.3 compatible
-
-Copyright (c) 2008-2010 Iain Hecker, released under the MIT license
diff --git a/vendor/plugins/http_accept_language/Rakefile b/vendor/plugins/http_accept_language/Rakefile
deleted file mode 100644 (file)
index 6c5a4f5..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-require 'rake'
-
-begin
-  require 'jeweler'
-  Jeweler::Tasks.new do |gem|
-    gem.name = "http_accept_language"
-    gem.summary = %Q{Parse the HTTP Accept Language Header}
-    gem.description = %Q{Find out which locale the user preferes by reading the languages they specified in their browser}
-    gem.email = "iain@iain.nl"
-    gem.homepage = "http://github.com/iain/http_accept_language"
-    gem.authors = ["Iain Hecker"]
-  end
-  Jeweler::GemcutterTasks.new
-rescue LoadError
-  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
-end
-
-require 'rake/testtask'
-desc 'Test the http_accept_language plugin.'
-Rake::TestTask.new(:test) do |t|
-  t.libs << 'lib'
-  t.pattern = 'test/**/*_test.rb'
-  t.verbose = true
-end
-
-desc 'Default: run unit tests.'
-task :default => :test
-
-require 'rake/rdoctask'
-desc 'Generate documentation for the http_accept_language plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
-  rdoc.rdoc_dir = 'rdoc'
-  rdoc.title    = 'HttpAcceptLanguage'
-  rdoc.options << '--line-numbers' << '--inline-source'
-  rdoc.rdoc_files.include('README')
-  rdoc.rdoc_files.include('lib/**/*.rb')
-end
diff --git a/vendor/plugins/http_accept_language/VERSION b/vendor/plugins/http_accept_language/VERSION
deleted file mode 100644 (file)
index 7dea76e..0000000
+++ /dev/null
@@ -1 +0,0 @@
-1.0.1
diff --git a/vendor/plugins/http_accept_language/init.rb b/vendor/plugins/http_accept_language/init.rb
deleted file mode 100644 (file)
index 050a7ca..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-if defined?(ActionDispatch::Request)
-  ActionDispatch::Request.send :include, HttpAcceptLanguage
-elsif defined?(ActionController::Request)
-  ActionController::Request.send :include, HttpAcceptLanguage
-elsif defined?(ActionController::AbstractRequest)
-  ActionController::AbstractRequest.send :include, HttpAcceptLanguage
-else
-  ActionController::CgiRequest.send :include, HttpAcceptLanguage
-end
diff --git a/vendor/plugins/http_accept_language/lib/http_accept_language.rb b/vendor/plugins/http_accept_language/lib/http_accept_language.rb
deleted file mode 100644 (file)
index 0cc8a77..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-module HttpAcceptLanguage
-
-  # Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE.
-  # Browsers send this HTTP header, so don't think this is holy.
-  #
-  # Example:
-  #
-  #   request.user_preferred_languages
-  #   # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
-  #
-  def user_preferred_languages
-    @user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(',').collect do |l|
-      l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
-      l.split(';q=')
-    end.sort do |x,y|
-      raise "Not correctly formatted" unless x.first =~ /^[a-z\-]+$/i
-      y.last.to_f <=> x.last.to_f
-    end.collect do |l|
-      l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase }
-    end
-  rescue # Just rescue anything if the browser messed up badly.
-    []
-  end
-
-  # Sets the user languages preference, overiding the browser
-  #
-  def user_preferred_languages=(languages)
-    @user_preferred_languages = languages
-  end
-
-  # Finds the locale specifically requested by the browser.
-  #
-  # Example:
-  #
-  #   request.preferred_language_from I18n.available_locales
-  #   # => 'nl'
-  #
-  def preferred_language_from(array)
-    (user_preferred_languages & array.collect { |i| i.to_s }).first
-  end
-
-  # Returns the first of the user_preferred_languages that is compatible
-  # with the available locales. Ignores region.
-  #
-  # Example:
-  #
-  #   request.compatible_language_from I18n.available_locales
-  #
-  def compatible_language_from(array)
-    user_preferred_languages.find do |x|
-      array.find { |y| y.to_s == x.to_s } ||
-      array.find { |y| y.to_s =~ /^#{Regexp.escape(x.to_s)}-/ }
-    end
-  end
-
-end
diff --git a/vendor/plugins/http_accept_language/test/http_accept_language_test.rb b/vendor/plugins/http_accept_language/test/http_accept_language_test.rb
deleted file mode 100644 (file)
index 8c8a446..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-require 'http_accept_language'
-require 'test/unit'
-
-class MockedCgiRequest
-  include HttpAcceptLanguage
-  def env
-    @env ||= {'HTTP_ACCEPT_LANGUAGE' => 'en-us,en-gb;q=0.8,en;q=0.6'}
-  end
-end
-
-class HttpAcceptLanguageTest < Test::Unit::TestCase
-  def test_should_return_empty_array
-    request.env['HTTP_ACCEPT_LANGUAGE'] = nil
-    assert_equal [], request.user_preferred_languages
-  end
-
-  def test_should_properly_split
-    assert_equal %w{en-US en-GB en}, request.user_preferred_languages
-  end
-
-  def test_should_ignore_jambled_header
-    request.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
-    assert_equal [], request.user_preferred_languages
-  end
-
-  def test_should_find_first_available_language
-    assert_equal 'en-GB', request.preferred_language_from(%w{en en-GB})
-  end
-
-  def test_should_find_first_compatible_language
-    assert_equal 'en-hk', request.compatible_language_from(%w{en-hk})
-    assert_equal 'en', request.compatible_language_from(%w{en})
-  end
-
-  def test_should_find_first_compatible_from_user_preferred
-    request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
-    assert_equal 'en', request.compatible_language_from(%w{de en})
-  end
-
-  private
-  def request
-    @request ||= MockedCgiRequest.new
-  end
-end