class Tml::Session

Attributes

application[RW]
block_options[RW]
current_language[RW]
current_locale[RW]
current_source[RW]
current_translator[RW]
current_user[RW]

Public Instance Methods

block_option(key, lookup = true) click to toggle source

Block Options

# File lib/tml/session.rb, line 156
def block_option(key, lookup = true)
  if lookup
    block_options_queue.reverse.each do |options|
      value = options[key.to_s] || options[key.to_sym]
      return value if value
    end
    return nil
  end
  block_options[key]
end
block_options_queue() click to toggle source
# File lib/tml/session.rb, line 176
def block_options_queue
  @block_options ||= []
end
init(opts = {}) click to toggle source
# File lib/tml/session.rb, line 43
def init(opts = {})
  return if Tml.config.disabled?

  # Tml.logger.debug(opts.inspect)

  Tml.cache.reset_version
  Tml.cache.namespace = opts[:namespace]

  init_application(opts)

  self
end
init_application(opts = {}) click to toggle source
# File lib/tml/session.rb, line 56
def init_application(opts = {})
  self.current_user = opts[:user]
  self.current_source = opts[:source] || 'index'
  self.current_locale = opts[:locale]
  self.current_translator = opts[:translator]

  app_config = Tml.config.application || {}
  self.application = Tml::Application.new(
      :key => opts[:key] || app_config[:key],
      :access_token => opts[:access_token] || opts[:token] || app_config[:token],
      :host => opts[:host] || app_config[:host],
      :cdn_host => opts[:cdn_host] || app_config[:cdn_host]
  ).fetch

  if self.current_translator
    self.current_translator.application = self.application
  end

  self.current_locale = preferred_locale(opts[:locale])
  self.current_language = self.application.current_language(self.current_locale)
end
inline_mode?() click to toggle source
# File lib/tml/session.rb, line 120
def inline_mode?
  current_translator and current_translator.inline?
end
pop_block_options() click to toggle source
# File lib/tml/session.rb, line 171
def pop_block_options
  return unless @block_options
  @block_options.pop
end
preferred_locale(locales) click to toggle source
# File lib/tml/session.rb, line 78
def preferred_locale(locales)
  return application.default_locale unless locales
  locales = locales.is_a?(String) ? locales.split(',') : locales

  locales.each do |locale|
    locale = Tml::Language.normalize_locale(locale)
    return locale if application.locales.include?(locale)
    locale = locale.split('-').first
    return locale if application.locales.include?(locale)
  end

  application.default_locale
end
push_block_options(opts) click to toggle source
# File lib/tml/session.rb, line 167
def push_block_options(opts)
  block_options_queue.push(opts)
end
reset() click to toggle source
# File lib/tml/session.rb, line 92
def reset
  self.application= nil
  self.current_user= nil
  self.current_language= nil
  self.current_translator= nil
  self.current_source= nil
  self.block_options= nil
end
source_language() click to toggle source
# File lib/tml/session.rb, line 109
def source_language
  locale = block_option(:locale)
  locale ? application.language(locale) : application.language
end
target_language() click to toggle source
# File lib/tml/session.rb, line 114
def target_language
  target_locale = block_option(:target_locale)
  language = (target_locale ? application.language(target_locale) : current_language)
  language || Tml.config.default_language
end
translate(label, description = '', tokens = {}, options = {}) click to toggle source
# File lib/tml/session.rb, line 124
def translate(label, description = '', tokens = {}, options = {})
  params = Tml::Utils.normalize_tr_params(label, description, tokens, options)
  return params[:label] if params[:label].tml_translated?

  params[:options][:caller] ||= caller(1, 1)

  if Tml.config.disabled?
    return Tml.config.default_language.translate(params[:label], params[:tokens], params[:options]).tml_translated
  end

  # Translate individual sentences
  if params[:options][:split]
    text = params[:label]
    sentences = Tml::Utils.split_sentences(text)
    sentences.each do |sentence|
      text = text.gsub(sentence, target_language.translate(sentence, params[:description], params[:tokens], params[:options]))
    end
    return text.tml_translated
  end

  target_language.translate(params).tml_translated
rescue Tml::Exception => ex
  #pp ex, ex.backtrace
  Tml.logger.error(ex.message)
  #Tml.logger.error(ex.message + "\n=> " + ex.backtrace.join("\n=> "))
  label
end
with_block_options(opts) { || ... } click to toggle source
# File lib/tml/session.rb, line 184
def with_block_options(opts)
  push_block_options(opts)
  if block_given?
    ret = yield
  end
  pop_block_options
  ret
end
Also aliased as: with_options
with_options(opts)
Alias for: with_block_options