rack-i18n_locale_switcher 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/LICENSE +21 -0
- data/Manifest +9 -0
- data/README.md +3 -0
- data/Rakefile +38 -0
- data/lib/rack/i18n_locale_switcher.rb +84 -0
- data/rack-i18n_locale_switcher.gemspec +44 -0
- data/spec/i18n_locale_switcher_spec.rb +22 -0
- data/spec/spec_helper.rb +7 -0
- metadata +126 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
Copyright (c) 2011 Christoph Bünte, Andreas Korth
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
19
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
20
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
21
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#coding:utf-8
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('rack-i18n_locale_switcher', '0.1.0') do |p|
|
6
|
+
|
7
|
+
p.description = "Detects the current locale from url, domain, parameter, session or accept header."
|
8
|
+
p.url = "http://github.com/christoph-buente/rack-i18n_locale_switcher"
|
9
|
+
p.author = ["Christoph Bünte", "Andreas Korth"]
|
10
|
+
p.email = ["info@christophbuente.de", "andreas.korth@gmail.com"]
|
11
|
+
|
12
|
+
p.retain_gemspec = true
|
13
|
+
|
14
|
+
p.ignore_pattern = %w{
|
15
|
+
Gemfile
|
16
|
+
Gemfile.lock
|
17
|
+
vendor/**/*
|
18
|
+
tmp/*
|
19
|
+
log/*
|
20
|
+
*.tmproj
|
21
|
+
}
|
22
|
+
|
23
|
+
p.runtime_dependencies = [ "rack", "i18n" ]
|
24
|
+
p.development_dependencies = [ "echoe", "rspec", "rack-test" ]
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'rspec/core'
|
28
|
+
require 'rspec/core/rake_task'
|
29
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
30
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
31
|
+
end
|
32
|
+
|
33
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
34
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
35
|
+
spec.rcov = true
|
36
|
+
end
|
37
|
+
|
38
|
+
task default: :spec
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
module Rack
|
3
|
+
class I18nLocaleSwitcher
|
4
|
+
def initialize(app, options = {})
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
request = Rack::Request.new env
|
10
|
+
|
11
|
+
session = request.session
|
12
|
+
locale = extract_locale_from_path_or_params(request)
|
13
|
+
|
14
|
+
if locale == I18n.default_locale
|
15
|
+
session["locale"] = locale
|
16
|
+
path = request.fullpath.gsub(/\/#{locale}\b/, '')
|
17
|
+
# ignore paths given with except option
|
18
|
+
unless request.fullpath =~ %r{/tolk/|/api/}
|
19
|
+
return [ 302, {'Location'=> "#{request.scheme}://#{request.host_with_port}#{path}" }, [] ]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
session["locale"] = locale if is_present?(locale)
|
24
|
+
session["locale"] = first_http_accept_language(env) unless is_present?(session["locale"])
|
25
|
+
|
26
|
+
I18n.locale = session["locale"]
|
27
|
+
|
28
|
+
@app.call cleanup_env(env)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def is_available?(locale)
|
34
|
+
locale ? I18n.available_locales.include?(locale.to_sym) : false
|
35
|
+
end
|
36
|
+
|
37
|
+
def extract_locale_from_params(request)
|
38
|
+
locale = request.params["locale"]
|
39
|
+
locale if is_available?(locale)
|
40
|
+
end
|
41
|
+
|
42
|
+
def extract_locale_from_path(request)
|
43
|
+
if request.path_info =~ /^\/(\w{2,3})\b/
|
44
|
+
$1 if is_available?($1)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def extract_locale_from_path_or_params(request)
|
49
|
+
symbolize_locale( extract_locale_from_path(request) || extract_locale_from_params(request) )
|
50
|
+
end
|
51
|
+
|
52
|
+
def cleanup_env env
|
53
|
+
%w{REQUEST_URI REQUEST_PATH PATH_INFO}.each do |key|
|
54
|
+
if is_present?(env[key]) && env[key].length > 1 && tmp = env[key].split("/")
|
55
|
+
tmp.delete_at(1) if tmp[1] =~ %r{^([a-zA-Z]{2})$}
|
56
|
+
env[key] = tmp.join("/")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
env
|
60
|
+
end
|
61
|
+
|
62
|
+
def first_http_accept_language(env)
|
63
|
+
if lang = env["HTTP_ACCEPT_LANGUAGE"]
|
64
|
+
lang = lang.split(",").map { |l|
|
65
|
+
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
|
66
|
+
l.split(';q=')
|
67
|
+
}.first
|
68
|
+
locale = symbolize_locale(lang.first.split("-").first)
|
69
|
+
else
|
70
|
+
locale = nil
|
71
|
+
end
|
72
|
+
is_available?(locale) ? locale : I18n.available_locales
|
73
|
+
end
|
74
|
+
|
75
|
+
def is_present?(value)
|
76
|
+
!value.to_s.empty?
|
77
|
+
end
|
78
|
+
|
79
|
+
def symbolize_locale(locale)
|
80
|
+
is_present?(locale) ? locale.to_s.downcase.to_sym : locale
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "rack-i18n_locale_switcher"
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Christoph B\u{fc}nte, Andreas Korth"]
|
9
|
+
s.date = "2011-11-24"
|
10
|
+
s.description = "Detects the current locale from url, domain, parameter, session or accept header."
|
11
|
+
s.email = ["info@christophbuente.de", "andreas.korth@gmail.com"]
|
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", "spec/spec_helper.rb"]
|
14
|
+
s.homepage = "http://github.com/christoph-buente/rack-i18n_locale_switcher"
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rack-i18n_locale_switcher", "--main", "README.md"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = "rack-i18n_locale_switcher"
|
18
|
+
s.rubygems_version = "1.8.11"
|
19
|
+
s.summary = "Detects the current locale from url, domain, parameter, session or accept header."
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
s.specification_version = 3
|
23
|
+
|
24
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
+
s.add_runtime_dependency(%q<rack>, [">= 0"])
|
26
|
+
s.add_runtime_dependency(%q<i18n>, [">= 0"])
|
27
|
+
s.add_development_dependency(%q<echoe>, [">= 0"])
|
28
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
29
|
+
s.add_development_dependency(%q<rack-test>, [">= 0"])
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
32
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
33
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
34
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
35
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
36
|
+
end
|
37
|
+
else
|
38
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
39
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
40
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
41
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
42
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Rack::I18nLocaleSwitcher" do
|
4
|
+
|
5
|
+
def app
|
6
|
+
Rack::Builder.new {
|
7
|
+
map "/" do
|
8
|
+
use Rack::I18nLocaleSwitcher
|
9
|
+
run lambda { |env| [200, {}, "Coolness"] }
|
10
|
+
end
|
11
|
+
}.to_app
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
it "should detect locale from Accept-Language-Header" do
|
16
|
+
get '/', {'Accept-Language' => 'en-US, en'}
|
17
|
+
I18n.locale.should == :en
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-i18n_locale_switcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Christoph B\xC3\xBCnte, Andreas Korth"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-11-24 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: i18n
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: echoe
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rspec
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rack-test
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id005
|
70
|
+
description: Detects the current locale from url, domain, parameter, session or accept header.
|
71
|
+
email:
|
72
|
+
- info@christophbuente.de
|
73
|
+
- andreas.korth@gmail.com
|
74
|
+
executables: []
|
75
|
+
|
76
|
+
extensions: []
|
77
|
+
|
78
|
+
extra_rdoc_files:
|
79
|
+
- CHANGELOG
|
80
|
+
- LICENSE
|
81
|
+
- README.md
|
82
|
+
- lib/rack/i18n_locale_switcher.rb
|
83
|
+
files:
|
84
|
+
- CHANGELOG
|
85
|
+
- LICENSE
|
86
|
+
- Manifest
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- lib/rack/i18n_locale_switcher.rb
|
90
|
+
- rack-i18n_locale_switcher.gemspec
|
91
|
+
- spec/i18n_locale_switcher_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: http://github.com/christoph-buente/rack-i18n_locale_switcher
|
94
|
+
licenses: []
|
95
|
+
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options:
|
98
|
+
- --line-numbers
|
99
|
+
- --inline-source
|
100
|
+
- --title
|
101
|
+
- Rack-i18n_locale_switcher
|
102
|
+
- --main
|
103
|
+
- README.md
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: "0"
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: "1.2"
|
118
|
+
requirements: []
|
119
|
+
|
120
|
+
rubyforge_project: rack-i18n_locale_switcher
|
121
|
+
rubygems_version: 1.8.11
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: Detects the current locale from url, domain, parameter, session or accept header.
|
125
|
+
test_files: []
|
126
|
+
|