Class: ActiveElement::Components::Util::I18n

Inherits:
Object
  • Object
show all
Defined in:
lib/active_element/components/util/i18n.rb

Overview

Translates various parameters using ‘I18n` library, allows users to specify labels, descriptions, placeholders, etc. for various components in locales files.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ I18n

Returns a new instance of I18n.



14
15
16
# File 'lib/active_element/components/util/i18n.rb', line 14

def initialize(component)
  @component = component
end

Class Method Details

.class_name(val, plural: false) ⇒ Object



9
10
11
12
# File 'lib/active_element/components/util/i18n.rb', line 9

def self.class_name(val, plural: false)
  base = val&.to_s&.underscore&.tr('/', '_')
  plural ? base&.pluralize : base
end

Instance Method Details

#description(field, record: nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/active_element/components/util/i18n.rb', line 25

def description(field, record: nil)
  return nil unless model?(record)

  key = "admin.models.#{model_key(record)}.fields.#{field}.description"
  ::I18n.t(key, default: nil)
end

#format(field, record: nil) ⇒ Object



39
40
41
42
43
44
# File 'lib/active_element/components/util/i18n.rb', line 39

def format(field, record: nil)
  return nil unless model?(record)

  key = "admin.models.#{model_key(record)}.fields.#{field}.format"
  ::I18n.t(key, default: nil)
end

#label(field, record: nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/active_element/components/util/i18n.rb', line 18

def label(field, record: nil)
  return titleize(field) unless model?(record)

  key = "admin.models.#{model_key(record)}.fields.#{field}.label"
  ::I18n.t(key, default: titleize(field))
end

#placeholder(field, record: nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/active_element/components/util/i18n.rb', line 32

def placeholder(field, record: nil)
  return nil unless model?(record)

  key = "admin.models.#{model_key(record)}.fields.#{field}.placeholder"
  ::I18n.t(key, default: nil)
end