Module: RouteLocalize

Defined in:
lib/route_localize.rb,
lib/route_localize/route.rb,
lib/route_localize/engine.rb,
lib/route_localize/version.rb

Defined Under Namespace

Classes: Engine, Route

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.define_locale_helpers(name, helper) ⇒ Object

Create _path and _url helpers for the given path name that uses I18n.locale to pick the current path



39
40
41
42
43
44
45
# File 'lib/route_localize.rb', line 39

def define_locale_helpers(name, helper)
  %w(url path).each do |method|
    helper.send :define_method, "#{name}_#{method}" do |*args|
      send("#{name}_#{I18n.locale}_#{method}", *args)
    end
  end
end

.translate_path(path, locale, by_subdomain: false) ⇒ Object

Returns a translated path Example: “/trees/:id(.:format)” -> “/arbres/:id(.:format)”, …



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

def translate_path(path, locale, by_subdomain: false)
  path = path.dup

  # Remove "(.:format)" in routes or "?args" if used elsewhere
  final_options = path.slice!(/(\(.+\)|\?.*)$/)

  segments = path.split('/').map do |segment|
    translate_segment(segment, locale)
  end

  segments.unshift(":locale") unless by_subdomain
  segments = segments.reject(&:blank?)

  "/#{segments.join('/')}#{final_options}"
end

.translate_route(app, conditions, requirements, defaults, as, anchor, route_set) ⇒ Object

Yields one or several route definitions if the route definition has a localize or localize_subdomain scope

The arguments it accepts are the arguments given to ActionDispatch::Routing::RouteSet‘s method add_route, with the addition of the route_set argument that should hold the current route set.

The array it yields are the arguments accepted by add_route so that these can be handed back to Rails to insert the yielded route.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/route_localize.rb', line 17

def translate_route(app, conditions, requirements, defaults, as, anchor, route_set)
  locales = defaults.delete(:localize) || defaults.delete(:localize_subdomain)
  if locales.present?

    # Makes sure the routes aren't created before i18n can read translations
    # This happens when gems like activeadmin call `Rails.application.reload_routes!`
    return unless I18n.load_path.grep(/routes.yml$/).any?

    locales.each do |locale|
      route = Route.new(app, conditions, requirements, defaults,
                          as, anchor, route_set, locale)
      yield *route.to_add_route_arguments
    end

    define_locale_helpers(as, route_set.named_routes.module)
  else
    yield app, conditions, requirements, defaults, as, anchor
  end
end

.translate_segment(segment, locale) ⇒ Object

Translates part of a path if it can Example: “trees” -> “arbres”, “:id” -> “:id”



68
69
70
71
72
73
74
75
76
# File 'lib/route_localize.rb', line 68

def translate_segment(segment, locale)
  if segment =~ /^[a-z_0-9]+$/i
    translation = I18n.t "routes.#{segment}", default: segment,
                                              locale: locale
    CGI.escape(translation)
  else
    segment
  end
end