Module: I18nRouting::Mapper
- Defined in:
- lib/i18n_routing_rails3.rb
Class Method Summary collapse
-
.included(mod) ⇒ Object
Alias methods in order to handle i18n routes.
Instance Method Summary collapse
- #create_globalized_resources(type, *resources, &block) ⇒ Object
-
#initialize(*args) ⇒ Object
On Routing::Mapper initialization (when doing Application.routes.draw do …) prepare routing system to be i18n ready.
-
#localized(locales = I18n.available_locales, opts = {}) ⇒ Object
Rails 3 routing system Create a block for localized routes, in your routes.rb :.
-
#localized_branch(locale) ⇒ Object
Create a branch for create routes in the specified locale.
- #match(*args) ⇒ Object
- #resource_with_i18n_routing(*resources, &block) ⇒ Object
- #resources_with_i18n_routing(*resources, &block) ⇒ Object
-
#skip_localization ⇒ Object
Set we do not want to localize next resource.
Class Method Details
.included(mod) ⇒ Object
Alias methods in order to handle i18n routes
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/i18n_routing_rails3.rb', line 230 def self.included(mod) mod.send :alias_method_chain, :resource, :i18n_routing mod.send :alias_method_chain, :resources, :i18n_routing # Here we redefine some methods, in order to handle # correct path_names translation on the fly [:map_method, :member, :collection].each do |m| rfname = "#{m}_without_i18n_routing".to_sym mod.send :define_method, "#{m}_with_i18n_routing".to_sym do |*args, &block| if @localized_branch and @scope[:i18n_scope_level_resource] and @scope[:i18n_real_resource_name] o = @scope[:scope_level_resource] @scope[:scope_level_resource] = @scope[:i18n_scope_level_resource] pname = @scope[:path_names] || {} pname[args[1]] = args[1] scope(:path_names => I18nRouting.path_names(@scope[:i18n_real_resource_name], {:path_names => pname})) do send(rfname, *args, &block) end @scope[:scope_level_resource] = o return end send(rfname, *args, &block) end mod.send :alias_method_chain, m, :i18n_routing end end |
Instance Method Details
#create_globalized_resources(type, *resources, &block) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/i18n_routing_rails3.rb', line 202 def create_globalized_resources(type, *resources, &block) #puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})" cur_scope = nil if @locales localized = localized_resources(type, *resources, &block) if !@skip_localization ## We do not translate if we are in a translations branch : return if localized and nested_deep > 0 # Set the current standard resource in order to customize url helper : if !@localized_branch r = resource_from_params(type, *resources) cur_scope = (parent_resource and parent_resource.name == r.name) ? parent_resource : r end end set_localizable_route(cur_scope) do skip_localization do #puts "#{' ' * nested_deep} \\- Call original #{type} : for #{resources.inspect}}" send("#{type}_without_i18n_routing".to_sym, *resources, &block) end end end |
#initialize(*args) ⇒ Object
On Routing::Mapper initialization (when doing Application.routes.draw do …) prepare routing system to be i18n ready
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/i18n_routing_rails3.rb', line 116 def initialize(*args) super # Add i18n as valid conditions for Rack::Mount @valid_conditions = @set.instance_eval { @set }.instance_eval { @valid_conditions } @valid_conditions << :i18n_locale if !@valid_conditions.include?(:i18n_locale) # Extends the current RouteSet in order to define localized helper for named routes # When calling define_url_helper, it calls define_localized_url_helper too. if !@set.named_routes.respond_to?(:define_localized_url_helper) @set.named_routes.class_eval <<-END_EVAL, __FILE__, __LINE__ + 1 alias_method :localized_define_url_helper, :define_url_helper def define_url_helper(route, name, kind, options) localized_define_url_helper(route, name, kind, options) define_localized_url_helper(route, name, kind, options) end END_EVAL @set.named_routes.extend I18nRouting::NamedRouteCollection end end |
#localized(locales = I18n.available_locales, opts = {}) ⇒ Object
Rails 3 routing system Create a block for localized routes, in your routes.rb :
localized do
resources :users
match 'about' => 'contents#about', :as => :about
end
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/i18n_routing_rails3.rb', line 146 def localized(locales = I18n.available_locales, opts = {}) # Add if not added Rails.root/config/locales/*.yml in the I18n.load_path if !@i18n_routing_path_set and defined?(Rails) and Rails.respond_to?(:root) and Rails.root I18n.load_path = (I18n.load_path << Dir[Rails.root.join('config', 'locales', '*.yml').to_s]).uniq @i18n_routing_path_set = true end old_value = @locales @locales = locales @i18n_verbose ||= opts.delete(:verbose) yield ensure @locales = old_value end |
#localized_branch(locale) ⇒ Object
Create a branch for create routes in the specified locale
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/i18n_routing_rails3.rb', line 163 def localized_branch(locale) set_localizable_route(nil) do old = @localized_branch @localized_branch = locale localized([locale]) do yield end @localized_branch = old end end |
#match(*args) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/i18n_routing_rails3.rb', line 182 def match(*args) # Localize simple match only if there is no resource scope. if args.size == 1 and @locales and !parent_resource and args.first[:as] @locales.each do |locale| mapping = LocalizedMapping.new(locale, @set, @scope, Marshal.load(Marshal.dump(args))) # Dump is dirty but how to make deep cloning easily ? :/ if mapping.localizable? puts("[I18n] > localize %-10s: %40s (%s) => %s" % ['route', args.first[:as], locale, mapping.path]) if @i18n_verbose @set.add_route(*mapping.to_route) end end # Now, create the real match : return set_localizable_route(args.first[:as]) do super end end super end |
#resource_with_i18n_routing(*resources, &block) ⇒ Object
261 262 263 |
# File 'lib/i18n_routing_rails3.rb', line 261 def resource_with_i18n_routing(*resources, &block) create_globalized_resources(:resource, *resources, &block) end |
#resources_with_i18n_routing(*resources, &block) ⇒ Object
265 266 267 |
# File 'lib/i18n_routing_rails3.rb', line 265 def resources_with_i18n_routing(*resources, &block) create_globalized_resources(:resources, *resources, &block) end |
#skip_localization ⇒ Object
Set we do not want to localize next resource
175 176 177 178 179 180 |
# File 'lib/i18n_routing_rails3.rb', line 175 def skip_localization old = @skip_localization @skip_localization = @localized_branch ? nil : true yield @skip_localization = old end |