Skip to content

Commit ea84364

Browse files
fix for url_for failing with format
1 parent db56721 commit ea84364

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

lib/i18n_routing_rails31.rb

+24-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module I18nRouting
77
module Mapper
88

99
private
10-
10+
1111
# Just create a Mapper:Resource with given parameters
1212
def resource_from_params(type, *resources)
1313
res = resources.clone
@@ -17,7 +17,7 @@ def resource_from_params(type, *resources)
1717

1818
type == :resource ? ActionDispatch::Routing::Mapper::SingletonResource.new(r, options.dup) : ActionDispatch::Routing::Mapper::Resource.new(r, options.dup)
1919
end
20-
20+
2121
# Localize a resources or a resource
2222
def localized_resources(type = :resources, *resources, &block)
2323
localizable_route = nil
@@ -42,7 +42,7 @@ def localized_resources(type = :resources, *resources, &block)
4242
opts = options.dup
4343
opts[:path] = localized_path
4444
opts[:controller] ||= r.to_s.pluralize
45-
45+
4646
resource = resource_from_params(type, r, opts.dup)
4747

4848
res = ["#{I18nRouting.locale_escaped(locale)}_#{r}".to_sym, opts]
@@ -53,7 +53,7 @@ def localized_resources(type = :resources, *resources, &block)
5353
scope(:constraints => constraints, :path_names => I18nRouting.path_names(resource.name, @scope)) do
5454
localized_branch(locale) do
5555
send(type, *res) do
56-
56+
5757
# In the resource(s) block, we need to keep and restore some context :
5858
if block
5959
old_name = @scope[:i18n_real_resource_name]
@@ -92,7 +92,7 @@ def localized_resources(type = :resources, *resources, &block)
9292
# If yes, localizable is a name, or a Mapper::Resource
9393
# Can take a block, if so, save the current context, set the new
9494
# 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)
9696
if block_given?
9797
old = @set.named_routes.localizable
9898
@set.named_routes.set_localizable_route(localizable)
@@ -103,11 +103,11 @@ def set_localizable_route(localizable)
103103
@set.named_routes.set_localizable_route(localizable)
104104
end
105105
end
106-
106+
107107
def localizable_route
108108
@set.named_routes.localizable
109109
end
110-
110+
111111
# Return the aproximate deep in scope level
112112
def nested_deep
113113
(@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 = {})
156156
I18n.load_path = (I18n.load_path << Dir[Rails.root.join('config', 'locales', '*.yml')]).flatten.uniq
157157
@i18n_routing_path_set = true
158158
end
159-
159+
160160
old_value = @locales
161161
@locales = locales
162162
@i18n_verbose ||= opts.delete(:verbose)
163163
yield
164164
ensure
165165
@locales = old_value
166166
end
167-
167+
168168
# Create a branch for create routes in the specified locale
169169
def localized_branch(locale)
170170
set_localizable_route(nil) do
@@ -176,7 +176,7 @@ def localized_branch(locale)
176176
@localized_branch = old
177177
end
178178
end
179-
179+
180180
# Set we do not want to localize next resource
181181
def skip_localization
182182
old = @skip_localization
@@ -227,7 +227,7 @@ def match(*args)
227227

228228
super
229229
end
230-
230+
231231
def create_globalized_resources(type, *resources, &block)
232232
#puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})"
233233

