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.



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

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

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

  return segment if segment.starts_with?('('.freeze) || segment.starts_with?('*'.freeze) || segment.include?(':'.freeze)

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

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