Module: I18n::Scope

Defined in:
lib/i18n_auto_scoping.rb

Class Method Summary collapse

Class Method Details

.defaultObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/i18n_auto_scoping.rb', line 67

def default
  # Temporarly default scope go first
  return Thread.current[:i18n_temporarly_default_scope] if Thread.current[:i18n_temporarly_default_scope]
  
  # Then, if we are in a render, we try to get the current scope by scanning the caller stack
  if Thread.current[:last_i18n_auto_scope_render_in_render]
    # Cache the analyse of the caller stack result for performance
    return (Thread.current[:last_i18n_auto_scope_render] ||= get_scope_views_context)
  end

  # Finally, if there is no scope before, return the default scope
  Thread.current[:i18n_default_scope]
end

.default=(scope) ⇒ Object

Set a default scope



63
64
65
# File 'lib/i18n_auto_scoping.rb', line 63

def default=(scope)
  Thread.current[:i18n_default_scope] = (scope.is_a?(String)) ? scope.gsub('/', '.') : scope
end

.get_scope_views_contextObject

Inspired from : github.com/yar/simple_loc_compat Return the current view file, only work in app/views folder



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/i18n_auto_scoping.rb', line 83

def get_scope_views_context
  stack_to_analyse = caller
  latest_app_file = caller.detect { |level| level =~ /.*\/app\/views\// }
  return nil unless latest_app_file
  
  scope = latest_app_file.match(/([^:]+):\d+.*/)[1]
  path = scope.split('/app/views/').last
  scope = File.basename(path, File.extname(path))
  # If there is a second extension we keep it, in order to work with .erb.html or .erb.iphone, etc...
  scope = "#{File.dirname(path)}/#{scope}" if File.dirname(path) != '.'

  scope.gsub!('/', '.')

  scope.to_sym
end

.temporarly_change(scope) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/i18n_auto_scoping.rb', line 99

def temporarly_change(scope)
  old = Thread.current[:i18n_temporarly_default_scope]
  Thread.current[:i18n_temporarly_default_scope] = scope
  r = yield
  Thread.current[:i18n_temporarly_default_scope] = old
  return r
end