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 => {})



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 35

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::TranslationKey.substitute_tokens(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
  Tr8n::Logger.error("ERROR: #{label}")
  Tr8n::Logger.error(ex.message + "\n=> " + ex.backtrace.join("\n=> "))
  label
end

#tr8n_applicationObject

Common methods - wrappers



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

def tr8n_application
  Tr8n.session.application
end

#tr8n_current_languageObject



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

def tr8n_current_language
  Tr8n.session.current_language
end

#tr8n_current_translatorObject



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

def tr8n_current_translator
  Tr8n.session.current_translator
end

#tr8n_current_userObject



97
98
99
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 97

def tr8n_current_user
  Tr8n.session.current_user
end

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

flash error



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

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



75
76
77
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 75

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



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

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



68
69
70
71
72
# File 'lib/tr8n_client_sdk/extensions/action_common_methods.rb', line 68

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