Module: RouteTranslator::Translator::Path

Defined in:
lib/route_translator/translator/path.rb,
lib/route_translator/translator/path/segment.rb

Defined Under Namespace

Modules: Segment

Class Method Summary collapse

Class Method Details

.translate(path, locale, scope) ⇒ Object

Translates a path and adds the locale prefix.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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