url_locale 0.1.0 → 0.2.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/README.markdown +93 -36
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/url_locale.rb +28 -14
- data/url_locale.gemspec +63 -0
- metadata +6 -5
data/README.markdown
CHANGED
@@ -1,42 +1,47 @@
|
|
1
1
|
# url_locale
|
2
2
|
|
3
|
-
Add _content-language_ to
|
3
|
+
Add _content-language_ to the response header using Rack. The locale variable is parsed from request URL, if no locale can be detected then `I18n.default_locale` will be your fallback.
|
4
4
|
|
5
5
|
## Rack
|
6
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
|
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 is 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 do this).
|
8
8
|
|
9
9
|
## Detect URL locale
|
10
10
|
|
11
|
-
The gem will
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
11
|
+
The gem will parse `request.url` for `I18n.available_locales` in the following order:
|
12
|
+
|
13
|
+
1. `request.url` listed in `config/url_locale.yml`?
|
14
|
+
2. `request.path` starts with `I18n.available_locales`
|
15
|
+
3. `I18n.default_locale`
|
16
|
+
|
17
|
+
|
18
|
+
## Example
|
19
|
+
|
20
|
+
```yml
|
21
|
+
# config/url_locale.yml
|
22
|
+
en:
|
23
|
+
- http://localehost:3000
|
24
|
+
- http://example.com
|
25
|
+
sv:
|
26
|
+
- http://localehost:3001
|
27
|
+
- http://example.se
|
28
|
+
```
|
29
|
+
```
|
30
|
+
I18n.default_locale => :en
|
31
|
+
I18n.available_locales => [:en, :sv]
|
32
|
+
|
33
|
+
http://example.com => "en"
|
34
|
+
http://sv.example.com => "en" # fallback
|
35
|
+
http://example.se/about => "sv"
|
36
|
+
http://example.com/sv/om => "en"
|
37
|
+
http://www.example.com/sv/om => "sv" # parse path
|
38
|
+
|
39
|
+
http://localhost:3000 => "en"
|
40
|
+
http://sv.localhost:3000 => "en" # fallback
|
41
|
+
http://localhost:3001/about => "sv"
|
42
|
+
http://localhost:3000/sv/om => "en"
|
43
|
+
http://localhost:3002/sv/om => "sv" # parse path
|
44
|
+
```
|
40
45
|
|
41
46
|
If a locale can't be detected, fallback will be `I18n.default_locale`
|
42
47
|
|
@@ -46,10 +51,58 @@ If a locale can't be detected, fallback will be `I18n.default_locale`
|
|
46
51
|
2. Notice the order of the middleware for each environment
|
47
52
|
3. Add `gem 'url_locale'` to Gemfile
|
48
53
|
4. Insert `UrlLocale::Middleware` as the first middleware in the Rack middleware stack
|
49
|
-
5. (optional)
|
54
|
+
5. (optional) create `config/url_locale.yml`
|
50
55
|
6. Add before filter in application to set locale
|
51
56
|
|
52
|
-
### Rails 3 example
|
57
|
+
### Rails 3.1 example
|
58
|
+
|
59
|
+
$ rake middleware RAILS_ENV=development
|
60
|
+
use ActionDispatch::Static
|
61
|
+
use Rack::Lock
|
62
|
+
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x101fefef0>
|
63
|
+
use Rack::Runtime
|
64
|
+
use Rack::MethodOverride
|
65
|
+
use Rails::Rack::Logger
|
66
|
+
use ActionDispatch::ShowExceptions
|
67
|
+
use ActionDispatch::RemoteIp
|
68
|
+
use Rack::Sendfile
|
69
|
+
use ActionDispatch::Reloader
|
70
|
+
use ActionDispatch::Callbacks
|
71
|
+
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
72
|
+
use ActiveRecord::QueryCache
|
73
|
+
use ActionDispatch::Cookies
|
74
|
+
use ActionDispatch::Session::CookieStore
|
75
|
+
use ActionDispatch::Flash
|
76
|
+
use ActionDispatch::ParamsParser
|
77
|
+
use ActionDispatch::Head
|
78
|
+
use Rack::ConditionalGet
|
79
|
+
use Rack::ETag
|
80
|
+
use ActionDispatch::BestStandardsSupport
|
81
|
+
run Learning::Application.routes
|
82
|
+
|
83
|
+
$ rake middleware RAILS_ENV=production
|
84
|
+
use Rack::Cache
|
85
|
+
use Rack::Lock
|
86
|
+
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x101feeb68>
|
87
|
+
use Rack::Runtime
|
88
|
+
use Rack::MethodOverride
|
89
|
+
use Rails::Rack::Logger
|
90
|
+
use ActionDispatch::ShowExceptions
|
91
|
+
use ActionDispatch::RemoteIp
|
92
|
+
use Rack::Sendfile
|
93
|
+
use ActionDispatch::Callbacks
|
94
|
+
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
95
|
+
use ActiveRecord::QueryCache
|
96
|
+
use ActionDispatch::Cookies
|
97
|
+
use ActionDispatch::Session::CookieStore
|
98
|
+
use ActionDispatch::Flash
|
99
|
+
use ActionDispatch::ParamsParser
|
100
|
+
use ActionDispatch::Head
|
101
|
+
use Rack::ConditionalGet
|
102
|
+
use Rack::ETag
|
103
|
+
use ActionDispatch::BestStandardsSupport
|
104
|
+
run Learning::Application.routes
|
105
|
+
|
53
106
|
|
54
107
|
# Gemfile
|
55
108
|
gem 'url_locale'
|
@@ -60,8 +113,13 @@ If a locale can't be detected, fallback will be `I18n.default_locale`
|
|
60
113
|
# config/environments/production.rb
|
61
114
|
config.middleware.insert_before "Rack::Cache", UrlLocale::Middleware
|
62
115
|
|
63
|
-
# config/
|
64
|
-
|
116
|
+
# config/url_locale.yml (optional configuration file)
|
117
|
+
en:
|
118
|
+
- http://localehost:3000
|
119
|
+
- http://example.com
|
120
|
+
sv:
|
121
|
+
- http://localehost:3001
|
122
|
+
- http://example.se
|
65
123
|
|
66
124
|
# app/controllers/application_controller.rb
|
67
125
|
before_filter :set_locale
|
@@ -75,7 +133,6 @@ If a locale can't be detected, fallback will be `I18n.default_locale`
|
|
75
133
|
There is room for improvement for this gem
|
76
134
|
|
77
135
|
- Easier installation
|
78
|
-
- Improved parsing modes
|
79
136
|
- Other ideas?
|
80
137
|
|
81
138
|
If url_locale gets more then 1000 downloads, further development might be worth the effort :)
|
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
|
|
18
18
|
gem.homepage = "http://github.com/danbys/url_locale"
|
19
19
|
gem.license = "MIT"
|
20
20
|
gem.summary = %Q{Use Rack middleware to parse the URL and set content-lang response header}
|
21
|
-
gem.description = %Q{All
|
21
|
+
gem.description = %Q{All HTML 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
22
|
gem.email = "dan.bystrom@gmail.com"
|
23
23
|
gem.authors = ["Dan Byström"]
|
24
24
|
# dependencies defined in Gemfile
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/url_locale.rb
CHANGED
@@ -8,31 +8,45 @@ module UrlLocale
|
|
8
8
|
end
|
9
9
|
|
10
10
|
# Returns a Regexp to extract the locale from +request.host+
|
11
|
-
def subdomain_locales
|
12
|
-
|
13
|
-
end
|
11
|
+
# def subdomain_locales
|
12
|
+
# @@subdomain_locales ||= Regexp.new "(^|\.)(#{translations*'|'})\."
|
13
|
+
# end
|
14
14
|
|
15
15
|
# Returns a Regexp to extract the locale from +request.path+
|
16
16
|
def path_locales
|
17
17
|
@@path_locales ||= Regexp.new "^\/(#{translations*'|'})(\/|$)"
|
18
18
|
end
|
19
19
|
|
20
|
-
def host_mode
|
21
|
-
|
22
|
-
end
|
20
|
+
# def host_mode
|
21
|
+
# @@mode = :host
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# def mode
|
25
|
+
# @@mode ||= :path
|
26
|
+
# end
|
23
27
|
|
24
|
-
def
|
25
|
-
@@
|
28
|
+
def host_locales
|
29
|
+
@@host_locales ||= begin
|
30
|
+
config_fpath = File.join Rails.root, 'config', 'url_locale.yml'
|
31
|
+
result = {}
|
32
|
+
if File.exist? config_fpath
|
33
|
+
YAML::load_file(config_fpath).each do |locale, hosts|
|
34
|
+
for host in hosts
|
35
|
+
result[host] = locale.underscore
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
26
41
|
end
|
27
42
|
|
28
43
|
def detect request
|
29
44
|
if translations.present?
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end || I18n.default_locale.to_s
|
45
|
+
host_url = request.url.split(request.host_with_port).first + request.host_with_port
|
46
|
+
host_locales[host_url] ||
|
47
|
+
path_locales.match(request.path).try(:[], 1) ||
|
48
|
+
I18n.default_locale.to_s
|
49
|
+
end
|
36
50
|
end
|
37
51
|
end
|
38
52
|
end
|
data/url_locale.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{url_locale}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dan Bystr\303\266m"]
|
12
|
+
s.date = %q{2011-07-25}
|
13
|
+
s.description = %q{All HTML 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.}
|
14
|
+
s.email = %q{dan.bystrom@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.markdown",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/url_locale.rb",
|
28
|
+
"lib/url_locale/middleware.rb",
|
29
|
+
"test/helper.rb",
|
30
|
+
"test/test_url_locale.rb",
|
31
|
+
"url_locale.gemspec"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/danbys/url_locale}
|
34
|
+
s.licenses = ["MIT"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.6.2}
|
37
|
+
s.summary = %q{Use Rack middleware to parse the URL and set content-lang response header}
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
44
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
45
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
46
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
47
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
50
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
51
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
53
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
57
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
60
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: url_locale
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Dan Bystr\xC3\xB6m"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-25 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -94,7 +94,7 @@ dependencies:
|
|
94
94
|
version: "0"
|
95
95
|
name: rcov
|
96
96
|
version_requirements: *id005
|
97
|
-
description: All
|
97
|
+
description: All HTML 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
98
|
email: dan.bystrom@gmail.com
|
99
99
|
executables: []
|
100
100
|
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- lib/url_locale/middleware.rb
|
116
116
|
- test/helper.rb
|
117
117
|
- test/test_url_locale.rb
|
118
|
+
- url_locale.gemspec
|
118
119
|
has_rdoc: true
|
119
120
|
homepage: http://github.com/danbys/url_locale
|
120
121
|
licenses:
|