Module: RouteTranslator::Translator

Included in:
RouteTranslator
Defined in:
lib/route_translator.rb

Instance Method Summary collapse

Instance Method Details

#add_prefix?(locale) ⇒ Boolean

Add prefix for all non-default locales

Returns:

  • (Boolean)


216
217
218
# File 'lib/route_translator.rb', line 216

def add_prefix? locale
  !default_locale?(locale)
end

#add_root_route(root_route, route_set) ⇒ Object

Add unmodified root route to route_set



171
172
173
174
175
176
# File 'lib/route_translator.rb', line 171

def add_root_route root_route, route_set
  root_route.conditions[:path_info] = root_route.conditions[:path_info].dup
  route_set.set.add_route *root_route
  route_set.named_routes[root_route.name] = root_route
  route_set.routes << root_route
end

#add_untranslated_helpers_to_controllers_and_views(old_name) ⇒ Object

Add standard route helpers for default locale e.g.

I18n.locale = :de
people_path -> people_de_path
I18n.locale = :fr
people_path -> people_fr_path


183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/route_translator.rb', line 183

def add_untranslated_helpers_to_controllers_and_views old_name
  ['path', 'url'].map do |suffix|
    new_helper_name = "#{old_name}_#{suffix}"
    
    ROUTE_HELPER_CONTAINER.each do |helper_container|
      helper_container.send :define_method, new_helper_name do |*args|
        send "#{old_name}_#{locale_suffix(I18n.locale)}_#{suffix}", *args
      end
    end

    new_helper_name.to_sym
  end
end

#translate(route_set) ⇒ Object

Translate a specific RouteSet, usually Rails.application.routes, but can be a RouteSet of a gem, plugin/engine etc.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/route_translator.rb', line 148

def translate route_set
  Rails.logger.info "Translating routes (default locale: #{default_locale})" if defined?(Rails) && defined?(Rails.logger)

  # save original routes and clear route set
  original_routes = route_set.routes.dup                     # Array [routeA, routeB, ...]

  original_named_routes = route_set.named_routes.routes.dup  # Hash {:name => :route}

  reset_route_set route_set

  original_routes.each do |original_route|
    translations_for(original_route).each do |translated_route_args|
      route_set.add_route *translated_route_args
    end
  end

  original_named_routes.each_key do |route_name|
    route_set.named_routes.helpers.concat add_untranslated_helpers_to_controllers_and_views(route_name)
  end
  
end

#translate_path(path, locale) ⇒ Object

Translates a path and adds the locale prefix.



221
222
223
224
225
226
227
228
# File 'lib/route_translator.rb', line 221

def translate_path path, locale
  final_optional_segments = path.match(/(\(.+\))$/)[1] rescue nil   # i.e: (.:format)
  path_segments = path.gsub(final_optional_segments,'').split("/")
  new_path = path_segments.map{ |seg| translate_path_segment(seg, locale) }.join('/')
  new_path = "/#{locale.downcase}#{new_path}" if add_prefix? locale
  new_path = '/' if new_path.blank?
  final_optional_segments ? new_path + final_optional_segments : new_path
end

#translate_path_segment(segment, locale) ⇒ Object

Tries to translate a single path segment. If the path segment contains sth. like a optional format “people(.:format)”, only “people” will be translated, if there is no translation, the path segment is blank or begins with a “:” (param key), the segment is returned untouched



235
236
237
238
239
240
241
# File 'lib/route_translator.rb', line 235

def translate_path_segment segment, locale
  return segment if segment.blank? or segment.starts_with?(":")

  match = TRANSLATABLE_SEGMENT.match(segment)[1] rescue nil

  (translate_string(match, locale) || segment).downcase
end

#translate_route(route, locale) ⇒ Object

Generate translation for a single route for one locale



205
206
207
208
209
210
211
212
213
# File 'lib/route_translator.rb', line 205

def translate_route route, locale
  conditions = { :path_info => translate_path(route.path, locale) }
  conditions[:request_method] = route.conditions[:request_method].source.upcase if route.conditions.has_key? :request_method
  requirements = route.requirements.merge LOCALE_PARAM_KEY => locale
  defaults = route.defaults.merge LOCALE_PARAM_KEY => locale
  new_name = "#{route.name}_#{locale_suffix(locale)}" if route.name

  [route.app, conditions, requirements, defaults, new_name]
end

#translate_string(str, locale) ⇒ Object



243
244
245
# File 'lib/route_translator.rb', line 243

def translate_string str, locale
  @dictionary[locale.to_s][str.to_s]
end

#translations_for(route) ⇒ Object

Generate translations for a single route for all available locales



198
199
200
201
202
# File 'lib/route_translator.rb', line 198

def translations_for route
  available_locales.map do |locale|
    translate_route route, locale
  end
end