Permalink
Browse files

First revision for rails2.2

  • Loading branch information...
1 parent e372553 commit a8f30ba5119af7804add6a4edddf0a4af306484d @raul committed Oct 24, 2008
Showing with 118 additions and 130 deletions.
  1. +59 −65 SampleApp/vendor/plugins/translate_routes/lib/translate_routes.rb
  2. +59 −65 lib/translate_routes.rb
@@ -1,3 +1,5 @@
+require 'ruby-debug'
+
# TranslateRoutes
module ActionController
@@ -64,44 +66,22 @@ def self.translate_current_routes(dictionaries)
old_routes.each do |old_route|
old_name = old_names.index(old_route)
-
# process and add the translated ones
trans_routes, trans_named_routes = translate_route(old_route, dictionaries, old_name)
new_routes.concat(trans_routes)
- # process the old route:
- new_old_route = clone_with_deeply_copied_static_segments(old_route) # we need a fresh route to apply requirements
+ # translate and add the old route:
+ new_old_route = translate_route_by_lang(old_route, default_lang, default_dict, old_name)
- translate_static_segments(new_old_route, default_dict)
- add_language_requirements(new_old_route, default_lang)
- add_language_segment(new_old_route, default_lang) if prefix_on_default_lang
new_routes << new_old_route
# if it's a named one we append the lang suffix and replace the old helper by a language-based call
if old_name
-
trans_named_routes["#{old_name}_#{default_lang}"] = new_old_route
trans_named_routes[old_name] = new_old_route # keep the old name to use the helper on integration tests
-
new_named_routes.merge! trans_named_routes
-
- ['path', 'url'].each do |suffix|
- new_helper_name = "#{old_name}_#{suffix}"
- def_new_helper = <<-DEF_NEW_HELPER
- def #{new_helper_name}(*args)
- if defined? @#{lang_param_key}
- send("#{old_name}_\#{@#{lang_param_key}}_#{suffix}", *args)
- else
- send("#{old_name}_#{default_lang}_#{suffix}", *args)
- end
- end
- DEF_NEW_HELPER
-
- [ActionController::Base, ActionView::Base].each { |d| d.module_eval(def_new_helper) }
- ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
- end
-
- end
+ generate_helpers(old_name)
+ end
end
@@ -111,69 +91,83 @@ def #{new_helper_name}(*args)
end
- def self.clone_with_deeply_copied_static_segments(route)
- copy = Route.new
- copy.segments = []
+ def self.generate_helpers(old_name)
+ ['path', 'url'].each do |suffix|
+ new_helper_name = "#{old_name}_#{suffix}"
+ def_new_helper = <<-DEF_NEW_HELPER
+ def #{new_helper_name}(*args)
+ if defined? @#{lang_param_key}
+ send("#{old_name}_\#{@#{lang_param_key}}_#{suffix}", *args)
+ else
+ send("#{old_name}_#{default_lang}_#{suffix}", *args)
+ end
+ end
+ DEF_NEW_HELPER
+
+ [ActionController::Base, ActionView::Base].each { |d| d.module_eval(def_new_helper) }
+ ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
+ end
+ end
+
+ def self.add_prefix?(lang)
+ @@prefix_on_default_lang || lang != @@default_lang
+ end
+
+ def self.translate_static_segments(route, dictionary)
+ segments = []
route.segments.each do |s|
-
if s.instance_of?(StaticSegment)
new_segment = StaticSegment.new
- new_segment.value = s.value.dup
+ new_segment.value = dictionary[s.value] || s.value.dup
new_segment.is_optional = s.is_optional
else
new_segment = s.dup # just reference the original
end
-
- copy.segments << new_segment
+ segments << new_segment
end
- copy.conditions = route.conditions.dup
- copy.requirements = route.requirements.dup
- copy
- end
-
- def self.add_language_requirements(route, lang)
- route.requirements[lang_param_key] = lang
+ segments
end
- def self.add_language_segment(route, lang)
-
+ def self.lang_segments(orig, lang, dictionary)
+ return translate_static_segments(orig, dictionary) unless add_prefix?(lang)
+
# divider ('/')
- new_divider_segment = DividerSegment.new
- new_divider_segment.value = route.segments.first.value
- new_divider_segment.is_optional = false # la prueba
+ divider = DividerSegment.new
+ divider.value = orig.segments.first.value
+ divider.is_optional = false # la prueba
# static ('es')
- new_static_segment = StaticSegment.new
- new_static_segment.value = lang
- new_static_segment.is_optional = false
+ static = StaticSegment.new
+ static.value = lang
+ static.is_optional = false
- route.segments = [new_divider_segment, new_static_segment] + route.segments
-
+ [divider, static] + translate_static_segments(orig, dictionary)
end
+ def self.lang_requirements(orig, lang)
+ orig.requirements.merge(@@lang_param_key => lang)
+ end
- def self.translate_static_segments(route, dictionary)
- route.segments.each do |segment|
- if segment.instance_of?(StaticSegment) && dictionary[segment.value]
- segment.value = dictionary[segment.value]
- end
- end
+ def self.translate_route_by_lang(orig, lang, dictionary, orig_name=nil)
+ segments = lang_segments(orig, lang, dictionary)
+ requirements = lang_requirements(orig, lang)
+ conditions = orig.conditions
+
+ r = Route.new
+ r.segments = segments
+ r.requirements = requirements
+ r.conditions = conditions
+ r
end
def self.translate_route(route, dictionaries, route_name = nil)
new_routes = []
new_named_routes = {}
dictionaries.keys.each do |lang|
-
- copy = clone_with_deeply_copied_static_segments(route)
- add_language_segment(copy, lang)
- add_language_requirements(copy, lang)
- translate_static_segments copy, dictionaries[lang]
-
- new_routes << copy
- new_named_routes["#{route_name}_#{lang}".to_sym] = copy if route_name
-
+ translated = translate_route_by_lang(route, lang, dictionaries[lang], route_name)
+ new_routes << translated
+ new_named_routes["#{route_name}_#{lang}".to_sym] = translated if route_name
end
[new_routes, new_named_routes]
end
View
@@ -1,3 +1,5 @@
+require 'ruby-debug'
+
# TranslateRoutes
module ActionController
@@ -64,44 +66,22 @@ def self.translate_current_routes(dictionaries)
old_routes.each do |old_route|
old_name = old_names.index(old_route)
-
# process and add the translated ones
trans_routes, trans_named_routes = translate_route(old_route, dictionaries, old_name)
new_routes.concat(trans_routes)
- # process the old route:
- new_old_route = clone_with_deeply_copied_static_segments(old_route) # we need a fresh route to apply requirements
+ # translate and add the old route:
+ new_old_route = translate_route_by_lang(old_route, default_lang, default_dict, old_name)
- translate_static_segments(new_old_route, default_dict)
- add_language_requirements(new_old_route, default_lang)
- add_language_segment(new_old_route, default_lang) if prefix_on_default_lang
new_routes << new_old_route
# if it's a named one we append the lang suffix and replace the old helper by a language-based call
if old_name
-
trans_named_routes["#{old_name}_#{default_lang}"] = new_old_route
trans_named_routes[old_name] = new_old_route # keep the old name to use the helper on integration tests
-
new_named_routes.merge! trans_named_routes
-
- ['path', 'url'].each do |suffix|
- new_helper_name = "#{old_name}_#{suffix}"
- def_new_helper = <<-DEF_NEW_HELPER
- def #{new_helper_name}(*args)
- if defined? @#{lang_param_key}
- send("#{old_name}_\#{@#{lang_param_key}}_#{suffix}", *args)
- else
- send("#{old_name}_#{default_lang}_#{suffix}", *args)
- end
- end
- DEF_NEW_HELPER
-
- [ActionController::Base, ActionView::Base].each { |d| d.module_eval(def_new_helper) }
- ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
- end
-
- end
+ generate_helpers(old_name)
+ end
end
@@ -111,69 +91,83 @@ def #{new_helper_name}(*args)
end
- def self.clone_with_deeply_copied_static_segments(route)
- copy = Route.new
- copy.segments = []
+ def self.generate_helpers(old_name)
+ ['path', 'url'].each do |suffix|
+ new_helper_name = "#{old_name}_#{suffix}"
+ def_new_helper = <<-DEF_NEW_HELPER
+ def #{new_helper_name}(*args)
+ if defined? @#{lang_param_key}
+ send("#{old_name}_\#{@#{lang_param_key}}_#{suffix}", *args)
+ else
+ send("#{old_name}_#{default_lang}_#{suffix}", *args)
+ end
+ end
+ DEF_NEW_HELPER
+
+ [ActionController::Base, ActionView::Base].each { |d| d.module_eval(def_new_helper) }
+ ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
+ end
+ end
+
+ def self.add_prefix?(lang)
+ @@prefix_on_default_lang || lang != @@default_lang
+ end
+
+ def self.translate_static_segments(route, dictionary)
+ segments = []
route.segments.each do |s|
-
if s.instance_of?(StaticSegment)
new_segment = StaticSegment.new
- new_segment.value = s.value.dup
+ new_segment.value = dictionary[s.value] || s.value.dup
new_segment.is_optional = s.is_optional
else
new_segment = s.dup # just reference the original
end
-
- copy.segments << new_segment
+ segments << new_segment
end
- copy.conditions = route.conditions.dup
- copy.requirements = route.requirements.dup
- copy
- end
-
- def self.add_language_requirements(route, lang)
- route.requirements[lang_param_key] = lang
+ segments
end
- def self.add_language_segment(route, lang)
-
+ def self.lang_segments(orig, lang, dictionary)
+ return translate_static_segments(orig, dictionary) unless add_prefix?(lang)
+
# divider ('/')
- new_divider_segment = DividerSegment.new
- new_divider_segment.value = route.segments.first.value
- new_divider_segment.is_optional = false # la prueba
+ divider = DividerSegment.new
+ divider.value = orig.segments.first.value
+ divider.is_optional = false # la prueba
# static ('es')
- new_static_segment = StaticSegment.new
- new_static_segment.value = lang
- new_static_segment.is_optional = false
+ static = StaticSegment.new
+ static.value = lang
+ static.is_optional = false
- route.segments = [new_divider_segment, new_static_segment] + route.segments
-
+ [divider, static] + translate_static_segments(orig, dictionary)
end
+ def self.lang_requirements(orig, lang)
+ orig.requirements.merge(@@lang_param_key => lang)
+ end
- def self.translate_static_segments(route, dictionary)
- route.segments.each do |segment|
- if segment.instance_of?(StaticSegment) && dictionary[segment.value]
- segment.value = dictionary[segment.value]
- end
- end
+ def self.translate_route_by_lang(orig, lang, dictionary, orig_name=nil)
+ segments = lang_segments(orig, lang, dictionary)
+ requirements = lang_requirements(orig, lang)
+ conditions = orig.conditions
+
+ r = Route.new
+ r.segments = segments
+ r.requirements = requirements
+ r.conditions = conditions
+ r
end
def self.translate_route(route, dictionaries, route_name = nil)
new_routes = []
new_named_routes = {}
dictionaries.keys.each do |lang|
-
- copy = clone_with_deeply_copied_static_segments(route)
- add_language_segment(copy, lang)
- add_language_requirements(copy, lang)
- translate_static_segments copy, dictionaries[lang]
-
- new_routes << copy
- new_named_routes["#{route_name}_#{lang}".to_sym] = copy if route_name
-
+ translated = translate_route_by_lang(route, lang, dictionaries[lang], route_name)
+ new_routes << translated
+ new_named_routes["#{route_name}_#{lang}".to_sym] = translated if route_name
end
[new_routes, new_named_routes]
end

0 comments on commit a8f30ba

Please sign in to comment.