|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RouteTranslator |
| 4 | + module CoreExt |
| 5 | + module MapperPatch81 |
| 6 | + extend ActiveSupport::Concern |
| 7 | + |
| 8 | + URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER |
| 9 | + private_constant :URI_PARSER |
| 10 | + |
| 11 | + |
| 12 | + def localized(engine) |
| 13 | + @localized = true |
| 14 | + @engine = engine |
| 15 | + yield |
| 16 | + @localized = false |
| 17 | + @engine = nil |
| 18 | + end |
| 19 | + |
| 20 | + |
| 21 | + private |
| 22 | + |
| 23 | + |
| 24 | + def add_route(action, controller, as, options_action, _path, to, via, formatted, anchor, options_constraints, internal, options_mapping) |
| 25 | + return super unless @localized |
| 26 | + |
| 27 | + localized_add_route(action, controller, as, options_action, _path, to, via, formatted, anchor, options_constraints, internal, options_mapping) |
| 28 | + end |
| 29 | + |
| 30 | + |
| 31 | + def localized_add_route(action, controller, as, options_action, _path, to, via, formatted, anchor, options_constraints, internal, options_mapping) |
| 32 | + # From Rails source |
| 33 | + # See: https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/routing/mapper.rb#L2195 |
| 34 | + path = path_for_action(action, _path) |
| 35 | + raise ArgumentError, "path is required" if path.blank? |
| 36 | + |
| 37 | + action = action.to_s |
| 38 | + |
| 39 | + default_action = options_action || @scope[:action] |
| 40 | + |
| 41 | + if /^[\w\-\/]+$/.match?(action) |
| 42 | + default_action ||= action.tr("-", "_") unless action.include?("/") |
| 43 | + else |
| 44 | + action = nil |
| 45 | + end |
| 46 | + |
| 47 | + as = name_for_action(as, action) if as |
| 48 | + path = ActionDispatch::Routing::Mapper::Mapping.normalize_path URI_PARSER.escape(path), formatted |
| 49 | + ast = ActionDispatch::Journey::Parser.parse path |
| 50 | + |
| 51 | + mapping = ActionDispatch::Routing::Mapper::Mapping.build(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, internal, options_mapping) |
| 52 | + # End of Rails source |
| 53 | + |
| 54 | + # Original |
| 55 | + # @set.add_route(mapping, as) |
| 56 | + |
| 57 | + # Our override |
| 58 | + _add_localized_route(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, internal, options_mapping, mapping, as, path, @engine) |
| 59 | + end |
| 60 | + |
| 61 | + |
| 62 | + def _add_localized_route(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, internal, options_mapping, mapping, as, path, engine) |
| 63 | + route = RouteTranslator::Route.new(set, path, as, options_constraints, options_mapping, mapping) |
| 64 | + translator = RouteTranslator.translator_for(engine) |
| 65 | + |
| 66 | + raise RouteTranslator::TranslatorNotFound, "RouteTranslator is not configured for current engine : #{engine}" if translator.nil? |
| 67 | + |
| 68 | + translator.translations_for(route) do |locale, translated_name, translated_path, translated_options_constraints, translated_options| |
| 69 | + translated_path_ast = ::ActionDispatch::Journey::Parser.parse(translated_path) |
| 70 | + translated_mapping = _translate_mapping(locale, set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor, internal) |
| 71 | + |
| 72 | + # call original method |
| 73 | + set.add_route translated_mapping, translated_name |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + |
| 78 | + def _translate_mapping(locale, route_set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor, internal) |
| 79 | + scope_params = { |
| 80 | + blocks: scope[:blocks] || [], |
| 81 | + constraints: scope[:constraints] || {}, |
| 82 | + defaults: (scope[:defaults] || {}).dup, |
| 83 | + module: scope[:module], |
| 84 | + options: scope[:options] ? scope[:options].merge(translated_options) : translated_options |
| 85 | + } |
| 86 | + |
| 87 | + ::ActionDispatch::Routing::Mapper::Mapping.build scope_params, route_set, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, internal, translated_options |
| 88 | + end |
| 89 | + |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments