Skip to content

Commit

Permalink
Merge pull request #16 from grosser/all
Browse files Browse the repository at this point in the history
All my pull requests in 1 (resolved merge conflicts)
  • Loading branch information
iain committed Jul 13, 2012
2 parents d60a7c6 + fe4e4c4 commit 86d04cd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Gemfile
@@ -1,4 +1,5 @@
source "http://rubygems.org" source :rubygems


# Specify your gem's dependencies in http_accept_language.gemspec
gemspec gemspec
gem "test-unit", ">=2.5.1"
gem "rake"
49 changes: 43 additions & 6 deletions lib/http_accept_language.rb
Expand Up @@ -55,11 +55,48 @@ def compatible_language_from(available_languages)
end.compact.first end.compact.first
end end


# Returns a supplied list of available locals without any extra application info
# that may be attached to the locale for storage in the application.
#
# Example:
# [ja_JP-x1, en-US-x4, en_UK-x5, fr-FR-x3] => [ja-JP, en-US, en-UK, fr-FR]
#
def sanitize_available_locales(available_languages)
available_languages.map do |avail|
split_locale = avail.split(/[_-]/)

split_locale.map do |e|
e unless e.match(/x|[0-9*]/)
end.compact.join("-")
end
end

# 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.
#
# Example:
#
# request.language_region_compatible(available_languages)
#
def language_region_compatible_from(available_languages)
available_languages = sanitize_available_locales(available_languages)
user_preferred_languages.map do |x| #en-US
lang_group = available_languages.select do |y| # en
y = y.to_s
x.split('-', 2).first == y.split('-', 2).first
end
lang_group.find{|l| l == x} || lang_group.first #en-US, en-UK
end.compact.first
end
end end
if defined?(ActionDispatch::Request)
ActionDispatch::Request.send :include, HttpAcceptLanguage if defined?(ActionPack)
elsif defined?(ActionDispatch::AbstractRequest) classes = if ActionPack::VERSION::MAJOR == 2
ActionDispatch::AbstractRequest.send :include, HttpAcceptLanguage [ActionController::Request, ActionController::CgiRequest]
elsif defined?(ActionDispatch::CgiRequest) else
ActionDispatch::CgiRequest.send :include, HttpAcceptLanguage [ActionDispatch::Request]
end
classes.each{|c| c.send :include, HttpAcceptLanguage }
end end
1 change: 0 additions & 1 deletion rails/init.rb

This file was deleted.

55 changes: 54 additions & 1 deletion test/http_accept_language_test.rb
Expand Up @@ -2,6 +2,24 @@
require 'http_accept_language' require 'http_accept_language'
require 'test/unit' require 'test/unit'


module ActionPack
module VERSION
MAJOR = 3
end
end

module ActionDispatch
class Request
end
end

module ActionController
class CgiRequest
end
class Request
end
end

class MockedCgiRequest class MockedCgiRequest
include HttpAcceptLanguage include HttpAcceptLanguage
def env def env
Expand Down Expand Up @@ -37,13 +55,48 @@ def test_should_find_first_compatible_from_user_preferred
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de' request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
assert_equal 'en', request.compatible_language_from(%w{de en}) assert_equal 'en', request.compatible_language_from(%w{de en})
end end

def test_should_accept_symbols_as_available_languages def test_should_accept_symbols_as_available_languages
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us' request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us'
assert_equal :"en-HK", request.compatible_language_from([:"en-HK"]) assert_equal :"en-HK", request.compatible_language_from([:"en-HK"])
end end


def test_should_sanitize_available_language_names
assert_equal ["en-UK", "en-US", "ja-JP", "pt-BR"], request.sanitize_available_locales(%w{en_UK-x3 en-US-x1 ja_JP-x2 pt-BR-x5})
end

def test_should_find_most_compatible_language_from_user_preferred
request.env['HTTP_ACCEPT_LANGUAGE'] = 'ja,en-gb,en-us,fr-fr'
assert_equal "ja-JP", request.language_region_compatible_from(%w{en-UK en-US ja-JP})
end

def test_should_be_included_into_actionpack_v2
silence_warnings do
ActionPack::VERSION.const_set(:MAJOR, 2)
end
load "http_accept_language.rb"

assert_include ActionController::Request.ancestors, HttpAcceptLanguage
assert_include ActionController::CgiRequest.ancestors, HttpAcceptLanguage
end

def test_should_be_included_into_actionpack_v3
silence_warnings do
ActionPack::VERSION.const_set(:MAJOR, 3)
end
load "http_accept_language.rb"
assert_include ActionDispatch::Request.ancestors, HttpAcceptLanguage
end

private private

def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end

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

0 comments on commit 86d04cd

Please sign in to comment.