rack-i18n_locale_switcher 0.3.0 → 0.3.1
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/CHANGELOG +5 -1
- data/Manifest +0 -1
- data/Rakefile +2 -2
- data/lib/rack/i18n_locale_switcher.rb +22 -51
- data/rack-i18n_locale_switcher.gemspec +4 -4
- data/spec/i18n_locale_switcher_spec.rb +13 -9
- metadata +17 -16
- data/spec/spec_helper.rb +0 -7
data/CHANGELOG
CHANGED
data/Manifest
CHANGED
data/Rakefile
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('rack-i18n_locale_switcher', '0.3.
|
5
|
+
Echoe.new('rack-i18n_locale_switcher', '0.3.1') do |p|
|
6
6
|
|
7
|
-
p.description = "Detects the current locale from
|
7
|
+
p.description = "Detects the current locale from host, top level domain, query parameter or Accept-Language header."
|
8
8
|
p.url = "http://github.com/christoph-buente/rack-i18n_locale_switcher"
|
9
9
|
p.author = ["Christoph Bünte", "Andreas Korth"]
|
10
10
|
p.email = ["info@christophbuente.de", "andreas.korth@gmail.com"]
|
@@ -3,85 +3,56 @@ require 'domainatrix'
|
|
3
3
|
|
4
4
|
module Rack
|
5
5
|
class I18nLocaleSwitcher
|
6
|
+
|
6
7
|
def initialize(app, options = {})
|
7
8
|
@app = app
|
8
9
|
end
|
9
10
|
|
10
11
|
def call(env)
|
11
|
-
|
12
|
-
|
13
|
-
I18n.locale = (is_present?(locale) ? locale : I18n.default_locale)
|
14
|
-
@app.call cleanup_env(env)
|
12
|
+
I18n.locale = extract_locale(env)
|
13
|
+
@app.call(env)
|
15
14
|
end
|
16
15
|
|
17
16
|
private
|
18
17
|
|
19
|
-
def
|
20
|
-
|
18
|
+
def extract_locale(env)
|
19
|
+
request = Rack::Request.new(env)
|
20
|
+
uses = [ :param, :path, :subdomain, :tld, :client ]
|
21
|
+
uses.each do |use|
|
22
|
+
if locale = send(:"extract_locale_from_#{ use }", request)
|
23
|
+
unless locale.empty?
|
24
|
+
locale = locale.to_sym
|
25
|
+
return locale if I18n.available_locales.include?(locale)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
I18n.default_locale
|
21
30
|
end
|
22
31
|
|
23
|
-
def
|
24
|
-
|
25
|
-
locale if is_available?(locale)
|
32
|
+
def extract_locale_from_param(request)
|
33
|
+
request.params["locale"]
|
26
34
|
end
|
27
35
|
|
28
36
|
def extract_locale_from_path(request)
|
29
|
-
|
30
|
-
$1 if is_available?($1)
|
31
|
-
end
|
37
|
+
request.path_info =~ /^\/(\w{2,3})\b/ && $1
|
32
38
|
end
|
33
39
|
|
34
40
|
def extract_locale_from_tld(request)
|
35
|
-
|
36
|
-
locale if is_available?(locale)
|
41
|
+
Domainatrix.parse(request.url).public_suffix rescue nil
|
37
42
|
end
|
38
43
|
|
39
44
|
def extract_locale_from_subdomain(request)
|
40
|
-
|
41
|
-
locale if is_available?(locale)
|
45
|
+
Domainatrix.parse(request.url).subdomain rescue nil
|
42
46
|
end
|
43
47
|
|
44
|
-
def
|
48
|
+
def extract_locale_from_client(request)
|
45
49
|
if lang = request.env["HTTP_ACCEPT_LANGUAGE"]
|
46
50
|
lang = lang.split(",").map { |l|
|
47
51
|
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
48
52
|
l.split(';q=')
|
49
53
|
}.first
|
50
|
-
|
51
|
-
else
|
52
|
-
locale = I18n.default_locale
|
53
|
-
end
|
54
|
-
is_available?(locale) ? locale : I18n.default_locale
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
def extract_locale(request)
|
59
|
-
locale = ( extract_locale_from_params(request) ||
|
60
|
-
extract_locale_from_path(request) ||
|
61
|
-
extract_locale_from_subdomain(request) ||
|
62
|
-
extract_locale_from_tld(request) ||
|
63
|
-
extract_locale_from_accept_language(request))
|
64
|
-
symbolize_locale(locale)
|
65
|
-
end
|
66
|
-
|
67
|
-
def cleanup_env env
|
68
|
-
%w{REQUEST_URI REQUEST_PATH PATH_INFO}.each do |key|
|
69
|
-
if is_present?(env[key]) && env[key].length > 1 && tmp = env[key].split("/")
|
70
|
-
tmp.delete_at(1) if tmp[1] =~ %r{^([a-zA-Z]{2})$}
|
71
|
-
env[key] = tmp.join("/")
|
72
|
-
end
|
54
|
+
lang.first.split("-").first
|
73
55
|
end
|
74
|
-
env
|
75
56
|
end
|
76
|
-
|
77
|
-
|
78
|
-
def is_present?(value)
|
79
|
-
!value.to_s.empty?
|
80
|
-
end
|
81
|
-
|
82
|
-
def symbolize_locale(locale)
|
83
|
-
(is_present?(locale) ? locale.to_s.downcase.to_sym : nil)
|
84
|
-
end
|
85
|
-
|
86
57
|
end
|
87
58
|
end
|
@@ -2,21 +2,21 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "rack-i18n_locale_switcher"
|
5
|
-
s.version = "0.3.
|
5
|
+
s.version = "0.3.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Christoph B\u{fc}nte, Andreas Korth"]
|
9
9
|
s.date = "2011-11-25"
|
10
|
-
s.description = "Detects the current locale from
|
10
|
+
s.description = "Detects the current locale from host, top level domain, query parameter or Accept-Language header."
|
11
11
|
s.email = ["info@christophbuente.de", "andreas.korth@gmail.com"]
|
12
12
|
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "lib/rack/i18n_locale_switcher.rb"]
|
13
|
-
s.files = ["CHANGELOG", "LICENSE", "Manifest", "README.md", "Rakefile", "lib/rack/i18n_locale_switcher.rb", "rack-i18n_locale_switcher.gemspec", "spec/i18n_locale_switcher_spec.rb"
|
13
|
+
s.files = ["CHANGELOG", "LICENSE", "Manifest", "README.md", "Rakefile", "lib/rack/i18n_locale_switcher.rb", "rack-i18n_locale_switcher.gemspec", "spec/i18n_locale_switcher_spec.rb"]
|
14
14
|
s.homepage = "http://github.com/christoph-buente/rack-i18n_locale_switcher"
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rack-i18n_locale_switcher", "--main", "README.md"]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
s.rubyforge_project = "rack-i18n_locale_switcher"
|
18
18
|
s.rubygems_version = "1.8.11"
|
19
|
-
s.summary = "Detects the current locale from
|
19
|
+
s.summary = "Detects the current locale from host, top level domain, query parameter or Accept-Language header."
|
20
20
|
|
21
21
|
if s.respond_to? :specification_version then
|
22
22
|
s.specification_version = 3
|
@@ -1,11 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require 'rack/i18n_locale_switcher'
|
2
|
+
require 'rack/test'
|
2
3
|
|
3
|
-
describe
|
4
|
+
describe Rack::I18nLocaleSwitcher do
|
4
5
|
|
5
|
-
|
6
|
-
I18n.available_locales = [:en, :'en-US', :de, :'de-DE', :es]
|
7
|
-
I18n.default_locale = :en
|
8
|
-
end
|
6
|
+
include Rack::Test::Methods
|
9
7
|
|
10
8
|
def app
|
11
9
|
Rack::Builder.new {
|
@@ -16,12 +14,17 @@ describe "Rack::I18nLocaleSwitcher" do
|
|
16
14
|
}.to_app
|
17
15
|
end
|
18
16
|
|
19
|
-
|
17
|
+
before do
|
18
|
+
I18n.available_locales = [:en, :'en-US', :de, :'de-DE', :es]
|
19
|
+
I18n.default_locale = :en
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set the locale to default locale" do
|
20
23
|
get '/'
|
21
24
|
I18n.locale.should eql(I18n.default_locale)
|
22
25
|
end
|
23
26
|
|
24
|
-
context '
|
27
|
+
context 'request param' do
|
25
28
|
|
26
29
|
it "should set the I18n locale" do
|
27
30
|
get '/', :locale => 'de'
|
@@ -29,13 +32,14 @@ describe "Rack::I18nLocaleSwitcher" do
|
|
29
32
|
I18n.locale.should eql(:de)
|
30
33
|
end
|
31
34
|
|
32
|
-
it "should
|
35
|
+
it "should only allow available locales" do
|
33
36
|
get '/', :locale => 'xx'
|
34
37
|
I18n.locale.should eql(I18n.default_locale)
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
38
41
|
context 'from path prefix ' do
|
42
|
+
|
39
43
|
it "should set the I18n locale" do
|
40
44
|
get '/de/'
|
41
45
|
I18n.locale.should eql(:de)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-i18n_locale_switcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-25 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &2154168640 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2154168640
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: i18n
|
27
|
-
requirement: &
|
27
|
+
requirement: &2154168180 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2154168180
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: domainatrix
|
38
|
-
requirement: &
|
38
|
+
requirement: &2154167720 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2154167720
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: echoe
|
49
|
-
requirement: &
|
49
|
+
requirement: &2154167260 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2154167260
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rspec
|
60
|
-
requirement: &
|
60
|
+
requirement: &2154166800 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2154166800
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rack-test
|
71
|
-
requirement: &
|
71
|
+
requirement: &2154166340 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,8 +76,9 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
80
|
-
description: Detects the current locale from
|
79
|
+
version_requirements: *2154166340
|
80
|
+
description: Detects the current locale from host, top level domain, query parameter
|
81
|
+
or Accept-Language header.
|
81
82
|
email:
|
82
83
|
- info@christophbuente.de
|
83
84
|
- andreas.korth@gmail.com
|
@@ -97,7 +98,6 @@ files:
|
|
97
98
|
- lib/rack/i18n_locale_switcher.rb
|
98
99
|
- rack-i18n_locale_switcher.gemspec
|
99
100
|
- spec/i18n_locale_switcher_spec.rb
|
100
|
-
- spec/spec_helper.rb
|
101
101
|
homepage: http://github.com/christoph-buente/rack-i18n_locale_switcher
|
102
102
|
licenses: []
|
103
103
|
post_install_message:
|
@@ -127,5 +127,6 @@ rubyforge_project: rack-i18n_locale_switcher
|
|
127
127
|
rubygems_version: 1.8.11
|
128
128
|
signing_key:
|
129
129
|
specification_version: 3
|
130
|
-
summary: Detects the current locale from
|
130
|
+
summary: Detects the current locale from host, top level domain, query parameter or
|
131
|
+
Accept-Language header.
|
131
132
|
test_files: []
|