Skip to content

Commit

Permalink
added preferred_language_from method
Browse files Browse the repository at this point in the history
  • Loading branch information
iain committed Aug 3, 2008
1 parent d25f491 commit 7acb5a3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ A small effort in making a plugin which helps you detect the users prefered lang

= 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

See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

= Example

class SomeController < ApplicationController
def some_action

request.user_preferred_language
# => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]

available = %w{en en-US nl-BE}
request.preferred_language_from(available)
# => 'nl-BE'

end
end

Expand Down
Binary file added lib/.http_accept_language.rb.swp
Binary file not shown.
3 changes: 3 additions & 0 deletions lib/http_accept_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ def user_preferred_languages
rescue
[]
end
def preferred_language_from(array)
(user_preferred_languages & array).first
end
end
4 changes: 4 additions & 0 deletions test/http_accept_language_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def test_should_ignore_jambled_header
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

private
def request
@request ||= MockedCgiRequest.new
Expand Down

0 comments on commit 7acb5a3

Please sign in to comment.