Class: I18n::Instrument::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/instrument/middleware.rb

Constant Summary collapse

STORE_KEY =

used to store request information in the request store

:i18n_instrumentation
JS_RESPONSE =

canned response sent to js instrumentation requests

[
  200, {
    'Content-Type' => 'application/json',
    'Content-Length' => 2
  }, [
    '{}'
  ]
]

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/i18n/instrument/middleware.rb', line 20

def initialize(app)
  @app = app

  # this will fire on every call to I18n.t
  I18n::Debug.on_lookup do |key, value|
    next unless enabled?
    config.on_lookup.call(key, value)

    begin
      # find the first application-specific line in the stack trace
      raw_trace = filter_stack_trace(::Kernel.caller)
      trace = raw_trace.split(":in `").first if raw_trace

      # grab path params (set in `call` method below)
      url = store.fetch(STORE_KEY, {}).fetch(:url, nil)

      if url.present? && trace.present?
        record_translation_lookup(
          url: url, trace: trace, key: key,
          locale: I18n.locale.to_s, source: 'ruby'
        )
      end
    rescue => e
      config.on_error.call(e)
    end
  end
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/i18n/instrument/middleware.rb', line 48

def call(env)
  if i18n_js_request?(env)
    handle_i18n_js_request(env)
  else
    handle_regular_request(env)
  end
rescue => e
  config.on_error.call(e)
  @app.call(env)
end