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



44
45
46
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 44

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



64
65
66
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 64

def tr8n_component
  nil
end

#tr8n_init_client_sdkObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 84

def tr8n_init_client_sdk
  return if Tr8n.config.disabled?

  @tr8n_started_at = Time.now

  Tr8n.session.init

  translator = nil

  cookie_name = "tr8n_#{tr8n_application.key}"
  if request.cookies[cookie_name]
    cookie_params = Tr8n::Utils.decode_and_verify_params(request.cookies[cookie_name], tr8n_application.secret)
    Tr8n.logger.info(cookie_params.inspect)
    locale = cookie_params["locale"]
    translator = Tr8n::Translator.new(cookie_params["translator"].merge(:application => tr8n_application)) unless cookie_params["translator"].nil?
  end

  locale ||= tr8n_init_current_locale || Tr8n.config.default_locale

  Tr8n.session.current_user = tr8n_init_current_user
  Tr8n.session.current_translator = translator
  Tr8n.session.current_language = tr8n_application.language(locale || tr8n_init_current_locale || Tr8n.config.default_locale)
  Tr8n.session.current_source = tr8n_source
  Tr8n.session.current_component = tr8n_component
end

#tr8n_init_current_localeObject



68
69
70
71
72
73
74
75
76
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 68

def tr8n_init_current_locale
  self.send(Tr8n.config.current_locale_method) if Tr8n.config.current_locale_method
rescue
  # 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]
end

#tr8n_init_current_userObject



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

def tr8n_init_current_user
  self.send(Tr8n.config.current_user_method) if Tr8n.config.current_user_method
rescue
  Tr8n.config.default_locale
end

#tr8n_reset_client_sdkObject



110
111
112
113
114
115
116
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 110

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



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

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

#tr8n_user_preferred_localeObject



48
49
50
51
52
53
54
# File 'lib/tr8n_client_sdk/extensions/action_controller_extension.rb', line 48

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