Module: Tr8nClientSdk::ActionControllerExtension::InstanceMethods

Defined in:
lib/tr8n_client_sdk/extensions/action_controller_extension.rb

Instance Method Summary collapse

Instance Method Details

#tr8n_browser_accepted_localesObject



52
53
54
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 52

def tr8n_browser_accepted_locales
  @tr8n_browser_accepted_locales ||= Tr8n::Utils.browser_accepted_locales(request)
end

#tr8n_componentObject

Overwrite this method in a controller to assign a custom component for all views



72
73
74
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 72

def tr8n_component
  nil
end


80
81
82
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 80

def tr8n_cookie
  request.cookies[tr8n_cookie_name]
end


76
77
78
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 76

def tr8n_cookie_name
  "tr8n_#{tr8n_application.key}"
end


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 84

def tr8n_cookie_params
  @tr8n_cookie_params ||= begin
    if tr8n_cookie
      cookie_params = Tr8n::Utils.decode_and_verify_params(tr8n_cookie, tr8n_application.secret)
      Tr8n.logger.info(cookie_params.inspect)
      cookie_params
    else
      {}
    end
  end
end

#tr8n_init_client_sdkObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 120

def tr8n_init_client_sdk
  return if Tr8n.config.disabled?

  @tr8n_started_at = Time.now

  Tr8n.session.init(
    :cookies => request.cookies,
    :locale => tr8n_locale,
    :user => tr8n_user,
    :source => tr8n_source,
    :component => tr8n_component
  )

  I18n.enforce_available_locales = false
  I18n.locale = Tr8n.session.current_language.locale
end

#tr8n_localeObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 101

def tr8n_locale
  self.send(Tr8n.config.current_locale_method) if Tr8n.config.current_locale_method
rescue
  # fallback onto the cookie params from the service
  return tr8n_translator_locale if tr8n_translator_locale

  # fallback to the default session based locale implementation
  # choose the first language from the accepted languages header
  session[:locale] = tr8n_user_preferred_locale unless session[:locale]
  session[:locale] = params[:locale] if params[:locale]
  session[:locale] || Tr8n.config.default_locale
end

#tr8n_reset_client_sdkObject



137
138
139
140
141
142
143
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 137

def tr8n_reset_client_sdk
  return if Tr8n.config.disabled?
  @tr8n_finished_at = Time.now
  tr8n_application.submit_missing_keys
  Tr8n.session.reset
  Tr8n.logger.info("Request took #{@tr8n_finished_at - @tr8n_started_at} mls") if @tr8n_started_at
end

#tr8n_sourceObject

Overwrite this method in a controller to assign a custom source for all views



65
66
67
68
69
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 65

def tr8n_source
  "/#{controller_name}/#{action_name}"
rescue
  self.class.name
end

#tr8n_translator_localeObject



96
97
98
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 96

def tr8n_translator_locale
  tr8n_cookie_params["locale"]
end

#tr8n_userObject



114
115
116
117
118
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 114

def tr8n_user
  self.send(Tr8n.config.current_user_method) if Tr8n.config.current_user_method
rescue
  nil
end

#tr8n_user_preferred_localeObject



56
57
58
59
60
61
62
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 56

def tr8n_user_preferred_locale
  tr8n_browser_accepted_locales.each do |locale|
    next unless Tr8n.session.application.locales.include?(locale)
    return locale
  end
  Tr8n.config.default_locale
end