@@ -7,7 +7,7 @@ module I18nRouting
7
7
module Mapper
8
8
9
9
private
10
-
10
+
11
11
# Just create a Mapper:Resource with given parameters
12
12
def resource_from_params ( type , *resources )
13
13
res = resources . clone
@@ -17,7 +17,7 @@ def resource_from_params(type, *resources)
17
17
18
18
type == :resource ? ActionDispatch ::Routing ::Mapper ::SingletonResource . new ( r , options . dup ) : ActionDispatch ::Routing ::Mapper ::Resource . new ( r , options . dup )
19
19
end
20
-
20
+
21
21
# Localize a resources or a resource
22
22
def localized_resources ( type = :resources , *resources , &block )
23
23
localizable_route = nil
@@ -42,7 +42,7 @@ def localized_resources(type = :resources, *resources, &block)
42
42
opts = options . dup
43
43
opts [ :path ] = localized_path
44
44
opts [ :controller ] ||= r . to_s . pluralize
45
-
45
+
46
46
resource = resource_from_params ( type , r , opts . dup )
47
47
48
48
res = [ "#{ I18nRouting . locale_escaped ( locale ) } _#{ r } " . to_sym , opts ]
@@ -53,7 +53,7 @@ def localized_resources(type = :resources, *resources, &block)
53
53
scope ( :constraints => constraints , :path_names => I18nRouting . path_names ( resource . name , @scope ) ) do
54
54
localized_branch ( locale ) do
55
55
send ( type , *res ) do
56
-
56
+
57
57
# In the resource(s) block, we need to keep and restore some context :
58
58
if block
59
59
old_name = @scope [ :i18n_real_resource_name ]
@@ -92,7 +92,7 @@ def localized_resources(type = :resources, *resources, &block)
92
92
# If yes, localizable is a name, or a Mapper::Resource
93
93
# Can take a block, if so, save the current context, set the new
94
94
# Call the block, then restore the old context and return the block return
95
- def set_localizable_route ( localizable )
95
+ def set_localizable_route ( localizable )
96
96
if block_given?
97
97
old = @set . named_routes . localizable
98
98
@set . named_routes . set_localizable_route ( localizable )
@@ -103,11 +103,11 @@ def set_localizable_route(localizable)
103
103
@set . named_routes . set_localizable_route ( localizable )
104
104
end
105
105
end
106
-
106
+
107
107
def localizable_route
108
108
@set . named_routes . localizable
109
109
end
110
-
110
+
111
111
# Return the aproximate deep in scope level
112
112
def nested_deep
113
113
( @scope and Array === @scope [ :nested_deep ] and @scope [ :scope_level ] ) ? @scope [ :nested_deep ] . size : 0
@@ -156,15 +156,15 @@ def localized(locales = I18n.available_locales, opts = {})
156
156
I18n . load_path = ( I18n . load_path << Dir [ Rails . root . join ( 'config' , 'locales' , '*.yml' ) ] ) . flatten . uniq
157
157
@i18n_routing_path_set = true
158
158
end
159
-
159
+
160
160
old_value = @locales
161
161
@locales = locales
162
162
@i18n_verbose ||= opts . delete ( :verbose )
163
163
yield
164
164
ensure
165
165
@locales = old_value
166
166
end
167
-
167
+
168
168
# Create a branch for create routes in the specified locale
169
169
def localized_branch ( locale )
170
170
set_localizable_route ( nil ) do
@@ -176,7 +176,7 @@ def localized_branch(locale)
176
176
@localized_branch = old
177
177
end
178
178
end
179
-
179
+
180
180
# Set we do not want to localize next resource
181
181
def skip_localization
182
182
old = @skip_localization
@@ -227,7 +227,7 @@ def match(*args)
227
227
228
228
super
229
229
end
230
-
230
+
231
231
def create_globalized_resources ( type , *resources , &block )
232
232
#puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})"
233
233
@@ -254,16 +254,16 @@ def create_globalized_resources(type, *resources, &block)
254
254
send ( "#{ type } _without_i18n_routing" . to_sym , *resources , &block )
255
255
end
256
256
end
257
-
257
+
258
258
@scope [ :nested_deep ] . pop
259
259
end
260
-
260
+
261
261
# Alias methods in order to handle i18n routes
262
262
def self . included ( mod )
263
263
mod . send :alias_method_chain , :initialize , :i18n_routing
264
264
mod . send :alias_method_chain , :resource , :i18n_routing
265
265
mod . send :alias_method_chain , :resources , :i18n_routing
266
-
266
+
267
267
# Here we redefine some methods, in order to handle
268
268
# correct path_names translation on the fly
269
269
[ :map_method , :member , :collection ] . each do |m |
@@ -272,7 +272,7 @@ def self.included(mod)
272
272
if @localized_branch and @scope [ :i18n_scope_level_resource ] and @scope [ :i18n_real_resource_name ]
273
273
o = @scope [ :scope_level_resource ]
274
274
@scope [ :scope_level_resource ] = @scope [ :i18n_scope_level_resource ]
275
-
275
+
276
276
pname = @scope [ :path_names ] || { }
277
277
i = 1
278
278
while i < args . size and ( String === args [ i ] or Symbol === args [ i ] )
@@ -285,18 +285,18 @@ def self.included(mod)
285
285
@scope [ :scope_level_resource ] = o
286
286
return
287
287
end
288
-
288
+
289
289
send ( rfname , *args , &block )
290
290
end
291
-
291
+
292
292
mod . send :alias_method_chain , m , :i18n_routing
293
293
end
294
294
end
295
-
295
+
296
296
def resource_with_i18n_routing ( *resources , &block )
297
297
create_globalized_resources ( :resource , *resources , &block )
298
298
end
299
-
299
+
300
300
def resources_with_i18n_routing ( *resources , &block )
301
301
create_globalized_resources ( :resources , *resources , &block )
302
302
end
@@ -313,11 +313,12 @@ def initialize(locale, set, scope, path, options)
313
313
# try to get translated path :
314
314
I18n . locale = locale
315
315
ts = @path . gsub ( /^\/ / , '' )
316
+ append_format = ts =~ /\( \. :format\) /
316
317
ts . gsub! ( '(.:format)' , '' )
317
-
318
- tp = @options [ :as ] && I18nRouting . translation_for ( @options [ :as ] , :named_routes ) ||
318
+
319
+ tp = @options [ :as ] && I18nRouting . translation_for ( @options [ :as ] , :named_routes ) ||
319
320
!ts . blank? && I18nRouting . translation_for ( ts , :named_routes_path ) || ts
320
-
321
+
321
322
localized_scope = I18nRouting . translation_for ( @scope [ :path ] . gsub ( /\/ / , '' ) , :scopes ) if @scope [ :path ]
322
323
path = localized_scope ? '/' << localized_scope : @scope [ :path ]
323
324
@localized_path = File . join ( ( path || '' ) , tp ) . gsub ( /\/ $/ , '' )
@@ -327,6 +328,7 @@ def initialize(locale, set, scope, path, options)
327
328
#@options[:controller] ||= @options[:as]
328
329
@options [ :as ] = "#{ I18nRouting . locale_escaped ( locale ) } _#{ @options [ :as ] } "
329
330
@path = @localized_path
331
+ @path = "#{ @path } (.:format)" if append_format
330
332
@options [ :constraints ] = @options [ :constraints ] ? @options [ :constraints ] . dup : { }
331
333
@options [ :constraints ] [ :i18n_locale ] = locale . to_s
332
334
@options [ :anchor ] = true
0 commit comments