http_accept_language 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +44 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/lib/http_accept_language.rb +52 -0
- data/rails/init.rb +7 -0
- data/tasks/http_accept_language_tasks.rake +4 -0
- data/test/http_accept_language_test.rb +45 -0
- metadata +61 -0
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= HttpAcceptLanguage
|
2
|
+
|
3
|
+
A small effort in making a plugin which helps you detect the users preferred language, as sent by the HTTP header.
|
4
|
+
|
5
|
+
== Features
|
6
|
+
|
7
|
+
* Splits the http-header into languages specified by the user
|
8
|
+
* Returns empty array if header is illformed.
|
9
|
+
* Corrects case to xx-XX
|
10
|
+
* Sorted by priority given, as much as possible.
|
11
|
+
* Gives you the most important language
|
12
|
+
* Gives compatible languages
|
13
|
+
See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
14
|
+
|
15
|
+
== Example
|
16
|
+
|
17
|
+
class SomeController < ApplicationController
|
18
|
+
def some_action
|
19
|
+
|
20
|
+
request.user_preferred_languages
|
21
|
+
# => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
|
22
|
+
|
23
|
+
available = %w{en en-US nl-BE}
|
24
|
+
request.preferred_language_from(available)
|
25
|
+
# => 'nl-BE'
|
26
|
+
|
27
|
+
request.user_preferred_languages
|
28
|
+
# => [ 'en-GB']
|
29
|
+
available = %w{en-US}
|
30
|
+
request.compatible_language_from(available)
|
31
|
+
# => 'en-US'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
== Installation
|
36
|
+
|
37
|
+
Install the gem <tt>http_accept_language</tt>, require it in your Rails app.
|
38
|
+
|
39
|
+
== Changelog
|
40
|
+
|
41
|
+
* 2010-01-05: Gem release
|
42
|
+
* 2009-03-12: Rails 2.3 compatible
|
43
|
+
|
44
|
+
Copyright (c) 2008-2010 Iain Hecker, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gem|
|
6
|
+
gem.name = "http_accept_language"
|
7
|
+
gem.summary = %Q{Parse the HTTP Accept Language Header}
|
8
|
+
gem.description = %Q{Find out which locale the user preferes by reading the languages they specified in their browser}
|
9
|
+
gem.email = "iain@iain.nl"
|
10
|
+
gem.homepage = "http://github.com/iain/http_accept_language"
|
11
|
+
gem.authors = ["Iain Hecker"]
|
12
|
+
end
|
13
|
+
Jeweler::GemcutterTasks.new
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/testtask'
|
19
|
+
desc 'Test the http_accept_language plugin.'
|
20
|
+
Rake::TestTask.new(:test) do |t|
|
21
|
+
t.libs << 'lib'
|
22
|
+
t.pattern = 'test/**/*_test.rb'
|
23
|
+
t.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Default: run unit tests.'
|
27
|
+
task :default => :test
|
28
|
+
|
29
|
+
require 'rake/rdoctask'
|
30
|
+
desc 'Generate documentation for the http_accept_language plugin.'
|
31
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
32
|
+
rdoc.rdoc_dir = 'rdoc'
|
33
|
+
rdoc.title = 'HttpAcceptLanguage'
|
34
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
35
|
+
rdoc.rdoc_files.include('README')
|
36
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
37
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module HttpAcceptLanguage
|
2
|
+
|
3
|
+
# Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE.
|
4
|
+
# Browsers send this HTTP header, so don't think this is holy.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# request.user_preferred_languages
|
9
|
+
# # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
|
10
|
+
#
|
11
|
+
def user_preferred_languages
|
12
|
+
@user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(',').collect do |l|
|
13
|
+
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
14
|
+
l.split(';q=')
|
15
|
+
end.sort do |x,y|
|
16
|
+
raise "Not correctly formatted" unless x.first =~ /^[a-z\-]+$/i
|
17
|
+
y.last.to_f <=> x.last.to_f
|
18
|
+
end.collect do |l|
|
19
|
+
l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase }
|
20
|
+
end
|
21
|
+
rescue # Just rescue anything if the browser messed up badly.
|
22
|
+
[]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Finds the locale specifically requested by the browser.
|
26
|
+
#
|
27
|
+
# Example:
|
28
|
+
#
|
29
|
+
# request.preferred_language_from I18n.available_locales
|
30
|
+
# # => 'nl'
|
31
|
+
#
|
32
|
+
def preferred_language_from(array)
|
33
|
+
(user_preferred_languages & array.collect { |i| i.to_s }).first
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the first of the user_preferred_languages that is compatible
|
37
|
+
# with the available locales. Ignores region.
|
38
|
+
#
|
39
|
+
# Example:
|
40
|
+
#
|
41
|
+
# request.compatible_language_from I18n.available_locales
|
42
|
+
#
|
43
|
+
def compatible_language_from(array)
|
44
|
+
user_preferred_languages.map do |x|
|
45
|
+
x = x.to_s.split("-")[0]
|
46
|
+
array.find do |y|
|
47
|
+
y.to_s.split("-")[0] == x
|
48
|
+
end
|
49
|
+
end.compact.first
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
if defined?(ActionController::Request)
|
2
|
+
ActionController::Request.send :include, HttpAcceptLanguage
|
3
|
+
elsif defined?(ActionController::AbstractRequest)
|
4
|
+
ActionController::AbstractRequest.send :include, HttpAcceptLanguage
|
5
|
+
else
|
6
|
+
ActionController::CgiRequest.send :include, HttpAcceptLanguage
|
7
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
require 'http_accept_language'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class MockedCgiRequest
|
6
|
+
include HttpAcceptLanguage
|
7
|
+
def env
|
8
|
+
@env ||= {'HTTP_ACCEPT_LANGUAGE' => 'en-us,en-gb;q=0.8,en;q=0.6'}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class HttpAcceptLanguageTest < Test::Unit::TestCase
|
13
|
+
def test_should_return_empty_array
|
14
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = nil
|
15
|
+
assert_equal [], request.user_preferred_languages
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_properly_split
|
19
|
+
assert_equal %w{en-US en-GB en}, request.user_preferred_languages
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_ignore_jambled_header
|
23
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
|
24
|
+
assert_equal [], request.user_preferred_languages
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_find_first_available_language
|
28
|
+
assert_equal 'en-GB', request.preferred_language_from(%w{en en-GB})
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_find_first_compatible_language
|
32
|
+
assert_equal 'en-hk', request.compatible_language_from(%w{en-hk})
|
33
|
+
assert_equal 'en', request.compatible_language_from(%w{en})
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_find_first_compatible_from_user_preferred
|
37
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
|
38
|
+
assert_equal 'en', request.compatible_language_from(%w{de en})
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def request
|
43
|
+
@request ||= MockedCgiRequest.new
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_accept_language
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Iain Hecker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-05 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Find out which locale the user preferes by reading the languages they specified in their browser
|
17
|
+
email: iain@iain.nl
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- VERSION
|
28
|
+
- lib/http_accept_language.rb
|
29
|
+
- rails/init.rb
|
30
|
+
- tasks/http_accept_language_tasks.rake
|
31
|
+
- test/http_accept_language_test.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/iain/http_accept_language
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Parse the HTTP Accept Language Header
|
60
|
+
test_files:
|
61
|
+
- test/http_accept_language_test.rb
|