module TmlRails::ActionControllerExtension::InstanceMethods

Public Instance Methods

tml_access_token() click to toggle source
# File lib/tml_rails/extensions/action_controller_extension.rb, line 124
def tml_access_token
  tml_cookie[:oauth] ? tml_cookie[:oauth][:token] : nil
end
tml_filter_init() click to toggle source
# File lib/tml_rails/extensions/action_controller_extension.rb, line 236
def tml_filter_init
  tml_init if Tml.config.auto_init
end
tml_filter_reset() click to toggle source
# File lib/tml_rails/extensions/action_controller_extension.rb, line 250
def tml_filter_reset
  tml_reset if Tml.config.auto_init
end
tml_init() click to toggle source
# File lib/tml_rails/extensions/action_controller_extension.rb, line 128
def tml_init
  return if Tml.config.disabled?

  # Tml.logger.info(tml_cookie.inspect)

  @tml_started_at = Time.now

  requested_locale = desired_locale = tml_locale

  # if user has a custom method for providing the locale, use it
  if Tml.config.current_locale_method
    begin
      desired_locale = self.send(Tml.config.current_locale_method)
    rescue
      desired_locale = requested_locale
    end
  end
  # check if locale was previously stored in a cookie
  desired_locale ||= Tml.config.locale_cookie_enabled? ? tml_cookie[:locale] : nil
  # fallback onto the browser locale
  desired_locale ||= Tml.config.locale_browser_enabled? ? Tml::Utils.browser_accepted_locales(
      request.env['HTTP_ACCEPT_LANGUAGE']
  ).join(',') : nil

  # pp requested_locale: requested_locale, desired_locale: desired_locale
  # pp cookie: tml_cookie

  # init SDK with desired locale and get the actual locale supported in the app
  Tml.session.init(
      :source => tml_source,
      :locale => desired_locale,
      :user => tml_viewing_user,
      :translator => tml_translator,
      :access_token => tml_access_token
  )

  if defined? I18n.enforce_available_locales
    I18n.enforce_available_locales = false
  end
  I18n.locale = tml_current_locale

  # pp current_locale: tml_current_locale

  # check if we want to store the last selected locale in the cookie
  if requested_locale == tml_current_locale and Tml.config.locale_cookie_enabled?
    tml_cookie[:locale] = tml_current_locale
    cookies[Tml::Utils.cookie_name(Tml.config.application[:key])] = {
        :value => Tml::Utils.encode(tml_cookie),
        :expires => 1.year.from_now,
        :domain => Tml.config.locale[:domain]
    }
  end

  # pp cookie: tml_cookie
  # pp redirect: Tml.config.locale_redirect_enabled?

  if Tml.config.locale_redirect_enabled?
    if Tml.config.locale[:skip_default] and tml_current_locale == tml_default_locale
      # first lets see if we are in default locale and user doesn't want to show locale in url
      if Tml.config.locale_strategy == 'pre-path' and not requested_locale.nil?
        return redirect_to(Tml.config.locale_param => nil)
      end

      if Tml.config.locale_strategy == 'pre-domain' and request.subdomains.any?
        fragments = request.host.split('.')
        if fragments.first.match(Tml.config.locale_expression)
          if Tml.config.locale[:default_subdomain]
            fragments[0] = Tml.config.locale[:default_subdomain]
          else
            fragments.shift
          end
        end
        return redirect_to(host: fragments.join('.'))
      end

      if Tml.config.locale_strategy == 'custom-domain'
        host = Tml.config.locale[:mapping][tml_default_locale]
        return redirect_to(host: host)
      end
    elsif requested_locale != tml_current_locale
      # otherwise, the locale is not the same as what was requested, deal with it
      if Tml.config.locale_strategy == 'pre-path'
        return redirect_to(Tml.config.locale_param => tml_current_locale)
      end

      if Tml.config.locale_strategy == 'pre-domain'
        fragments = request.host.split('.')
        if request.subdomains.any?
          fragments[0] = tml_current_locale
        else
          fragments.unshift(tml_current_locale)
        end
        return redirect_to(host: fragments.join('.'))
      end

      if Tml.config.locale_strategy == 'custom-domain'
        host = Tml.config.locale[:mapping][tml_current_locale]
        host ||= Tml.config.locale[:mapping][tml_default_locale]
        return redirect_to(host: host)
      end
    end
  end

  if tml_current_translator and tml_current_translator.inline?
    I18n.reload!
  end
end
tml_locale() click to toggle source

Locale is retrieved from method => params => cookie => subdomain => browser accepted locales Alternatively, this method can be overwritten

# File lib/tml_rails/extensions/action_controller_extension.rb, line 79
def tml_locale
  # if locale has been passed by a param, it will be in the params hash
  if Tml.config.locale_strategy == 'param'
    return params[Tml.config.locale_param]  # will be nil without ?locale=:locale
  end

  if Tml.config.locale_strategy == 'pre-path'
    return params[Tml.config.locale_param]  # will be nil without /:locale
  end

  if Tml.config.locale_strategy == 'pre-domain'
    locale = request.subdomains.first
    if locale.nil? or not locale.match(Tml.config.locale_expression)
      locale = Tml.config.locale[:default]
    end
    return locale
  end

  if Tml.config.locale_strategy == 'custom-domain'
    host = "#{request.host}#{[80, 443].include?(request.port) ? '' : ":#{request.port}"}"
    locale = Tml.config.locale[:mapping].invert[host]  # will be nil if host is wrong
    return locale
  end

  nil
end
tml_reset() click to toggle source
# File lib/tml_rails/extensions/action_controller_extension.rb, line 240
def tml_reset
  return if Tml.config.disabled?
  @tml_finished_at = Time.now
  Tml.session.application.submit_missing_keys if Tml.session.application
  Tml.session.reset
  Tml.cache.reset_version
  Tml.logger.info("Request took #{@tml_finished_at - @tml_started_at} mls") if @tml_started_at
  Tml.logger.info('-----------------------------------------------------------')
end
tml_source() click to toggle source

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

# File lib/tml_rails/extensions/action_controller_extension.rb, line 71
def tml_source
  "/#{controller_name}/#{action_name}"
rescue
  "/classes/#{self.class.name}"
end
tml_translator() click to toggle source
# File lib/tml_rails/extensions/action_controller_extension.rb, line 116
def tml_translator
  if tml_cookie[:translator]
    Tml::Translator.new(tml_cookie[:translator])
  else
    nil
  end
end
tml_viewing_user() click to toggle source
# File lib/tml_rails/extensions/action_controller_extension.rb, line 106
def tml_viewing_user
  unless Tml.config.current_user_method.blank?
    begin
      self.send(Tml.config.current_user_method)
    rescue
      {}
    end
  end
end