Module: I18nRouting

Defined in:
lib/i18n_routing_common.rb,
lib/i18n_routing_rails3.rb

Overview

I18nRouting module for common usage methods

Defined Under Namespace

Modules: Mapper, NamedRouteCollection, RackMountRoute Classes: LocalizedMapping

Constant Summary collapse

DefaultPathNames =
[:new, :edit]
PathNamesKeys =
[:path_names, :member, :collection]

Class Method Summary collapse

Class Method Details

.locale_escaped(locale) ⇒ Object



5
6
7
# File 'lib/i18n_routing_common.rb', line 5

def self.locale_escaped(locale)
  locale.to_s.downcase.gsub('-', '_')
end

.path_names(name, options) ⇒ Object

Return path names hash for given resource



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/i18n_routing_common.rb', line 33

def self.path_names(name, options)
  h = (options[:path_names] || {}).dup
  
  path_names = DefaultPathNames
  PathNamesKeys.each do |v|
    path_names += options[v].keys if options[v] and Hash === options[v]
  end
  
  path_names.each do |pn|
    n = translation_for(name, :path_names, pn)
    n = nil if n == pn.to_s
    # Get default path_names in path_names scope if no path_names found
    n ||= I18n.t(pn, :scope => :path_names, :default => name.to_s)

    h[pn] = n if n and n != name.to_s
  end

  return h
end

.translation_for(name, type = :resources, option = nil) ⇒ Object

Return the correct translation for given values



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/i18n_routing_common.rb', line 10

def self.translation_for(name, type = :resources, option = nil)
  # First, if an option is given, try to get the translation in the routes scope
  if option
    default = "{option}Noi18nRoutingTranslation"
    t = I18n.t(option, :scope => "routes.#{name}.#{type}", :default => default)
    return (t == default ? nil : t)
  else
    default = "{name}Noi18nRoutingTranslation"

    # Try to get the translation in routes namescope first      
    t = I18n.t(:as, :scope => "routes.#{name}", :default => default)

    return t if t and t != default

    t = I18n.t(name.to_s, :scope => type, :default => default)
    return (t == default ? nil : t)
  end
end