Module: RouteTranslator::Translator::Path::Segment

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

Class Method Summary collapse

Class Method Details

.translate(segment, locale, scope) ⇒ Object

Translates a single path segment.

If the path segment contains something like an optional format “people(.:format)”, only “people” will be translated. If the path contains a hyphenated suffix, it will be translated. If there is no translation, the path segment is blank, begins with a “:” (param key) or “*” (wildcard), the segment is returned untouched.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/route_translator/translator/path/segment.rb', line 50

def translate(segment, locale, scope)
  return segment if segment.empty?

  if segment.start_with?(':')
    named_param, hyphenized = segment.split('-', 2)
    return "#{named_param}-#{translate(hyphenized, locale, scope)}" if hyphenized
  end

  return segment if segment.start_with?('(', '*') || segment.include?(':')

  appended_part = segment.slice!(/(\()$/)
  str = translatable_segment(segment)

  (translate_string(str, locale, scope) || segment) + appended_part.to_s
end