rack-locale-root-redirect 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +1 -0
- data/lib/rack/locale-root-redirect.rb +29 -0
- data/rack-locale-root-redirect.gemspec +21 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rémi Prévost
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Rack::LocaleRootRedirect
|
2
|
+
|
3
|
+
`Rack::LocaleRootRedirect` redirects requests to `"/"` based on the `Accept-Language` HTTP header.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application’s Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rack-locale-root-redirect'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```shell
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
With Sinatra:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# Gemfile
|
25
|
+
gem "sinatra"
|
26
|
+
gem "rack-accept", :require => "rack/accept"
|
27
|
+
gem "rack-locale-root-redirect", :require => "rack/locale-root-redirect"
|
28
|
+
|
29
|
+
# config.ru
|
30
|
+
require 'bundler'
|
31
|
+
Bundler.require
|
32
|
+
|
33
|
+
class MyApp < Sinatra::Base
|
34
|
+
use Rack::Accept
|
35
|
+
use Rack::LocaleRootRedirect, :fr => "/fr", :en => "/en"
|
36
|
+
|
37
|
+
get("/fr") { "Français!" }
|
38
|
+
get("/en") { "English!" }
|
39
|
+
end
|
40
|
+
|
41
|
+
run MyApp
|
42
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "rack/accept"
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class LocaleRootRedirect
|
5
|
+
VERSION = "0.0.1"
|
6
|
+
|
7
|
+
def initialize(app, locales = {})
|
8
|
+
@locales = locales
|
9
|
+
@available_locales = locales.keys.map(&:to_s)
|
10
|
+
@default_locale = @available_locales.first
|
11
|
+
@app = app
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
status, headers, response = @app.call(env)
|
16
|
+
|
17
|
+
if env["REQUEST_URI"] == "/"
|
18
|
+
language_matcher = env['rack-accept.request'].language
|
19
|
+
language_matcher.first_level_match = true
|
20
|
+
redirect_lang = language_matcher.best_of(@available_locales) || @default_locale
|
21
|
+
|
22
|
+
status = 301
|
23
|
+
headers["Location"] = @locales[redirect_lang.to_sym]
|
24
|
+
end
|
25
|
+
|
26
|
+
[status, headers, response]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/locale-root-redirect'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rack-locale-root-redirect"
|
8
|
+
gem.version = Rack::LocaleRootRedirect::VERSION
|
9
|
+
gem.authors = ["Rémi Prévost"]
|
10
|
+
gem.email = ["remi@exomel.com"]
|
11
|
+
gem.description = %q{Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based on the `Accept-Language` HTTP header.}
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = "https://github.com/remiprev/rack-locale-root-redirect"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency "rack-accept", ">= 0.4.5"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-locale-root-redirect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rémi Prévost
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack-accept
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.4.5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.4.5
|
30
|
+
description: Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based
|
31
|
+
on the `Accept-Language` HTTP header.
|
32
|
+
email:
|
33
|
+
- remi@exomel.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/rack/locale-root-redirect.rb
|
44
|
+
- rack-locale-root-redirect.gemspec
|
45
|
+
homepage: https://github.com/remiprev/rack-locale-root-redirect
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based on the
|
69
|
+
`Accept-Language` HTTP header.
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|