rack-i18n_locale_switcher 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/README.md +11 -0
- data/Rakefile +1 -1
- data/lib/rack/i18n_locale_switcher.rb +9 -5
- data/rack-i18n_locale_switcher.gemspec +2 -2
- data/spec/rack/i18n_locale_switcher_spec.rb +17 -0
- metadata +4 -4
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -28,6 +28,10 @@ as will the following request:
|
|
28
28
|
|
29
29
|
require 'rack/i18n_locale_switcher'
|
30
30
|
config.middleware.use Rack::I18nLocaleSwitcher
|
31
|
+
|
32
|
+
If you use Rails 3.1 with the asset pipeline, make sure you exclude the path to assets:
|
33
|
+
|
34
|
+
config.middleware.use Rack::I18nLocaleSwitcher, :except => /^\/assets/
|
31
35
|
|
32
36
|
### Using it with Sinatra
|
33
37
|
|
@@ -70,6 +74,12 @@ To avoid this, you can set the `canonical` option resulting in requests to be re
|
|
70
74
|
|
71
75
|
In this configuration, requests to `http://en.example.org` will be redirected to `http://example.org` (provided you have set `I18n.default_locale` to `:en`).
|
72
76
|
|
77
|
+
### Exceptions
|
78
|
+
|
79
|
+
If you would like to exclude certain paths from locale switcher, just pass a regex matching these paths in the `except` option.
|
80
|
+
|
81
|
+
use Rack::I18nLocaleSwitcher, :redirect => :path, :except => /^\/(assets|static)\b/
|
82
|
+
|
73
83
|
|
74
84
|
## Configuring I18n
|
75
85
|
|
@@ -81,6 +91,7 @@ You should also set the default locale to which Locale Switcher will fall back i
|
|
81
91
|
|
82
92
|
I18n.default_locale = :de
|
83
93
|
|
94
|
+
|
84
95
|
## Feedback and Contributions
|
85
96
|
|
86
97
|
We appreciate your feedback and contributions. If you find a bug, feel free to to open a GitHub issue. Better yet, add a test that exposes the bug, fix it and send us a pull request.
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('rack-i18n_locale_switcher', '0.5.
|
5
|
+
Echoe.new('rack-i18n_locale_switcher', '0.5.2') do |p|
|
6
6
|
|
7
7
|
p.description = "Detects the current locale from query parameter, path prefix, host or accept header."
|
8
8
|
p.url = "http://github.com/christoph-buente/rack-i18n_locale_switcher"
|
@@ -12,7 +12,8 @@ module Rack
|
|
12
12
|
:param => 'locale',
|
13
13
|
:source => SOURCES,
|
14
14
|
:redirect => nil,
|
15
|
-
:canonical => false
|
15
|
+
:canonical => false,
|
16
|
+
:except => nil
|
16
17
|
}.freeze
|
17
18
|
|
18
19
|
def initialize(app, options = {})
|
@@ -28,6 +29,7 @@ module Rack
|
|
28
29
|
|
29
30
|
@param = options[:param]
|
30
31
|
@canonical = options[:canonical]
|
32
|
+
@except = options[:except]
|
31
33
|
|
32
34
|
@sources = options[:source]
|
33
35
|
@sources = Array(@sources) unless @sources.is_a?(Array)
|
@@ -46,10 +48,12 @@ module Rack
|
|
46
48
|
end
|
47
49
|
|
48
50
|
def call(env)
|
49
|
-
|
51
|
+
return @app.call(env) if env['PATH_INFO'] =~ @except
|
50
52
|
|
53
|
+
I18n.locale = I18n.default_locale
|
54
|
+
|
51
55
|
env['PATH_INFO'].gsub!(/([^\/])\/$/, '\1')
|
52
|
-
|
56
|
+
|
53
57
|
request = Rack::Request.new(env)
|
54
58
|
request_url = request.url
|
55
59
|
|
@@ -61,12 +65,12 @@ module Rack
|
|
61
65
|
I18n.locale = locale
|
62
66
|
end
|
63
67
|
end
|
64
|
-
|
68
|
+
|
65
69
|
if @redirect
|
66
70
|
unless @canonical && I18n.locale == I18n.default_locale
|
67
71
|
send(:"set_locale_in_#@redirect", env)
|
68
72
|
end
|
69
|
-
|
73
|
+
|
70
74
|
if request.url != request_url
|
71
75
|
env['PATH_INFO'] = '' if env['PATH_INFO'] == '/'
|
72
76
|
return [ 301, { 'Location' => request.url }, ["Redirecting"]]
|
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{rack-i18n_locale_switcher}
|
5
|
-
s.version = "0.5.
|
5
|
+
s.version = "0.5.2"
|
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\303\274nte, Andreas Korth"]
|
9
|
-
s.date = %q{2011-11-
|
9
|
+
s.date = %q{2011-11-30}
|
10
10
|
s.description = %q{Detects the current locale from query parameter, path prefix, host or accept 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"]
|
@@ -294,5 +294,22 @@ describe Rack::I18nLocaleSwitcher do
|
|
294
294
|
|
295
295
|
it_should_behave_like "a redirect with the default locale"
|
296
296
|
end
|
297
|
+
|
298
|
+
context "exceptions" do
|
299
|
+
|
300
|
+
let :options do
|
301
|
+
{ :redirect => :path, :except => /^\/assets/ }
|
302
|
+
end
|
303
|
+
|
304
|
+
it "should not redirect if the path is exempt" do
|
305
|
+
[ "http://example.com/assets",
|
306
|
+
"http://de.example.com/assets/foo/bar",
|
307
|
+
"http://example.com/assets/"
|
308
|
+
].each do |url|
|
309
|
+
get url
|
310
|
+
last_response.should_not be_redirect
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
297
314
|
end
|
298
315
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-i18n_locale_switcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 2
|
10
|
+
version: 0.5.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Christoph B\xC3\xBCnte, Andreas Korth"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-30 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|