module RouteTranslator::Translator::Path::Segment
def fallback_options(str, locale)
def fallback_options(str, locale) if RouteTranslator.config.disable_fallback && locale.to_s != I18n.default_locale.to_s { scope: :routes, fallback: false } else { scope: :routes, default: str } end end
def translatable_segment(segment)
def translatable_segment(segment) matches = TRANSLATABLE_SEGMENT.match(segment) matches[1] if matches.present? end
def translate(segment, locale, scope)
If there is no translation, the path segment is blank, begins with a
If the path contains a hyphenated suffix, it will be translated.
"people(.:format)", only "people" will be translated.
If the path segment contains something like an optional format
Translates a single path segment.
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
def translate_resource(str, locale, scope)
def translate_resource(str, locale, scope) handler = proc { |exception| exception } opts = { locale: locale, scope: scope } if I18n.t(str, **opts.merge(exception_handler: handler)).is_a?(I18n::MissingTranslation) I18n.t! str, **opts.merge(fallback_options(str, locale)) else I18n.t str, **opts end end
def translate_string(str, locale, scope)
def translate_string(str, locale, scope) translated_resource = translate_resource(str, locale.to_s, scope) URI::DEFAULT_PARSER.escape translated_resource end