module RouteTranslator::Translator::Path

Private Class Methods

config_requires_locale?() click to toggle source
# File lib/route_translator/translator/path.rb, line 15
def config_requires_locale?
  config = RouteTranslator.config
  (config.force_locale || config.generate_unlocalized_routes || config.generate_unnamed_unlocalized_routes).present?
end
default_locale?(locale) click to toggle source
# File lib/route_translator/translator/path.rb, line 20
def default_locale?(locale)
  locale.to_sym == I18n.default_locale.to_sym
end
display_locale?(locale) click to toggle source
# File lib/route_translator/translator/path.rb, line 11
def display_locale?(locale)
  !RouteTranslator.config.hide_locale && (!default_locale?(locale) || config_requires_locale?)
end
locale_param_present?(path) click to toggle source
# File lib/route_translator/translator/path.rb, line 24
def locale_param_present?(path)
  path.split('/').include? ":#{RouteTranslator.locale_param_key}"
end
locale_segment(locale) click to toggle source
# File lib/route_translator/translator/path.rb, line 28
def locale_segment(locale)
  if RouteTranslator.config.locale_segment_proc
    locale_segment_proc = RouteTranslator.config.locale_segment_proc

    locale_segment_proc.to_proc.call(locale)
  else
    locale.to_s.downcase
  end
end

Public Instance Methods

translate(path, locale, scope) click to toggle source

Translates a path and adds the locale prefix.

# File lib/route_translator/translator/path.rb, line 42
def translate(path, locale, scope)
  new_path = path.dup
  final_optional_segments = new_path.slice!(%r{(\([^\/]+\))$})
  translated_segments = new_path.split('/').map do |seg|
    seg.split('.').map { |phrase| Segment.translate(phrase, locale, scope) }.join('.')
  end
  translated_segments.reject!(&:empty?)

  if display_locale?(locale) && !locale_param_present?(new_path)
    translated_segments.unshift(locale_segment(locale))
  end

  joined_segments = translated_segments.join('/')

  "/#{joined_segments}#{final_optional_segments}".gsub(%r{\/\(\/}, '(/')
end