Module: I18n::Scope

Defined in:
lib/i18n_auto_scoping.rb

Class Method Summary collapse

Class Method Details

.defaultObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/i18n_auto_scoping.rb', line 74

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



70
71
72
# File 'lib/i18n_auto_scoping.rb', line 70

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/i18n_auto_scoping.rb', line 90

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/').last
  scope = File.basename(path).split('.').shift
  # 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



105
106
107
108
109
110
111
# File 'lib/i18n_auto_scoping.rb', line 105

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