@@ -254,16 +254,16 @@ def create_globalized_resources(type, *resources, &block)
254254
send("#{type}_without_i18n_routing".to_sym, *resources, &block)
255255
end
256256
end
257-
257+
258258
@scope[:nested_deep].pop
259259
end
260-
260+
261261
# Alias methods in order to handle i18n routes
262262
def self.included(mod)
263263
mod.send :alias_method_chain, :initialize, :i18n_routing
264264
mod.send :alias_method_chain, :resource, :i18n_routing
265265
mod.send :alias_method_chain, :resources, :i18n_routing
266-
266+
267267
# Here we redefine some methods, in order to handle
268268
# correct path_names translation on the fly
269269
[:map_method, :member, :collection].each do |m|
@@ -272,7 +272,7 @@ def self.included(mod)
272272
if @localized_branch and @scope[:i18n_scope_level_resource] and @scope[:i18n_real_resource_name]
273273
o = @scope[:scope_level_resource]
274274
@scope[:scope_level_resource] = @scope[:i18n_scope_level_resource]
275-
275+
276276
pname = @scope[:path_names] || {}
277277
i = 1
278278
while i < args.size and (String === args[i] or Symbol === args[i])
@@ -285,18 +285,18 @@ def self.included(mod)
285285
@scope[:scope_level_resource] = o
286286
return
287287
end
288-
288+
289289
send(rfname, *args, &block)
290290
end
291-
291+
292292
mod.send :alias_method_chain, m, :i18n_routing
293293
end
294294
end
295-
295+
296296
def resource_with_i18n_routing(*resources, &block)
297297
create_globalized_resources(:resource, *resources, &block)
298298
end
299-
299+
300300
def resources_with_i18n_routing(*resources, &block)
301301
create_globalized_resources(:resources, *resources, &block)
302302
end
@@ -313,11 +313,12 @@ def initialize(locale, set, scope, path, options)
313313
# try to get translated path :
314314
I18n.locale = locale
315315
ts = @path.gsub(/^\//, '')
316+
append_format = ts =~ /\(\.:format\)/
316317
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) ||
319320
!ts.blank? && I18nRouting.translation_for(ts, :named_routes_path) || ts
320-
321+
321322
localized_scope = I18nRouting.translation_for(@scope[:path].gsub(/\//, ''), :scopes) if @scope[:path]
322323
path = localized_scope ? '/' << localized_scope : @scope[:path]
323324
@localized_path = File.join((path || ''), tp).gsub(/\/$/, '')
@@ -327,6 +328,7 @@ def initialize(locale, set, scope, path, options)
327328
#@options[:controller] ||= @options[:as]
328329
@options[:as] = "#{I18nRouting.locale_escaped(locale)}_#{@options[:as]}"
329330
@path = @localized_path
331+
@path = "#{@path}(.:format)" if append_format
330332
@options[:constraints] = @options[:constraints] ? @options[:constraints].dup : {}
331333
@options[:constraints][:i18n_locale] = locale.to_s
332334
@options[:anchor] = true

lib/i18n_routing_rails32.rb

+24-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module I18nRouting
77
module Mapper
88

99
private
10-
10+
1111
# Just create a Mapper:Resource with given parameters
1212
def resource_from_params(type, *resources)
1313
res = resources.clone
@@ -17,7 +17,7 @@ def resource_from_params(type, *resources)
1717

1818
type == :resource ? ActionDispatch::Routing::Mapper::SingletonResource.new(r, options.dup) : ActionDispatch::Routing::Mapper::Resource.new(r, options.dup)
1919
end
20-
20+
2121
# Localize a resources or a resource
2222
def localized_resources(type = :resources, *resources, &block)
2323
localizable_route = nil
@@ -42,7 +42,7 @@ def localized_resources(type = :resources, *resources, &block)
4242
opts = options.dup
4343
opts[:path] = localized_path
4444
opts[:controller] ||= r.to_s.pluralize
45-
45+
4646
resource = resource_from_params(type, r, opts.dup)
4747

4848
res = ["#{I18nRouting.locale_escaped(locale)}_#{r}".to_sym, opts]
@@ -53,7 +53,7 @@ def localized_resources(type = :resources, *resources, &block)
5353
scope(:constraints => constraints, :path_names => I18nRouting.path_names(resource.name, @scope)) do
5454
localized_branch(locale) do
5555
send(type, *res) do
56-
56+
5757
# In the resource(s) block, we need to keep and restore some context :
5858
if block
5959
old_name = @scope[:i18n_real_resource_name]
@@ -92,7 +92,7 @@ def localized_resources(type = :resources, *resources, &block)
9292
# If yes, localizable is a name, or a Mapper::Resource
9393
# Can take a block, if so, save the current context, set the new
9494
# 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)
9696
if block_given?
9797
old = @set.named_routes.localizable
9898
@set.named_routes.set_localizable_route(localizable)
@@ -103,11 +103,11 @@ def set_localizable_route(localizable)
103103
@set.named_routes.set_localizable_route(localizable)
104104
end
105105
end
106-
106+
107107
def localizable_route
108108
@set.named_routes.localizable
109109
end
110-
110+
111111
# Return the aproximate deep in scope level
112112
def nested_deep
113113
(@scope and Array === @scope[:nested_deep] and @scope[:scope_level]) ? @scope[:nested_deep].size : 0
@@ -155,15 +155,15 @@ def localized(locales = I18n.available_locales, opts = {})
155155
I18n.load_path = (I18n.load_path << Dir[Rails.root.join('config', 'locales', '*.yml')]).flatten.uniq
156156
@i18n_routing_path_set = true
157157
end
158-
158+
159159
old_value = @locales
160160
@locales = locales
161161
@i18n_verbose ||= opts.delete(:verbose)
162162
yield
163163
ensure
164164
@locales = old_value
165165
end
166-
166+
167167
# Create a branch for create routes in the specified locale
168168
def localized_branch(locale)
169169
set_localizable_route(nil) do
@@ -175,7 +175,7 @@ def localized_branch(locale)
175175
@localized_branch = old
176176
end
177177
end
178-
178+
179179
# Set we do not want to localize next resource
180180
def skip_localization
181181
old = @skip_localization
@@ -226,7 +226,7 @@ def match(*args)
226226

227227
super
228228
end
229-
229+
230230
def create_globalized_resources(type, *resources, &block)
231231
#puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})"
232232

@@ -253,16 +253,16 @@ def create_globalized_resources(type, *resources, &block)
253253
send("#{type}_without_i18n_routing".to_sym, *resources, &block)
254254
end
255255
end
256-
256+
257257
@scope[:nested_deep].pop
258258
end
259-
259+
260260
# Alias methods in order to handle i18n routes
261261
def self.included(mod)
262262
mod.send :alias_method_chain, :initialize, :i18n_routing
263263
mod.send :alias_method_chain, :resource, :i18n_routing
264264
mod.send :alias_method_chain, :resources, :i18n_routing
265-
265+
266266
# Here we redefine some methods, in order to handle
267267
# correct path_names translation on the fly
268268
[:map_method, :member, :collection].each do |m|
@@ -271,7 +271,7 @@ def self.included(mod)
271271
if @localized_branch and @scope[:i18n_scope_level_resource] and @scope[:i18n_real_resource_name]
272272
o = @scope[:scope_level_resource]
273273
@scope[:scope_level_resource] = @scope[:i18n_scope_level_resource]
274-
274+
275275
pname = @scope[:path_names] || {}
276276
i = 1
277277
while i < args.size and (String === args[i] or Symbol === args[i])
@@ -284,18 +284,18 @@ def self.included(mod)
284284
@scope[:scope_level_resource] = o
285285
return
286286
end
287-
287+
288288
send(rfname, *args, &block)
289289
end
290-
290+
291291
mod.send :alias_method_chain, m, :i18n_routing
292292
end
293293
end
294-
294+
295295
def resource_with_i18n_routing(*resources, &block)
296296
create_globalized_resources(:resource, *resources, &block)
297297
end
298-
298+
299299
def resources_with_i18n_routing(*resources, &block)
300300
create_globalized_resources(:resources, *resources, &block)
301301
end
@@ -312,11 +312,12 @@ def initialize(locale, set, scope, path, options)
312312
# try to get translated path :
313313
I18n.locale = locale
314314
ts = @path.gsub(/^\//, '')
315+
append_format = ts =~ /\(\.:format\)/
315316
ts.gsub!('(.:format)', '')
316-
317-
tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||
317+
318+
tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||
318319
!ts.blank? && I18nRouting.translation_for(ts, :named_routes_path) || ts
319-
320+
320321
localized_scope = I18nRouting.translation_for(@scope[:path].gsub(/\//, ''), :scopes) if @scope[:path]
321322
path = localized_scope ? '/' << localized_scope : @scope[:path]
322323
@localized_path = File.join((path || ''), tp).gsub(/\/$/, '')
@@ -326,6 +327,7 @@ def initialize(locale, set, scope, path, options)
326327
#@options[:controller] ||= @options[:as]
327328
@options[:as] = "#{I18nRouting.locale_escaped(locale)}_#{@options[:as]}"
328329
@path = @localized_path
330+
@path = "#{@path}(.:format)" if append_format
329331
@options[:constraints] = @options[:constraints] ? @options[:constraints].dup : {}
330332
@options[:constraints][:i18n_locale] = locale.to_s
331333
@options[:anchor] = true

0 commit comments

Comments
 (0)