i18n_routing 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -2
- data/Gemfile.lock +54 -56
- data/Rakefile +3 -0
- data/lib/i18n_routing.rb +3 -1
- data/lib/{i18n_routing_rails3.rb → i18n_routing_rails31.rb} +0 -0
- data/lib/i18n_routing_rails32.rb +435 -0
- data/spec/spec_helper.rb +5 -1
- metadata +40 -62
data/Gemfile
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
|
-
gem 'rails', '3.1
|
3
|
-
gem 'rspec', '>=2.
|
2
|
+
gem 'rails', '3.2.1'
|
3
|
+
gem 'rspec', '>=2.8.0'
|
data/Gemfile.lock
CHANGED
@@ -1,84 +1,82 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
actionmailer (3.1
|
5
|
-
actionpack (= 3.1
|
6
|
-
mail (~> 2.
|
7
|
-
actionpack (3.1
|
8
|
-
activemodel (= 3.1
|
9
|
-
activesupport (= 3.1
|
4
|
+
actionmailer (3.2.1)
|
5
|
+
actionpack (= 3.2.1)
|
6
|
+
mail (~> 2.4.0)
|
7
|
+
actionpack (3.2.1)
|
8
|
+
activemodel (= 3.2.1)
|
9
|
+
activesupport (= 3.2.1)
|
10
10
|
builder (~> 3.0.0)
|
11
11
|
erubis (~> 2.7.0)
|
12
|
-
|
13
|
-
rack (~> 1.
|
14
|
-
rack-cache (~> 1.
|
15
|
-
rack-mount (~> 0.8.2)
|
12
|
+
journey (~> 1.0.1)
|
13
|
+
rack (~> 1.4.0)
|
14
|
+
rack-cache (~> 1.1)
|
16
15
|
rack-test (~> 0.6.1)
|
17
|
-
sprockets (~> 2.
|
18
|
-
activemodel (3.1
|
19
|
-
activesupport (= 3.1
|
20
|
-
bcrypt-ruby (~> 3.0.0)
|
16
|
+
sprockets (~> 2.1.2)
|
17
|
+
activemodel (3.2.1)
|
18
|
+
activesupport (= 3.2.1)
|
21
19
|
builder (~> 3.0.0)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
arel (~> 2.2.1)
|
20
|
+
activerecord (3.2.1)
|
21
|
+
activemodel (= 3.2.1)
|
22
|
+
activesupport (= 3.2.1)
|
23
|
+
arel (~> 3.0.0)
|
27
24
|
tzinfo (~> 0.3.29)
|
28
|
-
activeresource (3.1
|
29
|
-
activemodel (= 3.1
|
30
|
-
activesupport (= 3.1
|
31
|
-
activesupport (3.1
|
25
|
+
activeresource (3.2.1)
|
26
|
+
activemodel (= 3.2.1)
|
27
|
+
activesupport (= 3.2.1)
|
28
|
+
activesupport (3.2.1)
|
29
|
+
i18n (~> 0.6)
|
32
30
|
multi_json (~> 1.0)
|
33
|
-
arel (
|
34
|
-
bcrypt-ruby (3.0.1)
|
31
|
+
arel (3.0.0)
|
35
32
|
builder (3.0.0)
|
36
33
|
diff-lcs (1.1.3)
|
37
34
|
erubis (2.7.0)
|
38
35
|
hike (1.2.1)
|
39
36
|
i18n (0.6.0)
|
40
|
-
|
37
|
+
journey (1.0.1)
|
38
|
+
json (1.6.5)
|
39
|
+
mail (2.4.1)
|
41
40
|
i18n (>= 0.4.0)
|
42
41
|
mime-types (~> 1.16)
|
43
42
|
treetop (~> 1.4.8)
|
44
|
-
mime-types (1.
|
45
|
-
multi_json (1.0.
|
46
|
-
polyglot (0.3.
|
47
|
-
rack (1.
|
48
|
-
rack-cache (1.
|
43
|
+
mime-types (1.17.2)
|
44
|
+
multi_json (1.0.4)
|
45
|
+
polyglot (0.3.3)
|
46
|
+
rack (1.4.1)
|
47
|
+
rack-cache (1.1)
|
49
48
|
rack (>= 0.4)
|
50
|
-
rack-mount (0.8.3)
|
51
|
-
rack (>= 1.0.0)
|
52
49
|
rack-ssl (1.3.2)
|
53
50
|
rack
|
54
51
|
rack-test (0.6.1)
|
55
52
|
rack (>= 1.0)
|
56
|
-
rails (3.1
|
57
|
-
actionmailer (= 3.1
|
58
|
-
actionpack (= 3.1
|
59
|
-
activerecord (= 3.1
|
60
|
-
activeresource (= 3.1
|
61
|
-
activesupport (= 3.1
|
53
|
+
rails (3.2.1)
|
54
|
+
actionmailer (= 3.2.1)
|
55
|
+
actionpack (= 3.2.1)
|
56
|
+
activerecord (= 3.2.1)
|
57
|
+
activeresource (= 3.2.1)
|
58
|
+
activesupport (= 3.2.1)
|
62
59
|
bundler (~> 1.0)
|
63
|
-
railties (= 3.1
|
64
|
-
railties (3.1
|
65
|
-
actionpack (= 3.1
|
66
|
-
activesupport (= 3.1
|
60
|
+
railties (= 3.2.1)
|
61
|
+
railties (3.2.1)
|
62
|
+
actionpack (= 3.2.1)
|
63
|
+
activesupport (= 3.2.1)
|
67
64
|
rack-ssl (~> 1.3.2)
|
68
65
|
rake (>= 0.8.7)
|
69
66
|
rdoc (~> 3.4)
|
70
67
|
thor (~> 0.14.6)
|
71
|
-
rake (0.9.2)
|
72
|
-
rdoc (3.
|
73
|
-
|
74
|
-
|
75
|
-
rspec-
|
76
|
-
rspec-
|
77
|
-
|
78
|
-
rspec-
|
68
|
+
rake (0.9.2.2)
|
69
|
+
rdoc (3.12)
|
70
|
+
json (~> 1.4)
|
71
|
+
rspec (2.8.0)
|
72
|
+
rspec-core (~> 2.8.0)
|
73
|
+
rspec-expectations (~> 2.8.0)
|
74
|
+
rspec-mocks (~> 2.8.0)
|
75
|
+
rspec-core (2.8.0)
|
76
|
+
rspec-expectations (2.8.0)
|
79
77
|
diff-lcs (~> 1.1.2)
|
80
|
-
rspec-mocks (2.
|
81
|
-
sprockets (2.
|
78
|
+
rspec-mocks (2.8.0)
|
79
|
+
sprockets (2.1.2)
|
82
80
|
hike (~> 1.2)
|
83
81
|
rack (~> 1.0)
|
84
82
|
tilt (~> 1.1, != 1.3.0)
|
@@ -87,11 +85,11 @@ GEM
|
|
87
85
|
treetop (1.4.10)
|
88
86
|
polyglot
|
89
87
|
polyglot (>= 0.3.1)
|
90
|
-
tzinfo (0.3.
|
88
|
+
tzinfo (0.3.31)
|
91
89
|
|
92
90
|
PLATFORMS
|
93
91
|
ruby
|
94
92
|
|
95
93
|
DEPENDENCIES
|
96
|
-
rails (= 3.1
|
97
|
-
rspec (>= 2.
|
94
|
+
rails (= 3.2.1)
|
95
|
+
rspec (>= 2.8.0)
|
data/Rakefile
CHANGED
data/lib/i18n_routing.rb
CHANGED
File without changes
|
@@ -0,0 +1,435 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'journey'
|
3
|
+
require 'action_dispatch'
|
4
|
+
require 'active_support/core_ext/module'
|
5
|
+
|
6
|
+
module I18nRouting
|
7
|
+
module Mapper
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
# Just create a Mapper:Resource with given parameters
|
12
|
+
def resource_from_params(type, *resources)
|
13
|
+
res = resources.clone
|
14
|
+
|
15
|
+
options = res.extract_options!
|
16
|
+
r = res.first
|
17
|
+
|
18
|
+
type == :resource ? ActionDispatch::Routing::Mapper::SingletonResource.new(r, options.dup) : ActionDispatch::Routing::Mapper::Resource.new(r, options.dup)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Localize a resources or a resource
|
22
|
+
def localized_resources(type = :resources, *resources, &block)
|
23
|
+
localizable_route = nil
|
24
|
+
|
25
|
+
if @locales
|
26
|
+
res = resources.clone
|
27
|
+
|
28
|
+
options = res.extract_options!
|
29
|
+
r = res.first
|
30
|
+
|
31
|
+
resource = resource_from_params(type, r, options.dup)
|
32
|
+
|
33
|
+
# Check for translated resource
|
34
|
+
stored_locale = I18n.locale
|
35
|
+
@locales.each do |locale|
|
36
|
+
I18n.locale = locale
|
37
|
+
localized_path = I18nRouting.translation_for(resource.name, type)
|
38
|
+
|
39
|
+
# A translated route exists :
|
40
|
+
if !localized_path.blank? and String === localized_path
|
41
|
+
puts("[I18n] > localize %-10s: %40s (%s) => /%s" % [type, resource.name, locale, localized_path]) if @i18n_verbose
|
42
|
+
opts = options.dup
|
43
|
+
opts[:path] = localized_path
|
44
|
+
opts[:controller] ||= r.to_s.pluralize
|
45
|
+
|
46
|
+
resource = resource_from_params(type, r, opts.dup)
|
47
|
+
|
48
|
+
res = ["#{I18nRouting.locale_escaped(locale)}_#{r}".to_sym, opts]
|
49
|
+
|
50
|
+
constraints = opts[:constraints] ? opts[:constraints].dup : {}
|
51
|
+
constraints[:i18n_locale] = locale.to_s
|
52
|
+
|
53
|
+
scope(:constraints => constraints, :path_names => I18nRouting.path_names(resource.name, @scope)) do
|
54
|
+
localized_branch(locale) do
|
55
|
+
send(type, *res) do
|
56
|
+
|
57
|
+
# In the resource(s) block, we need to keep and restore some context :
|
58
|
+
if block
|
59
|
+
old_name = @scope[:i18n_real_resource_name]
|
60
|
+
old = @scope[:scope_level_resource]
|
61
|
+
|
62
|
+
@scope[:i18n_real_resource_name] = resource.name
|
63
|
+
@scope[:i18n_scope_level_resource] = old
|
64
|
+
@scope[:scope_level_resource] = resource
|
65
|
+
|
66
|
+
if type == :resource and @scope[:name_prefix]
|
67
|
+
# Need to fake name_prefix for singleton resource
|
68
|
+
@scope[:name_prefix] = @scope[:name_prefix].gsub(Regexp.new("#{old.name}$"), resource.name)
|
69
|
+
end
|
70
|
+
|
71
|
+
block.call if block
|
72
|
+
|
73
|
+
@scope[:scope_level_resource] = old
|
74
|
+
@scope[:i18n_real_resource_name] = old_name
|
75
|
+
end
|
76
|
+
|
77
|
+
@scope[:i18n_scope_level_resource] = nil
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
localizable_route = resource
|
84
|
+
end
|
85
|
+
end
|
86
|
+
I18n.locale = stored_locale
|
87
|
+
end
|
88
|
+
return localizable_route
|
89
|
+
end
|
90
|
+
|
91
|
+
# Set if the next route created will be a localized route or not
|
92
|
+
# If yes, localizable is a name, or a Mapper::Resource
|
93
|
+
# Can take a block, if so, save the current context, set the new
|
94
|
+
# Call the block, then restore the old context and return the block return
|
95
|
+
def set_localizable_route(localizable)
|
96
|
+
if block_given?
|
97
|
+
old = @set.named_routes.localizable
|
98
|
+
@set.named_routes.set_localizable_route(localizable)
|
99
|
+
r = yield
|
100
|
+
@set.named_routes.set_localizable_route(old)
|
101
|
+
return r
|
102
|
+
else
|
103
|
+
@set.named_routes.set_localizable_route(localizable)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def localizable_route
|
108
|
+
@set.named_routes.localizable
|
109
|
+
end
|
110
|
+
|
111
|
+
# Return the aproximate deep in scope level
|
112
|
+
def nested_deep
|
113
|
+
(@scope and Array === @scope[:nested_deep] and @scope[:scope_level]) ? @scope[:nested_deep].size : 0
|
114
|
+
end
|
115
|
+
|
116
|
+
public
|
117
|
+
|
118
|
+
# On Routing::Mapper initialization (when doing Application.routes.draw do ...)
|
119
|
+
# prepare routing system to be i18n ready
|
120
|
+
def initialize_with_i18n_routing(*args)
|
121
|
+
initialize_without_i18n_routing(*args)
|
122
|
+
|
123
|
+
# Add i18n_locale as valid conditions for Rack::Mount / And add also :locale, as Rails 3.0.4 removed it ...
|
124
|
+
@valid_conditions = @set.instance_eval { @set }.instance_eval { @valid_conditions }
|
125
|
+
[:i18n_locale, :locale].each do |k|
|
126
|
+
@set.valid_conditions[k] = true if !@set.valid_conditions.has_key?(k)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Extends the current RouteSet in order to define localized helper for named routes
|
130
|
+
# When calling define_url_helper, it calls define_localized_url_helper too.
|
131
|
+
if !@set.named_routes.respond_to?(:define_localized_url_helper)
|
132
|
+
@set.named_routes.class_eval <<-END_EVAL, __FILE__, __LINE__ + 1
|
133
|
+
alias_method :localized_define_url_helper, :define_url_helper
|
134
|
+
def define_url_helper(route, name, kind, options)
|
135
|
+
localized_define_url_helper(route, name, kind, options)
|
136
|
+
define_localized_url_helper(route, name, kind, options)
|
137
|
+
end
|
138
|
+
END_EVAL
|
139
|
+
|
140
|
+
@set.named_routes.extend I18nRouting::NamedRouteCollection
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Rails 3 routing system
|
145
|
+
# Create a block for localized routes, in your routes.rb :
|
146
|
+
#
|
147
|
+
# localized do
|
148
|
+
# resources :users
|
149
|
+
# match 'about' => 'contents#about', :as => :about
|
150
|
+
# end
|
151
|
+
#
|
152
|
+
def localized(locales = I18n.available_locales, opts = {})
|
153
|
+
# Add if not added Rails.root/config/locales/*.yml in the I18n.load_path
|
154
|
+
if !@i18n_routing_path_set and defined?(Rails) and Rails.respond_to?(:root) and Rails.root
|
155
|
+
I18n.load_path = (I18n.load_path << Dir[Rails.root.join('config', 'locales', '*.yml')]).flatten.uniq
|
156
|
+
@i18n_routing_path_set = true
|
157
|
+
end
|
158
|
+
|
159
|
+
old_value = @locales
|
160
|
+
@locales = locales
|
161
|
+
@i18n_verbose ||= opts.delete(:verbose)
|
162
|
+
yield
|
163
|
+
ensure
|
164
|
+
@locales = old_value
|
165
|
+
end
|
166
|
+
|
167
|
+
# Create a branch for create routes in the specified locale
|
168
|
+
def localized_branch(locale)
|
169
|
+
set_localizable_route(nil) do
|
170
|
+
old = @localized_branch
|
171
|
+
@localized_branch = locale
|
172
|
+
localized([locale]) do
|
173
|
+
yield
|
174
|
+
end
|
175
|
+
@localized_branch = old
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
# Set we do not want to localize next resource
|
180
|
+
def skip_localization
|
181
|
+
old = @skip_localization
|
182
|
+
@skip_localization = @localized_branch ? nil : true
|
183
|
+
yield
|
184
|
+
@skip_localization = old
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
# Overrides match method
|
190
|
+
# Two variants of match are supported now
|
191
|
+
# match 'about' => 'pages#about', :as => 'about'
|
192
|
+
# match 'about', :to => 'pages#about'
|
193
|
+
# match 'home', :to => 'pages#home', :as => 'main' # Route name overridden to 'main'
|
194
|
+
def match(*args)
|
195
|
+
# Localize simple match only if there is no resource scope.
|
196
|
+
if @locales and (args.size >= 1) and !parent_resource and
|
197
|
+
args.last.is_a?(Hash) # In both variants Hash should be present as last parameter
|
198
|
+
|
199
|
+
# Make deep copy of last argument (Hash)
|
200
|
+
options = Marshal.load(Marshal.dump(args.last)) # Dump is dirty but how to make deep cloning easily ? :/
|
201
|
+
|
202
|
+
# Now handle parameters
|
203
|
+
if (args.first.is_a?(Hash))
|
204
|
+
path, to = options.find { |name, value| name.is_a?(String) }
|
205
|
+
options.merge!(:to => to).delete(path)
|
206
|
+
else
|
207
|
+
path = args[0]
|
208
|
+
options[:as] ||= path
|
209
|
+
end
|
210
|
+
|
211
|
+
if (options[:as])
|
212
|
+
@locales.each do |locale|
|
213
|
+
mapping = LocalizedMapping.new(locale, @set, @scope, path, options)
|
214
|
+
if mapping.localizable?
|
215
|
+
puts("[I18n] > localize %-10s: %40s (%s) => %s" % ['route', options[:as], locale, mapping.path]) if @i18n_verbose
|
216
|
+
@set.add_route(*mapping.to_route)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
# Now, create the real match :
|
221
|
+
return set_localizable_route(options[:as]) do
|
222
|
+
super
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
super
|
228
|
+
end
|
229
|
+
|
230
|
+
def create_globalized_resources(type, *resources, &block)
|
231
|
+
#puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})"
|
232
|
+
|
233
|
+
@scope[:nested_deep] ||= []
|
234
|
+
@scope[:nested_deep] << 1
|
235
|
+
|
236
|
+
cur_scope = nil
|
237
|
+
if @locales
|
238
|
+
localized = localized_resources(type, *resources, &block) if !@skip_localization
|
239
|
+
|
240
|
+
## We do not translate if we are in a translation branch :
|
241
|
+
return if localized and nested_deep > 0
|
242
|
+
|
243
|
+
# Set the current standard resource in order to customize url helper :
|
244
|
+
if !@localized_branch
|
245
|
+
r = resource_from_params(type, *resources)
|
246
|
+
cur_scope = (parent_resource and parent_resource.name == r.name) ? parent_resource : r
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
set_localizable_route(cur_scope) do
|
251
|
+
skip_localization do
|
252
|
+
#puts "#{' ' * nested_deep} \\- Call original #{type} : for #{resources.inspect}}"
|
253
|
+
send("#{type}_without_i18n_routing".to_sym, *resources, &block)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
@scope[:nested_deep].pop
|
258
|
+
end
|
259
|
+
|
260
|
+
# Alias methods in order to handle i18n routes
|
261
|
+
def self.included(mod)
|
262
|
+
mod.send :alias_method_chain, :initialize, :i18n_routing
|
263
|
+
mod.send :alias_method_chain, :resource, :i18n_routing
|
264
|
+
mod.send :alias_method_chain, :resources, :i18n_routing
|
265
|
+
|
266
|
+
# Here we redefine some methods, in order to handle
|
267
|
+
# correct path_names translation on the fly
|
268
|
+
[:map_method, :member, :collection].each do |m|
|
269
|
+
rfname = "#{m}_without_i18n_routing".to_sym
|
270
|
+
mod.send :define_method, "#{m}_with_i18n_routing".to_sym do |*args, &block|
|
271
|
+
if @localized_branch and @scope[:i18n_scope_level_resource] and @scope[:i18n_real_resource_name]
|
272
|
+
o = @scope[:scope_level_resource]
|
273
|
+
@scope[:scope_level_resource] = @scope[:i18n_scope_level_resource]
|
274
|
+
|
275
|
+
pname = @scope[:path_names] || {}
|
276
|
+
i = 1
|
277
|
+
while i < args.size and (String === args[i] or Symbol === args[i])
|
278
|
+
pname[args[i]] = args[i]
|
279
|
+
i += 1
|
280
|
+
end
|
281
|
+
scope(:path_names => I18nRouting.path_names(@scope[:i18n_real_resource_name], {:path_names => pname})) do
|
282
|
+
send(rfname, *args, &block)
|
283
|
+
end
|
284
|
+
@scope[:scope_level_resource] = o
|
285
|
+
return
|
286
|
+
end
|
287
|
+
|
288
|
+
send(rfname, *args, &block)
|
289
|
+
end
|
290
|
+
|
291
|
+
mod.send :alias_method_chain, m, :i18n_routing
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
def resource_with_i18n_routing(*resources, &block)
|
296
|
+
create_globalized_resources(:resource, *resources, &block)
|
297
|
+
end
|
298
|
+
|
299
|
+
def resources_with_i18n_routing(*resources, &block)
|
300
|
+
create_globalized_resources(:resources, *resources, &block)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
# Used for localize simple named routes
|
305
|
+
class LocalizedMapping < ActionDispatch::Routing::Mapper::Mapping
|
306
|
+
attr_reader :path
|
307
|
+
|
308
|
+
def initialize(locale, set, scope, path, options)
|
309
|
+
super(set, scope, path.clone, options ? options.clone : nil)
|
310
|
+
stored_locale = I18n.locale
|
311
|
+
|
312
|
+
# try to get translated path :
|
313
|
+
I18n.locale = locale
|
314
|
+
ts = @path.gsub(/^\//, '')
|
315
|
+
ts.gsub!('(.:format)', '')
|
316
|
+
|
317
|
+
tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||
|
318
|
+
!ts.blank? && I18nRouting.translation_for(ts, :named_routes_path) || ts
|
319
|
+
|
320
|
+
localized_scope = I18nRouting.translation_for(@scope[:path].gsub(/\//, ''), :scopes) if @scope[:path]
|
321
|
+
path = localized_scope ? '/' << localized_scope : @scope[:path]
|
322
|
+
@localized_path = File.join((path || ''), tp).gsub(/\/$/, '')
|
323
|
+
|
324
|
+
# If a translated path exists, set localized infos
|
325
|
+
if !@localized_path.blank? and @localized_path != @path
|
326
|
+
#@options[:controller] ||= @options[:as]
|
327
|
+
@options[:as] = "#{I18nRouting.locale_escaped(locale)}_#{@options[:as]}"
|
328
|
+
@path = @localized_path
|
329
|
+
@options[:constraints] = @options[:constraints] ? @options[:constraints].dup : {}
|
330
|
+
@options[:constraints][:i18n_locale] = locale.to_s
|
331
|
+
@options[:anchor] = true
|
332
|
+
# Force the recomputation of the requirements with the new values
|
333
|
+
@requirements = nil
|
334
|
+
else
|
335
|
+
@localized_path = nil
|
336
|
+
end
|
337
|
+
I18n.locale = stored_locale
|
338
|
+
end
|
339
|
+
|
340
|
+
# Return true if this route is localizable
|
341
|
+
def localizable?
|
342
|
+
@localized_path != nil
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
module NamedRouteCollection
|
347
|
+
attr_reader :localizable
|
348
|
+
|
349
|
+
def set_localizable_route(localizable)
|
350
|
+
@localizable = localizable
|
351
|
+
end
|
352
|
+
|
353
|
+
# Alias named route helper in order to check if a localized helper exists
|
354
|
+
# If not use the standard one.
|
355
|
+
def define_localized_url_helper(route, name, kind, options)
|
356
|
+
if n = localizable
|
357
|
+
selector = url_helper_name(name, kind)
|
358
|
+
|
359
|
+
rlang = if n.kind_of?(ActionDispatch::Routing::Mapper::Resources::Resource) and i = name.to_s.rindex("_#{n.plural}")
|
360
|
+
"#{selector.to_s[0, i]}_glang_#{n.plural}#{selector.to_s[i + "_#{n.plural}".size, selector.to_s.size]}"
|
361
|
+
elsif n.kind_of?(ActionDispatch::Routing::Mapper::Resources::Resource) and i = name.to_s.rindex("_#{n.singular}")
|
362
|
+
"#{selector.to_s[0, i]}_glang_#{n.singular}#{selector.to_s[i + "_#{n.singular}".size, selector.to_s.size]}"
|
363
|
+
else
|
364
|
+
"glang_#{selector}"
|
365
|
+
end
|
366
|
+
|
367
|
+
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
|
368
|
+
alias_method :localized_#{selector}, :#{selector}
|
369
|
+
|
370
|
+
def #{selector}(*args)
|
371
|
+
selector_g = '#{rlang}'.gsub('glang', I18nRouting.locale_escaped(I18n.locale.to_s)).to_sym
|
372
|
+
|
373
|
+
#puts "Call routes : #{selector} => \#{selector_g} (\#{I18n.locale}) "
|
374
|
+
if respond_to? selector_g and selector_g != :#{selector}
|
375
|
+
send(selector_g, *args)
|
376
|
+
else
|
377
|
+
localized_#{selector}(*args)
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
end_eval
|
382
|
+
|
383
|
+
end
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
# Journey::Route module
|
388
|
+
# Exists in order to use appropriate localized route when using url_for
|
389
|
+
module JourneyRoute
|
390
|
+
# Alias methods in order to handle i18n routes
|
391
|
+
def self.included(mod)
|
392
|
+
mod.send :alias_method_chain, :format, :i18n_routing
|
393
|
+
mod.send :alias_method_chain, :initialize, :i18n_routing
|
394
|
+
end
|
395
|
+
|
396
|
+
# During route initialization, if a condition i18n_locale is present
|
397
|
+
# Delete it, store it in @locale, and add it to @defaults
|
398
|
+
def initialize_with_i18n_routing(name, app, path, constraints, defaults = {})
|
399
|
+
@locale = if constraints.key?(:i18n_locale)
|
400
|
+
c = defaults[:i18n_locale] = constraints.delete(:i18n_locale)
|
401
|
+
# In rails 3.0 it's a regexp otherwise it's a string, so we need to call source on the regexp
|
402
|
+
(c.respond_to?(:source) ? c.source : c).to_sym
|
403
|
+
else
|
404
|
+
nil
|
405
|
+
end
|
406
|
+
initialize_without_i18n_routing(name, app, path, constraints, defaults)
|
407
|
+
end
|
408
|
+
|
409
|
+
# Called for dynamic route generation
|
410
|
+
# If a @locale is present and if this locale is not the current one
|
411
|
+
# => return nil and refuse to generate the route
|
412
|
+
def format_with_i18n_routing(path_options)
|
413
|
+
return nil if @locale and @locale != I18n.locale.to_sym
|
414
|
+
format_without_i18n_routing(path_options)
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
|
419
|
+
# Journey::Formatter module
|
420
|
+
# Exists in order to inject the current locale in the routing options
|
421
|
+
module JourneyFormatter
|
422
|
+
def self.included(mod)
|
423
|
+
mod.send :alias_method_chain, :non_recursive, :i18n_routing
|
424
|
+
end
|
425
|
+
|
426
|
+
def non_recursive_with_i18n_routing(cache, options)
|
427
|
+
options << [:i18n_locale, I18n.locale.to_s]
|
428
|
+
non_recursive_without_i18n_routing(cache, options)
|
429
|
+
end
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
ActionDispatch::Routing::Mapper.send :include, I18nRouting::Mapper
|
434
|
+
Journey::Route.send :include, I18nRouting::JourneyRoute
|
435
|
+
Journey::Formatter.send :include, I18nRouting::JourneyFormatter
|
data/spec/spec_helper.rb
CHANGED
@@ -28,7 +28,11 @@ else
|
|
28
28
|
gem 'actionpack', '> 2.9'
|
29
29
|
require 'action_controller'
|
30
30
|
require 'action_dispatch'
|
31
|
-
|
31
|
+
if Rails.version < '3.2'
|
32
|
+
require 'rack/mount'
|
33
|
+
else
|
34
|
+
require 'journey'
|
35
|
+
end
|
32
36
|
require 'active_support/core_ext/hash/slice.rb'
|
33
37
|
end
|
34
38
|
|
metadata
CHANGED
@@ -1,100 +1,78 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n_routing
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 5
|
9
|
-
- 0
|
10
|
-
version: 0.5.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Guillaume Luccisano
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-01-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: i18n
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &80783030 !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 25
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
- 3
|
33
|
-
- 5
|
18
|
+
requirements:
|
19
|
+
- - ! '>'
|
20
|
+
- !ruby/object:Gem::Version
|
34
21
|
version: 0.3.5
|
35
22
|
type: :runtime
|
36
|
-
|
37
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *80783030
|
25
|
+
description: I18n_routing is a plugin for Ruby on Rails that lets you easily translate
|
26
|
+
your routes trough the I18n api included in Rails since version 2.2
|
38
27
|
email: guillaume.luccisano@gmail.com
|
39
28
|
executables: []
|
40
|
-
|
41
29
|
extensions: []
|
42
|
-
|
43
30
|
extra_rdoc_files: []
|
44
|
-
|
45
|
-
files:
|
31
|
+
files:
|
46
32
|
- lib/i18n_routing.rb
|
47
|
-
- lib/
|
33
|
+
- lib/i18n_routing_rails32.rb
|
34
|
+
- lib/i18n_routing_rails31.rb
|
48
35
|
- lib/i18n_routing_rails2.rb
|
49
|
-
- lib/
|
50
|
-
- spec/i18n_routing/i18n_spec.rb
|
51
|
-
- spec/locales/de.yml
|
52
|
-
- spec/locales/en.yml
|
53
|
-
- spec/locales/fr.yml
|
36
|
+
- lib/i18n_routing_common.rb
|
54
37
|
- spec/locales/pt-br.yml
|
38
|
+
- spec/locales/fr.yml
|
39
|
+
- spec/locales/en.yml
|
40
|
+
- spec/locales/de.yml
|
41
|
+
- spec/i18n_routing/i18n_spec.rb
|
55
42
|
- spec/spec_helper.rb
|
56
|
-
- CHANGELOG.rdoc
|
57
43
|
- Gemfile
|
58
44
|
- Gemfile.lock
|
59
45
|
- MIT-LICENSE
|
60
|
-
- Rakefile
|
61
46
|
- README.rdoc
|
47
|
+
- CHANGELOG.rdoc
|
48
|
+
- Rakefile
|
62
49
|
- init.rb
|
63
|
-
has_rdoc: true
|
64
50
|
homepage: http://github.com/kwi/i18n_routing
|
65
51
|
licenses: []
|
66
|
-
|
67
52
|
post_install_message:
|
68
53
|
rdoc_options: []
|
69
|
-
|
70
|
-
require_paths:
|
54
|
+
require_paths:
|
71
55
|
- lib
|
72
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
57
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
|
78
|
-
segments:
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
segments:
|
79
63
|
- 0
|
80
|
-
|
81
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
hash: 647656513
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
66
|
none: false
|
83
|
-
requirements:
|
84
|
-
- -
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
hash: 19
|
87
|
-
segments:
|
88
|
-
- 1
|
89
|
-
- 3
|
90
|
-
- 4
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
91
70
|
version: 1.3.4
|
92
71
|
requirements: []
|
93
|
-
|
94
72
|
rubyforge_project: i18n_routing
|
95
|
-
rubygems_version: 1.
|
73
|
+
rubygems_version: 1.8.10
|
96
74
|
signing_key:
|
97
75
|
specification_version: 3
|
98
|
-
summary: I18n routing module for Rails 2.3.x and Rails 3. Translate your routes with
|
76
|
+
summary: I18n routing module for Rails 2.3.x and Rails 3. Translate your routes with
|
77
|
+
ease !
|
99
78
|
test_files: []
|
100
|
-
|