Module: I18nRouting::Mapper

Defined in:
lib/i18n_routing_rails31.rb,
lib/i18n_routing_rails32.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

Alias methods in order to handle i18n routes



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/i18n_routing_rails31.rb', line 262

def self.included(mod)
  mod.send :alias_method_chain, :initialize, :i18n_routing
  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] || {}
        i = 1
        while i < args.size and (String === args[i] or Symbol === args[i])
          pname[args[i]] = args[i]
          i += 1
        end
        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



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_rails31.rb', line 231

def create_globalized_resources(type, *resources, &block)
  #puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})"

  @scope[:nested_deep] ||= []
  @scope[:nested_deep] << 1

  cur_scope = nil
  if @locales
    localized = localized_resources(type, *resources, &block) if !@skip_localization

    ## We do not translate if we are in a translation 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

  @scope[:nested_deep].pop
end

#initialize_with_i18n_routing(*args) ⇒ Object

On Routing::Mapper initialization (when doing Application.routes.draw do …) prepare routing system to be i18n ready



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/i18n_routing_rails31.rb', line 120

def initialize_with_i18n_routing(*args)
  initialize_without_i18n_routing(*args)

  # Add i18n_locale as valid conditions for Rack::Mount / And add also :locale, as Rails 3.0.4 removed it ...
  @valid_conditions = @set.instance_eval { @set }.instance_eval { @valid_conditions }
  [:i18n_locale, :locale].each do |k|
    @valid_conditions << k if !@valid_conditions.include?(k)
    @set.valid_conditions << k if !@set.valid_conditions.include?(k)
  end

  # 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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/i18n_routing_rails31.rb', line 153

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')]).flatten.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



169
170
171
172
173
174
175
176
177
178
# File 'lib/i18n_routing_rails31.rb', line 169

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

Overrides match method Two variants of match are supported now

match 'about' => 'pages#about', :as => 'about'
match 'about', :to => 'pages#about'
match 'home', :to => 'pages#home', :as => 'main' # Route name overridden to 'main'


195
196
197
198
199
200
201
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
228
229
# File 'lib/i18n_routing_rails31.rb', line 195

def match(*args)
  # Localize simple match only if there is no resource scope.
  if @locales and (args.size >= 1) and !parent_resource and
         args.last.is_a?(Hash) # In both variants Hash should be present as last parameter

    # Make deep copy of last argument (Hash)
    options = Marshal.load(Marshal.dump(args.last)) # Dump is dirty but how to make deep cloning easily ? :/

    # Now handle parameters
    if (args.first.is_a?(Hash))
      path, to = options.find { |name, value| name.is_a?(String) }
      options.merge!(:to => to).delete(path)
    else
      path = args[0]
      options[:as] ||= path
    end

    if (options[:as])
      @locales.each do |locale|
        mapping = LocalizedMapping.new(locale, @set, @scope, path, options)
        if mapping.localizable?
          puts("[I18n] > localize %-10s: %40s (%s) => %s" % ['route', options[:as], locale, mapping.path]) if @i18n_verbose
          @set.add_route(*mapping.to_route)
        end
      end

      # Now, create the real match :
      return set_localizable_route(options[:as]) do
        super
      end
    end
  end

  super
end

#resource_with_i18n_routing(*resources, &block) ⇒ Object



296
297
298
# File 'lib/i18n_routing_rails31.rb', line 296

def resource_with_i18n_routing(*resources, &block)
  create_globalized_resources(:resource, *resources, &block)
end

#resources_with_i18n_routing(*resources, &block) ⇒ Object



300
301
302
# File 'lib/i18n_routing_rails31.rb', line 300

def resources_with_i18n_routing(*resources, &block)
  create_globalized_resources(:resources, *resources, &block)
end

#skip_localizationObject

Set we do not want to localize next resource



181
182
183
184
185
186
# File 'lib/i18n_routing_rails31.rb', line 181

def skip_localization
  old = @skip_localization
  @skip_localization = @localized_branch ? nil : true
  yield
  @skip_localization = old
end