http_accept_language 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -1
- data/Gemfile +4 -0
- data/README.rdoc +6 -5
- data/Rakefile +2 -17
- data/http_accept_language.gemspec +11 -41
- data/lib/http_accept_language.rb +18 -12
- data/test/http_accept_language_test.rb +5 -0
- metadata +34 -47
- data/VERSION +0 -1
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/README.rdoc
CHANGED
@@ -16,14 +16,14 @@ See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
|
16
16
|
|
17
17
|
class SomeController < ApplicationController
|
18
18
|
def some_action
|
19
|
-
|
19
|
+
|
20
20
|
request.user_preferred_languages
|
21
21
|
# => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
|
22
|
-
|
22
|
+
|
23
23
|
available = %w{en en-US nl-BE}
|
24
24
|
request.preferred_language_from(available)
|
25
25
|
# => 'nl-BE'
|
26
|
-
|
26
|
+
|
27
27
|
request.user_preferred_languages
|
28
28
|
# => [ 'en-GB']
|
29
29
|
available = %w{en-US}
|
@@ -34,11 +34,12 @@ See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
|
34
34
|
|
35
35
|
== Installation
|
36
36
|
|
37
|
-
Install the gem
|
37
|
+
Install the gem +http_accept_language+, require it in your Rails app.
|
38
38
|
|
39
39
|
== Changelog
|
40
40
|
|
41
|
+
* 2011-09-11: 1.0.2 release, still works appearantly
|
41
42
|
* 2010-01-05: Gem release
|
42
43
|
* 2009-03-12: Rails 2.3 compatible
|
43
44
|
|
44
|
-
Copyright (c) 2008-
|
45
|
+
Copyright (c) 2008-2011 Iain Hecker, released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,19 +1,4 @@
|
|
1
|
-
require
|
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
|
1
|
+
require "bundler/gem_tasks"
|
17
2
|
|
18
3
|
require 'rake/testtask'
|
19
4
|
desc 'Test the http_accept_language plugin.'
|
@@ -32,6 +17,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
32
17
|
rdoc.rdoc_dir = 'rdoc'
|
33
18
|
rdoc.title = 'HttpAcceptLanguage'
|
34
19
|
rdoc.options << '--line-numbers' << '--inline-source'
|
35
|
-
rdoc.rdoc_files.include('README')
|
20
|
+
rdoc.rdoc_files.include('README.rdoc')
|
36
21
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
37
22
|
end
|
@@ -1,47 +1,17 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
1
|
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
2
|
Gem::Specification.new do |s|
|
7
|
-
s.name
|
8
|
-
s.version
|
9
|
-
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
3
|
+
s.name = "http_accept_language"
|
4
|
+
s.version = '1.0.2'
|
5
|
+
s.authors = ["iain"]
|
6
|
+
s.email = ["iain@iain.nl"]
|
7
|
+
s.homepage = "https://github.com/iain/http_accept_language"
|
8
|
+
s.summary = %q{Find out which locale the user preferes by reading the languages they specified in their browser}
|
13
9
|
s.description = %q{Find out which locale the user preferes by reading the languages they specified in their browser}
|
14
|
-
s.email = %q{iain@iain.nl}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"README.rdoc"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".gitignore",
|
20
|
-
"README.rdoc",
|
21
|
-
"Rakefile",
|
22
|
-
"VERSION",
|
23
|
-
"http_accept_language.gemspec",
|
24
|
-
"lib/http_accept_language.rb",
|
25
|
-
"rails/init.rb",
|
26
|
-
"test/http_accept_language_test.rb"
|
27
|
-
]
|
28
|
-
s.homepage = %q{http://github.com/iain/http_accept_language}
|
29
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
-
s.require_paths = ["lib"]
|
31
|
-
s.rubygems_version = %q{1.3.6}
|
32
|
-
s.summary = %q{Parse the HTTP Accept Language Header}
|
33
|
-
s.test_files = [
|
34
|
-
"test/http_accept_language_test.rb"
|
35
|
-
]
|
36
10
|
|
37
|
-
|
38
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
-
s.specification_version = 3
|
11
|
+
s.rubyforge_project = "http_accept_language"
|
40
12
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
46
17
|
end
|
47
|
-
|
data/lib/http_accept_language.rb
CHANGED
@@ -9,7 +9,7 @@ module HttpAcceptLanguage
|
|
9
9
|
# # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
|
10
10
|
#
|
11
11
|
def user_preferred_languages
|
12
|
-
@user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(
|
12
|
+
@user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(/\s*,\s*/).collect do |l|
|
13
13
|
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
14
14
|
l.split(';q=')
|
15
15
|
end.sort do |x,y|
|
@@ -22,6 +22,12 @@ module HttpAcceptLanguage
|
|
22
22
|
[]
|
23
23
|
end
|
24
24
|
|
25
|
+
# Sets the user languages preference, overiding the browser
|
26
|
+
#
|
27
|
+
def user_preferred_languages=(languages)
|
28
|
+
@user_preferred_languages = languages
|
29
|
+
end
|
30
|
+
|
25
31
|
# Finds the locale specifically requested by the browser.
|
26
32
|
#
|
27
33
|
# Example:
|
@@ -40,20 +46,20 @@ module HttpAcceptLanguage
|
|
40
46
|
#
|
41
47
|
# request.compatible_language_from I18n.available_locales
|
42
48
|
#
|
43
|
-
def compatible_language_from(
|
44
|
-
user_preferred_languages.map do |x|
|
45
|
-
|
46
|
-
|
47
|
-
y.
|
49
|
+
def compatible_language_from(available_languages)
|
50
|
+
user_preferred_languages.map do |x| #en-US
|
51
|
+
available_languages.find do |y| # en
|
52
|
+
y = y.to_s
|
53
|
+
x == y || x.split('-', 2).first == y.split('-', 2).first
|
48
54
|
end
|
49
55
|
end.compact.first
|
50
56
|
end
|
51
57
|
|
52
58
|
end
|
53
|
-
if defined?(
|
54
|
-
|
55
|
-
elsif defined?(
|
56
|
-
|
57
|
-
elsif defined?(
|
58
|
-
|
59
|
+
if defined?(ActionDispatch::Request)
|
60
|
+
ActionDispatch::Request.send :include, HttpAcceptLanguage
|
61
|
+
elsif defined?(ActionDispatch::AbstractRequest)
|
62
|
+
ActionDispatch::AbstractRequest.send :include, HttpAcceptLanguage
|
63
|
+
elsif defined?(ActionDispatch::CgiRequest)
|
64
|
+
ActionDispatch::CgiRequest.send :include, HttpAcceptLanguage
|
59
65
|
end
|
@@ -37,6 +37,11 @@ class HttpAcceptLanguageTest < Test::Unit::TestCase
|
|
37
37
|
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
|
38
38
|
assert_equal 'en', request.compatible_language_from(%w{de en})
|
39
39
|
end
|
40
|
+
|
41
|
+
def test_should_accept_symbols_as_available_languages
|
42
|
+
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us'
|
43
|
+
assert_equal :"en-HK", request.compatible_language_from([:"en-HK"])
|
44
|
+
end
|
40
45
|
|
41
46
|
private
|
42
47
|
def request
|
metadata
CHANGED
@@ -1,69 +1,56 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_accept_language
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 1.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
12
|
-
-
|
7
|
+
authors:
|
8
|
+
- iain
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2010-02-25 00:00:00 +01:00
|
18
|
-
default_executable:
|
12
|
+
date: 2011-09-11 00:00:00.000000000Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
|
-
|
22
|
-
email:
|
14
|
+
description: Find out which locale the user preferes by reading the languages they
|
15
|
+
specified in their browser
|
16
|
+
email:
|
17
|
+
- iain@iain.nl
|
23
18
|
executables: []
|
24
|
-
|
25
19
|
extensions: []
|
26
|
-
|
27
|
-
|
28
|
-
- README.rdoc
|
29
|
-
files:
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
30
22
|
- .gitignore
|
23
|
+
- Gemfile
|
31
24
|
- README.rdoc
|
32
25
|
- Rakefile
|
33
|
-
- VERSION
|
34
26
|
- http_accept_language.gemspec
|
35
27
|
- lib/http_accept_language.rb
|
36
28
|
- rails/init.rb
|
37
29
|
- test/http_accept_language_test.rb
|
38
|
-
|
39
|
-
homepage: http://github.com/iain/http_accept_language
|
30
|
+
homepage: https://github.com/iain/http_accept_language
|
40
31
|
licenses: []
|
41
|
-
|
42
32
|
post_install_message:
|
43
|
-
rdoc_options:
|
44
|
-
|
45
|
-
require_paths:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
46
35
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
- 0
|
60
|
-
version: "0"
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
61
48
|
requirements: []
|
62
|
-
|
63
|
-
|
64
|
-
rubygems_version: 1.3.6
|
49
|
+
rubyforge_project: http_accept_language
|
50
|
+
rubygems_version: 1.8.6
|
65
51
|
signing_key:
|
66
52
|
specification_version: 3
|
67
|
-
summary:
|
68
|
-
|
53
|
+
summary: Find out which locale the user preferes by reading the languages they specified
|
54
|
+
in their browser
|
55
|
+
test_files:
|
69
56
|
- test/http_accept_language_test.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.1
|