Module: I18nRailsHelpers

Defined in:
lib/i18n_rails_helpers.rb,
lib/i18n_rails_helpers/railtie.rb

Defined Under Namespace

Classes: Railtie

Instance Method Summary collapse

Instance Method Details

#t_action(action = nil, model = nil) ⇒ Object

Returns translated string for current action.

If no action is given, it uses the current action.

The translation file comes with the plugin supports the following actions by default: index, edit, show, new, delete, back, next, previous

Example:

t_action('delete')        => 'Löschen'
t_action                  => 'Ändern'  # when called in an edit view


84
85
86
87
# File 'lib/i18n_rails_helpers.rb', line 84

def t_action(action = nil, model = nil)
  action ||= action_name
  I18n::translate(action, :scope => 'crud.action', :model => t_model(model))
end

#t_attr(attribute, model = nil) ⇒ Object

Returns translated name for the given attribute.

If no model is given, it uses the controller name to guess the model by singularize it.

Example:

t_attr('first_name', Patient) => 'Vorname'
t_attr('first_name')          => 'Vorname' # when called in patients_controller views


22
23
24
25
26
27
28
29
# File 'lib/i18n_rails_helpers.rb', line 22

def t_attr(attribute, model = nil)
  if model.is_a? Class
    model_name = model.name.underscore
  elsif model.nil?
    model_name = controller_name.singularize
  end
  I18n::translate(attribute, :scope => [:activerecord, :attributes, model_name])
end

#t_confirm_delete(record) ⇒ Object

Returns translated deletion confirmation for record.

It uses record.to_s in the message.

Example:

t_confirm_delete(@account) => 'Konto Kasse wirklich löschen'


96
97
98
# File 'lib/i18n_rails_helpers.rb', line 96

def t_confirm_delete(record)
  I18n::translate('messages.confirm_delete', :model => t_model(record), :record => record.to_s)
end

#t_model(model = nil) ⇒ Object

Returns translated name for the given model.

If no model is given, it uses the controller name to guess the model by singularize it. model can be both a class or an actual instance.

Example:

t_model(Account)     => 'Konto'
t_model(Account.new) => 'Konto'
t_model              => 'Konto' # when called in patients_controller views


41
42
43
44
45
46
47
48
49
50
# File 'lib/i18n_rails_helpers.rb', line 41

def t_model(model = nil)
  if model.is_a? Class
    model_name = model.name.underscore
  elsif model.nil?
    model_name = controller_name.singularize
  else
    model_name = model.class.name.underscore
  end
  I18n::translate(model_name, :scope => [:activerecord, :models])
end

#t_page_headObject

Returns translated identifier



5
6
7
8
9
10
11
# File 'lib/i18n_rails_helpers.rb', line 5

def t_page_head
  if params[:id] and resource
    return "%s %s" % [t_title, resource.to_s]
  else
    return t_title
  end
end

#t_title(action = nil, model = nil) ⇒ Object Also known as: t_crud

Returns translated title for current action on model.

If no action is given, it uses the current action.

If no model is given, it uses the controller name to guess the model by singularize it. model can be both a class or an actual instance.

The translation file comming with the plugin supports the following actions by default: index, edit, show, new, delete

Example:

t_title('new', Account') => 'Konto anlegen'
t_title('delete')        => 'Konto löschen' # when called in accounts_controller views
t_title                  => 'Konto ändern'  # when called in accounts_controller edit view


67
68
69
70
# File 'lib/i18n_rails_helpers.rb', line 67

def t_title(action = nil, model = nil)
  action ||= action_name
  I18n::translate(action, :scope => 'crud.title', :model => t_model(model))
end