<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,6 +1,13 @@
 module HttpAcceptLanguage
+
   # Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE.
-  # Browsers send this HTTP header. Don't think this is holy.
+  # Browsers send this HTTP header, so don't think this is holy.
+  #
+  # Example:
+  #
+  #   request.user_preferred_languages
+  #   # =&gt; [ '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+$/
@@ -9,17 +16,37 @@ module HttpAcceptLanguage
       raise &quot;Not correctly formatted&quot; unless x.first =~ /^[a-z\-]+$/i
       y.last.to_f &lt;=&gt; x.last.to_f
     end.collect do |l|
-      l.first.downcase.gsub(/-[a-z]+$/i){|x| x.upcase} 
+      l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase }
     end
-  rescue
+  rescue # Just rescue anything if the browser messed up badly.
     []
   end
 
+  # Finds the locale specifically requested by the browser.
+  #
+  # Example:
+  #
+  #   request.preferred_language_from I18n.available_locales
+  #   # =&gt; 'nl'
+  #
   def preferred_language_from(array)
     (user_preferred_languages &amp; 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)
-    array.select{|x| lang = x.to_s.split(&quot;-&quot;)[0]; user_preferred_languages.any?{|y| y.to_s.split(&quot;-&quot;)[0] == lang }}.first
+    user_preferred_languages.map do |x|
+      x = x.to_s.split(&quot;-&quot;)[0]
+      array.find do |y|
+        y.to_s.split(&quot;-&quot;)[0] == x
+      end
+    end.compact.first
   end
+
 end</diff>
      <filename>lib/http_accept_language.rb</filename>
    </modified>
    <modified>
      <diff>@@ -33,6 +33,11 @@ class HttpAcceptLanguageTest &lt; Test::Unit::TestCase
     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</diff>
      <filename>test/http_accept_language_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>b907a7cd19582eeca9147df463c92f953741b63c</id>
    </parent>
  </parents>
  <author>
    <name>Iain Hecker</name>
    <email>github@iain.nl</email>
  </author>
  <url>http://github.com/iain/http_accept_language/commit/159d2446c7a908afbe6b18471ef9be9a94b454a7</url>
  <id>159d2446c7a908afbe6b18471ef9be9a94b454a7</id>
  <committed-date>2009-03-12T14:56:15-07:00</committed-date>
  <authored-date>2009-03-12T14:56:15-07:00</authored-date>
  <message>Return the first of user compatible locales.
Added some more RDoc stuff.</message>
  <tree>b25173086b28f1aa2ac025d42b303e2c70a73a25</tree>
  <committer>
    <name>Iain Hecker</name>
    <email>github@iain.nl</email>
  </committer>
</commit>
