Module: Tr8nClientSdk::ActionCommonMethods

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

Instance Method Summary collapse

Instance Method Details

#tr(label, description = '', tokens = {}, options = {}) ⇒ Object

There are three ways to call the tr method

tr(label, desc = “”, tokens = {}, options = {}) or tr(label, tokens = {}, options = {}) or tr(:label => label, :description => “”, :tokens => {}, :options => {})



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 43

def tr(label, description = '', tokens = {}, options = {})
  return label.html_safe if label.tr8n_translated?

  params = Tr8n::Utils.normalize_tr_params(label, description, tokens, options)
  params[:options][:caller] = caller

  if request
    params[:options][:url]  = request.url
    params[:options][:host] = request.env['HTTP_HOST']
  end

  if Tr8n.config.disabled?
    return Tr8n.config.default_language.translate(params[:label], params[:tokens], params[:options]).tr8n_translated.html_safe
  end

  # Translate individual sentences
  if params[:options][:split]
    text = params[:label]
    sentences = Tr8n::Utils.split_by_sentence(text)
    sentences.each do |sentence|
      text = text.gsub(sentence, tr8n_current_language.translate(sentence, params[:description], params[:tokens], params[:options]))
    end
    return text.tr8n_translated.html_safe
  end

  tr8n_current_language.translate(params).tr8n_translated.html_safe
rescue Tr8n::Exception => ex
  #pp ex, ex.backtrace
  Tr8n.logger.error(ex.message)
  #Tr8n.logger.error(ex.message + "\n=> " + ex.backtrace.join("\n=> "))
  label
end

#tr8n_applicationObject

Common methods - wrappers



102
103
104
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 102

def tr8n_application
  Tr8n.session.application
end

#tr8n_current_languageObject



114
115
116
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 114

def tr8n_current_language
  Tr8n.session.current_language
end

#tr8n_current_translatorObject



110
111
112
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 110

def tr8n_current_translator
  Tr8n.session.current_translator
end

#tr8n_current_userObject



106
107
108
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 106

def tr8n_current_user
  Tr8n.session.current_user
end

#trfe(label, desc = "", tokens = {}, options = {}) ⇒ Object

flash error



89
90
91
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 89

def trfe(label, desc = "", tokens = {}, options = {})
  flash[:trfe] = tr(Tr8n::Utils.normalize_tr_params(label, desc, tokens, options))
end

#trfn(label, desc = "", tokens = {}, options = {}) ⇒ Object

flash notice



84
85
86
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 84

def trfn(label, desc = "", tokens = {}, options = {})
  flash[:trfn] = tr(Tr8n::Utils.normalize_tr_params(label, desc, tokens, options))
end

#trfw(label, desc = "", tokens = {}, options = {}) ⇒ Object

flash warning



94
95
96
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 94

def trfw(label, desc = "", tokens = {}, options = {})
  flash[:trfw] = tr(Tr8n::Utils.normalize_tr_params(label, desc, tokens, options))
end

#trl(label, description = "", tokens = {}, options = {}) ⇒ Object

for translating labels



77
78
79
80
81
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 77

def trl(label, description = "", tokens = {}, options = {})
  params = Tr8n::Utils.normalize_tr_params(label, description, tokens, options)
  params[:options][:skip_decorations] = true
  tr(params)
end