Class: I18nRouting::LocalizedMapping

Inherits:
ActionDispatch::Routing::Mapper::Mapping
  • Object
show all
Defined in:
lib/i18n_routing_rails31.rb,
lib/i18n_routing_rails32.rb

Overview

Used for localize simple named routes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, set, scope, path, options) ⇒ LocalizedMapping

Returns a new instance of LocalizedMapping.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/i18n_routing_rails31.rb', line 309

def initialize(locale, set, scope, path, options)
  super(set, scope, path.clone, options ? options.clone : nil)
  stored_locale = I18n.locale

  # try to get translated path :
  I18n.locale = locale
  ts = @path.gsub(/^\//, '')
  append_format = ts =~ /\(\.:format\)/
  ts.gsub!('(.:format)', '')

  tp = @options[:as] && I18nRouting.translation_for(@options[:as], :named_routes) ||
      !ts.blank? && I18nRouting.translation_for(ts, :named_routes_path) || ts

  localized_scope = I18nRouting.translation_for(@scope[:path].gsub(/\//, ''), :scopes) if @scope[:path]
  path = localized_scope ? '/' << localized_scope : @scope[:path]
  @localized_path = File.join((path || ''), tp).gsub(/\/$/, '')

  # If a translated path exists, set localized infos
  if !@localized_path.blank? and @localized_path != @path
    #@options[:controller] ||= @options[:as]
    @options[:as] = "#{I18nRouting.locale_escaped(locale)}_#{@options[:as]}"
    @path = @localized_path
    @path = "#{@path}(.:format)" if append_format
    @options[:constraints] = @options[:constraints] ? @options[:constraints].dup : {}
    @options[:constraints][:i18n_locale] = locale.to_s
    @options[:anchor] = true
    # Force the recomputation of the requirements with the new values
    @requirements = nil
  else
    @localized_path = nil
  end
  I18n.locale = stored_locale
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



307
308
309
# File 'lib/i18n_routing_rails31.rb', line 307

def path
  @path
end

Instance Method Details

#localizable?Boolean

Return true if this route is localizable

Returns:

  • (Boolean)


344
345
346
# File 'lib/i18n_routing_rails31.rb', line 344

def localizable?
  @localized_path != nil
end