url_locale 0.1.0
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/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +87 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/url_locale/middleware.rb +18 -0
- data/lib/url_locale.rb +38 -0
- data/test/helper.rb +18 -0
- data/test/test_url_locale.rb +7 -0
- metadata +153 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "yard", "~> 0.6.0"
|
11
|
+
gem "bundler", "~> 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.6.4"
|
13
|
+
gem "rcov", ">= 0"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.2)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
yard (0.6.8)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
bundler (~> 1.0.0)
|
19
|
+
jeweler (~> 1.6.4)
|
20
|
+
rcov
|
21
|
+
shoulda
|
22
|
+
yard (~> 0.6.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Dan Byström
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# url_locale
|
2
|
+
|
3
|
+
Add _content-language_ to your response header using Rack. The locale variable is parsed from the requested URL or if no locale can be detected then `I18n.default_locale` will be your fallback.
|
4
|
+
|
5
|
+
## Rack
|
6
|
+
|
7
|
+
Rack middleware parses the URL and sets the response header _content-language_ attribute accordingly. Since the header is passed on to the web server, inserting `<meta http-equiv="Content-Language" content="en"/>` in the response body will be superfluous. Please note that cached responses passing through Rack also will get the correct content-language header (can be tricky to configure the web server to this).
|
8
|
+
|
9
|
+
## Detect URL locale
|
10
|
+
|
11
|
+
The gem will look for `I18n.available_locales` in `request.host` or `request.path`. In Path mode which is the default mode, `locale` will be parsed from the path. In Host mode `locale` will be parsed from the host string.
|
12
|
+
|
13
|
+
### Path mode examples
|
14
|
+
|
15
|
+
I18n.default_locale => :en
|
16
|
+
I18n.available_locales => [:sv, :pt]
|
17
|
+
|
18
|
+
http://example.com => "en"
|
19
|
+
http://sv.example.com => "en"
|
20
|
+
http://www.example.com/about => "en"
|
21
|
+
http://www.example.com/pt/em => "pt"
|
22
|
+
http://example.com/sv/om => "sv"
|
23
|
+
http://example.com/fr/sur => "en" # fallback
|
24
|
+
|
25
|
+
If a locale can't be detected, fallback will be `I18n.default_locale`
|
26
|
+
|
27
|
+
### Host mode examples
|
28
|
+
|
29
|
+
I18n.default_locale => :en
|
30
|
+
I18n.available_locales => [:sv, :pt]
|
31
|
+
|
32
|
+
http://example.com => "en"
|
33
|
+
http://sv.example.com => "sv"
|
34
|
+
http://www.example.com/pt/em => "en"
|
35
|
+
http://fr.example.com/sur => "en" # fallback
|
36
|
+
http://example.com/sv/sur => "en"
|
37
|
+
http://sv.example.com/pt/em => "sv"
|
38
|
+
http://sv.mobile.example.com => "sv"
|
39
|
+
http://modile.sv.example.com => "en" # fallback
|
40
|
+
|
41
|
+
If a locale can't be detected, fallback will be `I18n.default_locale`
|
42
|
+
|
43
|
+
## Installation and configuration
|
44
|
+
|
45
|
+
1. Run `rake middleware RAILS_ENV=development` and `rake middleware RAILS_ENV=production`
|
46
|
+
2. Notice the order of the middleware for each environment
|
47
|
+
3. Add `gem 'url_locale'` to Gemfile
|
48
|
+
4. Insert `UrlLocale::Middleware` as the first middleware in the Rack middleware stack
|
49
|
+
5. (optional) Configure - Change to Host mode
|
50
|
+
6. Add before filter in application to set locale
|
51
|
+
|
52
|
+
### Rails 3 example
|
53
|
+
|
54
|
+
# Gemfile
|
55
|
+
gem 'url_locale'
|
56
|
+
|
57
|
+
# config/environments/development.rb
|
58
|
+
config.middleware.insert_before "ActionDispatch::Static", UrlLocale::Middleware
|
59
|
+
|
60
|
+
# config/environments/production.rb
|
61
|
+
config.middleware.insert_before "Rack::Cache", UrlLocale::Middleware
|
62
|
+
|
63
|
+
# config/initializers/url_locale.rb (optional configuration file)
|
64
|
+
UrlLocale.host_mode # comment out this line to run Path mode
|
65
|
+
|
66
|
+
# app/controllers/application_controller.rb
|
67
|
+
before_filter :set_locale
|
68
|
+
|
69
|
+
def set_locale
|
70
|
+
I18n.locale = UrlLocale.detect request
|
71
|
+
end
|
72
|
+
|
73
|
+
## Development
|
74
|
+
|
75
|
+
There is room for improvement for this gem
|
76
|
+
|
77
|
+
- Easier installation
|
78
|
+
- Improved parsing modes
|
79
|
+
- Other ideas?
|
80
|
+
|
81
|
+
If url_locale gets more then 1000 downloads, further development might be worth the effort :)
|
82
|
+
|
83
|
+
## Copyright
|
84
|
+
|
85
|
+
Copyright (c) 2011 Dan Byström. See LICENSE.txt for
|
86
|
+
further details.
|
87
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "url_locale"
|
18
|
+
gem.homepage = "http://github.com/danbys/url_locale"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Use Rack middleware to parse the URL and set content-lang response header}
|
21
|
+
gem.description = %Q{All html and xml responses passing through Rack will have content-lang header when reaching the webserver. The response body won't need a meta content-lang tag, not even when it's is cached response.}
|
22
|
+
gem.email = "dan.bystrom@gmail.com"
|
23
|
+
gem.authors = ["Dan Byström"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'yard'
|
46
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module UrlLocale
|
2
|
+
class Middleware
|
3
|
+
|
4
|
+
def initialize app
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call env
|
9
|
+
status, headers, body = @app.call env
|
10
|
+
request = ::Rack::Request.new env
|
11
|
+
mime_types = env["action_dispatch.request.formats"] || []
|
12
|
+
if mime_types.find(&:html?).present?
|
13
|
+
env['rack.locale'] = headers['Content-Language'] = UrlLocale.detect(request)
|
14
|
+
end
|
15
|
+
[status, headers, body]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/url_locale.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "url_locale/middleware"
|
2
|
+
module UrlLocale
|
3
|
+
|
4
|
+
class << self
|
5
|
+
# Returns an <tt>I18n.available_locales - I18n.default_locale</tt>
|
6
|
+
def translations
|
7
|
+
@@translations ||= (I18n.available_locales - [I18n.default_locale]).map &:to_s
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns a Regexp to extract the locale from +request.host+
|
11
|
+
def subdomain_locales
|
12
|
+
@@subdomain_locales ||= Regexp.new "(^|\.)(#{translations*'|'})\."
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns a Regexp to extract the locale from +request.path+
|
16
|
+
def path_locales
|
17
|
+
@@path_locales ||= Regexp.new "^\/(#{translations*'|'})(\/|$)"
|
18
|
+
end
|
19
|
+
|
20
|
+
def host_mode
|
21
|
+
@@mode = :host
|
22
|
+
end
|
23
|
+
|
24
|
+
def mode
|
25
|
+
@@mode ||= :path
|
26
|
+
end
|
27
|
+
|
28
|
+
def detect request
|
29
|
+
if translations.present?
|
30
|
+
if mode == :path
|
31
|
+
path_locales.match(request.path).try :[], 1
|
32
|
+
else
|
33
|
+
subdomain_locales.match(request.host).try :[], 2
|
34
|
+
end
|
35
|
+
end || I18n.default_locale.to_s
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'url_locale'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: url_locale
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Dan Bystr\xC3\xB6m"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-14 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :development
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
name: shoulda
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
type: :development
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 7
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
- 6
|
47
|
+
- 0
|
48
|
+
version: 0.6.0
|
49
|
+
name: yard
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
prerelease: false
|
53
|
+
type: :development
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 23
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 0
|
63
|
+
- 0
|
64
|
+
version: 1.0.0
|
65
|
+
name: bundler
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
type: :development
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 7
|
76
|
+
segments:
|
77
|
+
- 1
|
78
|
+
- 6
|
79
|
+
- 4
|
80
|
+
version: 1.6.4
|
81
|
+
name: jeweler
|
82
|
+
version_requirements: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
prerelease: false
|
85
|
+
type: :development
|
86
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
name: rcov
|
96
|
+
version_requirements: *id005
|
97
|
+
description: All html and xml responses passing through Rack will have content-lang header when reaching the webserver. The response body won't need a meta content-lang tag, not even when it's is cached response.
|
98
|
+
email: dan.bystrom@gmail.com
|
99
|
+
executables: []
|
100
|
+
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files:
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.markdown
|
106
|
+
files:
|
107
|
+
- .document
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.markdown
|
112
|
+
- Rakefile
|
113
|
+
- VERSION
|
114
|
+
- lib/url_locale.rb
|
115
|
+
- lib/url_locale/middleware.rb
|
116
|
+
- test/helper.rb
|
117
|
+
- test/test_url_locale.rb
|
118
|
+
has_rdoc: true
|
119
|
+
homepage: http://github.com/danbys/url_locale
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
hash: 3
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
version: "0"
|
145
|
+
requirements: []
|
146
|
+
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 1.6.2
|
149
|
+
signing_key:
|
150
|
+
specification_version: 3
|
151
|
+
summary: Use Rack middleware to parse the URL and set content-lang response header
|
152
|
+
test_files: []
|
153
|
+
|