perfectline-locale-routing 0.1.3
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 +90 -0
- data/Rakefile +45 -0
- data/VERSION.yml +4 -0
- data/lib/locale_routing.rb +7 -0
- data/lib/locale_routing/config.rb +60 -0
- data/lib/locale_routing/locale.rb +80 -0
- data/lib/locale_routing/locale_route_set.rb +48 -0
- metadata +60 -0
data/README.markdown
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# LocaleRouting
|
2
|
+
|
3
|
+
LocaleRouting makes using I18N locale from an url parameter or hostname seamless.
|
4
|
+
It taps into the route recognition and generation methods and checks or adds the locale when nessecary.
|
5
|
+
|
6
|
+
The plugin loads its available locales from Rails default I18N class.
|
7
|
+
For every request, it check the configured part the URL match any of the locale definitions exposed by I18n.available_locales.
|
8
|
+
If a locale string is found, the application I18n.locale is changed and the locale is passed via params[:locale].
|
9
|
+
|
10
|
+
Access to mapped URLs is wrapped also, the current I18n.locale is always prepended to the generated URLs parameters.
|
11
|
+
Additionally you can pass a {:locale => "en} option to your link helpers, which will inject the given locale into the output URL.
|
12
|
+
This parameter defaults to I18n.locale.
|
13
|
+
|
14
|
+
No routes have to be changed to use this functionality.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
There are several different ways to install this plugin.
|
19
|
+
|
20
|
+
### Installing as Rails gem dependency
|
21
|
+
|
22
|
+
To add **locale-routing** as a gem dependecy, add this to your 'config/environment.rb' Rails::Initializer.run do |config| block:
|
23
|
+
config.gem 'perfectline-locale-routing', :lib => 'locale-routing', :source => 'http://gems.github.com'
|
24
|
+
|
25
|
+
To install this gem (and other missing gem dependencies) run 'rake gems:install'.
|
26
|
+
|
27
|
+
### Installing the gem manually
|
28
|
+
|
29
|
+
This requires you to have at least RubyGems version 1.2 (run 'gem -v' to check your current version).
|
30
|
+
As the 'locale-routing' gem is built by GitHub, you have to add GitHub as a gem source on your environment (if you havent done it already):
|
31
|
+
sudo gem sources -a http://gems.github.com
|
32
|
+
|
33
|
+
Install the plugin library:
|
34
|
+
sudo gem install perfectline-locale-routing
|
35
|
+
|
36
|
+
To include them gem in your application, add this to the bottom of your 'environment.rb' or in an initializer:
|
37
|
+
require 'locale_routing'
|
38
|
+
|
39
|
+
### Install as a plugin via Git
|
40
|
+
|
41
|
+
Alternatively you can use 'script/plugin' to export the code into your applications 'vendor/plugins' directory.
|
42
|
+
'script/plugin install git://github.com/perfectline/locale-routing.git'
|
43
|
+
|
44
|
+
## Configuration
|
45
|
+
|
46
|
+
To enable or disable the locale routing set **Perfectline::LocaleRouting::Config.enabled = true/false** # defaults to true
|
47
|
+
To specifiy which part of the URL should be matched for the locale identifier, set **Perfectline::LocaleRouting::Config.match_from = option**
|
48
|
+
|
49
|
+
### Available options for match_from are:
|
50
|
+
* **:params**
|
51
|
+
Searches for a 2 letter locale string in the parameters, specifically in the first query stirng token.
|
52
|
+
Found token is matched against I18n.available_locales and if no such locale exists, it will fall back to the default locale.
|
53
|
+
|
54
|
+
Example:
|
55
|
+
www.myapp.com resolves to root controller with the default locale
|
56
|
+
www.myapp.com/en resolves to root controller with locale => "en"
|
57
|
+
www.myapp.com/foo/bar resolves to "foo" controller, "bar" action with default locale
|
58
|
+
www.myapp.com/fr/foo/bar resolves to "foo" controller, "bar" action with locale => "fr"
|
59
|
+
|
60
|
+
* **:host**
|
61
|
+
Searches for configured matches in the domain name. If a match is found, the configured locale is then checked against I18n.available_locales.
|
62
|
+
If the configured locale does not exist in available locales, it will fall back to the default locale.
|
63
|
+
|
64
|
+
Configuring host to locale mappings is done via **Perfectline::LocaleRouting::Config.mapping do |block|**.
|
65
|
+
Hash key must be the hostname pattern to be matched and value is the locale string.
|
66
|
+
Hostname patterns are like simplified regexp patterns with * wildcard support.
|
67
|
+
Mappings matching is similar to route config - first match found is used, rest is ignored.
|
68
|
+
|
69
|
+
Example:
|
70
|
+
Perfectline::LocaleRouting::Config.mapping do |map|
|
71
|
+
map.match "en.dev.*", "en", # matches en.dev.yoursite.com and en.dev.mysite.co.uk
|
72
|
+
map.match "*.dev.*", "en", # matches foo.dev.yoursite.com and www.dev.yoursite.com but not en.dev.mysite.com as its defined AFTER that mapping
|
73
|
+
map.match "en.*", "en", # matches en.yoursite.com but not en.dev.yoursite.com or en.foo.dev.yoursite.com as its defined AFTER that mapping
|
74
|
+
map.match "*.com", "en", # matches anything with a .com TLD
|
75
|
+
end
|
76
|
+
|
77
|
+
## Warning
|
78
|
+
This plugin has not been fully tested with all possible cases except perhaps the params locale matching.
|
79
|
+
As you can see there are no tests yet either, although they will be created as soon as possible.
|
80
|
+
So if you do run into issues or have any improvement ideas, feel free to contact the authors.
|
81
|
+
|
82
|
+
## Honorable mentions
|
83
|
+
This plugin is heavily inspired by the "routing-filter" plugin by **Sven Fuchs**.
|
84
|
+
|
85
|
+
## Authors:
|
86
|
+
Tanel Suurhans - tanel.suurhans_at_perfectline_d0t_ee
|
87
|
+
Tarmo Lehtpuu - tarmo.lehtpuu_at_perfectline_d0t_ee
|
88
|
+
|
89
|
+
## License
|
90
|
+
Copyright 2009 by PerfectLine LLC (<http://www.perfectline.co.uk>) and is released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rcov/rcovtask'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'jeweler'
|
11
|
+
Jeweler::Tasks.new do |jewel|
|
12
|
+
jewel.name = 'locale-routing'
|
13
|
+
jewel.summary = 'LocaleRouting makes parsing I18N locale from an url parameters/hostname seamless.'
|
14
|
+
jewel.email = 'tanel.suurhans@perfectline.ee'
|
15
|
+
jewel.homepage = 'http://github.com/perfectline/locale-routing/tree/master'
|
16
|
+
jewel.description = 'A library for configuring/parsin locales from url params or hostname for I18n.'
|
17
|
+
jewel.authors = ["Tanel Suurhans", "Tarmo Lehtpuu"]
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Test the LocaleRouting plugin.'
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Generate documentation for the LocaleRouting plugin.'
|
32
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
33
|
+
rdoc.rdoc_dir = 'rdoc'
|
34
|
+
rdoc.title = 'LocaleRouting'
|
35
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
36
|
+
rdoc.rdoc_files.include('README.markdown')
|
37
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Generate code coverage report'
|
41
|
+
Rcov::RcovTask.new(:rcov) do |rcov|
|
42
|
+
rcov.libs << 'test'
|
43
|
+
rcov.test_files = FileList['test/*_test.rb']
|
44
|
+
rcov.verbose = true
|
45
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Perfectline
|
2
|
+
module LocaleRouting
|
3
|
+
|
4
|
+
class Mapper
|
5
|
+
def initialize(config)
|
6
|
+
@config = config
|
7
|
+
end
|
8
|
+
|
9
|
+
def match(host, locale)
|
10
|
+
@config.push({:host => host.to_s.gsub('.', '\.').gsub('*', '.*'), :locale => locale.to_s})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Config
|
15
|
+
@locale_match_options = [:params, :host]
|
16
|
+
@host_mapping = []
|
17
|
+
|
18
|
+
# defaults
|
19
|
+
@enabled = true
|
20
|
+
@match_from = :params
|
21
|
+
|
22
|
+
class << self
|
23
|
+
|
24
|
+
def enabled?
|
25
|
+
@enabled
|
26
|
+
end
|
27
|
+
|
28
|
+
def enabled=(enabled)
|
29
|
+
@enabled = (enabled.nil? || enabled == false) ? false : true
|
30
|
+
end
|
31
|
+
|
32
|
+
def match_from
|
33
|
+
@match_from
|
34
|
+
end
|
35
|
+
|
36
|
+
def match_from=(location)
|
37
|
+
unless @locale_match_options.include?(location.to_sym)
|
38
|
+
raise "#{location.to_sym} is not supported as Perfectline::LocaleRouting.match_from option."
|
39
|
+
end
|
40
|
+
|
41
|
+
@match_from = location.to_sym
|
42
|
+
end
|
43
|
+
|
44
|
+
def host_mapping
|
45
|
+
@host_mapping
|
46
|
+
end
|
47
|
+
|
48
|
+
def mapping
|
49
|
+
yield Mapper.new(@host_mapping)
|
50
|
+
end
|
51
|
+
|
52
|
+
# straps the LocaleRouteSet module into ActionControllers RouteSet
|
53
|
+
def bootstrap
|
54
|
+
ActionController::Routing::RouteSet.send(:include, Perfectline::LocaleRouting::LocaleRouteSet) if self.enabled?
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Perfectline
|
2
|
+
module LocaleRouting
|
3
|
+
# class containing Locale handling methods
|
4
|
+
class Locale
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# locale matching pattern
|
8
|
+
def match_params_locale
|
9
|
+
@@match_params_locale ||= %r(^/((#{I18n.available_locales.map{|o| Regexp.escape(o.to_s)}.join('|')})(?=/|$)))
|
10
|
+
end
|
11
|
+
|
12
|
+
def match_url
|
13
|
+
@@match_url ||= %r(^(http.?://[^/]*)?(.*))
|
14
|
+
end
|
15
|
+
|
16
|
+
# prepend locale to generated url
|
17
|
+
|
18
|
+
def prepend(result, locale)
|
19
|
+
# check the results
|
20
|
+
url = result.is_a?(Array) ? result.first : result
|
21
|
+
locale = I18n.locale if locale.nil?
|
22
|
+
|
23
|
+
# substitute the locale
|
24
|
+
url.sub!(self.match_url){"#{$1}/#{locale}#{$2}"}
|
25
|
+
end
|
26
|
+
|
27
|
+
# extract and set locale
|
28
|
+
def extract(path, env)
|
29
|
+
case Perfectline::LocaleRouting::Config.match_from
|
30
|
+
when :params then path = extract_from_params(path)
|
31
|
+
when :host then extract_from_host(env)
|
32
|
+
end
|
33
|
+
|
34
|
+
return path
|
35
|
+
end
|
36
|
+
|
37
|
+
def extract_from_params(path)
|
38
|
+
# match for configured locales and remove them
|
39
|
+
replacement = path.sub!(self.match_params_locale, '')
|
40
|
+
locale = I18n.default_locale
|
41
|
+
|
42
|
+
# if matches were found, set the locale and new path
|
43
|
+
if (not replacement.nil?)
|
44
|
+
path = replacement
|
45
|
+
locale = $1.to_sym
|
46
|
+
else
|
47
|
+
RAILS_DEFAULT_LOGGER.info "No valid locale string found in the URL, falling back to #{locale}"
|
48
|
+
end
|
49
|
+
|
50
|
+
# set the locale and return the 'cleaned' path string
|
51
|
+
I18n.locale = locale and return path
|
52
|
+
end
|
53
|
+
|
54
|
+
def extract_from_host(env)
|
55
|
+
if env[:host].nil?
|
56
|
+
raise "Valid host not found (Perfectline::LocaleRouting.match_from = :host)."
|
57
|
+
end
|
58
|
+
|
59
|
+
host = env[:host].to_s
|
60
|
+
locale = I18n.default_locale
|
61
|
+
mappings = Perfectline::LocaleRouting::Config.host_mapping
|
62
|
+
|
63
|
+
if mappings.nil? || mappings.empty?
|
64
|
+
RAILS_DEFAULT_LOGGER.warn "Perfectline::LocaleRouting.host_mapping is nil or empty."
|
65
|
+
end
|
66
|
+
|
67
|
+
puts mappings.inspect
|
68
|
+
|
69
|
+
mappings.each do |element|
|
70
|
+
unless host.match(%r(^#{element[:host]}$)).nil?
|
71
|
+
locale = element[:locale] and break
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
I18n.locale = locale
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Perfectline
|
2
|
+
module LocaleRouting
|
3
|
+
# module containing methods for RouteSet extending
|
4
|
+
module LocaleRouteSet
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
base.alias_method_chain(:recognize_path, :locale)
|
9
|
+
base.alias_method_chain(:generate, :locale)
|
10
|
+
base.alias_method_chain(:extract_request_environment, :locale)
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
|
15
|
+
# TODO: support locale in subdomain and top level domain extension
|
16
|
+
def recognize_path_with_locale(path, env)
|
17
|
+
# set locale and strip it from the path for recognition
|
18
|
+
path = Perfectline::LocaleRouting::Locale.extract(path, env)
|
19
|
+
# process the route and add the locale parameter to return value
|
20
|
+
returning recognize_path_without_locale(path, env) do |params|
|
21
|
+
params[:locale] = I18n.locale
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def generate_with_locale(*args)
|
26
|
+
# check if locale is set
|
27
|
+
locale = args.first.delete(:locale)
|
28
|
+
|
29
|
+
returning generate_without_locale(*args) do |result|
|
30
|
+
Perfectline::LocaleRouting::Locale.prepend(result, locale)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# override request env extraction to include more info
|
35
|
+
def extract_request_environment_with_locale(request)
|
36
|
+
# merge the original hash with new info
|
37
|
+
extract_request_environment_without_locale(request).merge({
|
38
|
+
:host => request.host,
|
39
|
+
:port => request.port,
|
40
|
+
:domain => request.domain,
|
41
|
+
:subdomain => request.subdomains.first,
|
42
|
+
:subdomains => request.subdomains
|
43
|
+
})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: perfectline-locale-routing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tanel Suurhans
|
8
|
+
- Tarmo Lehtpuu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-05-02 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A library for configuring/parsin locales from url params or hostname for I18n.
|
18
|
+
email: tanel.suurhans@perfectline.ee
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.markdown
|
25
|
+
files:
|
26
|
+
- README.markdown
|
27
|
+
- Rakefile
|
28
|
+
- VERSION.yml
|
29
|
+
- lib/locale_routing.rb
|
30
|
+
- lib/locale_routing/config.rb
|
31
|
+
- lib/locale_routing/locale.rb
|
32
|
+
- lib/locale_routing/locale_route_set.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/perfectline/locale-routing/tree/master
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: LocaleRouting makes parsing I18N locale from an url parameters/hostname seamless.
|
59
|
+
test_files: []
|
60
|
+
|