Class: Translation

Inherits:
Forest::ApplicationRecord show all
Defined in:
app/models/translation.rb

Constant Summary collapse

CACHE_KEY =
'forest_translations'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Forest::ApplicationRecord

cache_key, cache_key_name, csv_columns, #expire_cache_key, expire_cache_key, statusable?, #statusable?, to_csv_template, #to_friendly_param, #to_label, #to_select2_response, #to_select2_selection, versionable, #versionable

Class Method Details

.expire_cache!Object



19
20
21
# File 'app/models/translation.rb', line 19

def self.expire_cache!
  Rails.cache.delete self::CACHE_KEY
end

.for(key) ⇒ Object



11
12
13
# File 'app/models/translation.rb', line 11

def self.for(key)
  self.get(key).try(:value)
end

.get(key) ⇒ Object



15
16
17
# File 'app/models/translation.rb', line 15

def self.get(key)
  self.translations.select { |setting| setting.key == key.to_s }.first
end

.initialize_from_i18nObject

TODO: Update to support multiple locales



24
25
26
27
28
29
30
31
32
33
34
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
66
67
68
69
70
71
72
# File 'app/models/translation.rb', line 24

def self.initialize_from_i18n
  I18n.backend.send(:init_translations) unless I18n.backend.initialized?
  translations = I18n.backend.send(:translations).dig(:en, :forest, :translations).presence || []

  translations.each do |original_translation|
    translation = original_translation.dup

    k = translation.delete(:key)
    v = translation.delete(:value)
    d = translation.delete(:description)

    if k.blank? && v.blank?
      if translation.is_a?(Hash)
        k = translation.keys.first.to_s
        v = translation[k]
      else
        k = translation[0].to_s
        v = translation[1]
      end
    end

    if [k, v].all?(&:present?)
      t = Translation.get(k)
      if t.blank?
        t = Translation.new(key: k)
      end

      t.value = v
      t.description = d

      if t.new_record?
        logger.info { "[Forest][Translation] Creating new translation for #{k}" }
        t.save
      elsif t.updated_at == t.created_at
        if t.changed?
          logger.info { "[Forest][Translation] Updating value for translation key #{k}" }
          to_update = {}
          to_update[:value] = v if t.value_changed?
          to_update[:description] = v if t.description_changed?
          t.update_columns(to_update)
        end
      end
    else
      logger.warn { "[Forest][Translation] Warning: unable to create translation for #{original_translation}. Key or value is blank." }
    end
  end

  expire_cache!
end

.resource_descriptionObject



74
75
76
# File 'app/models/translation.rb', line 74

def self.resource_description
  "Translations may be used in multilingual websites or to define values for particular setting or menu keys."
end

Instance Method Details

#display_nameObject



78
79
80
# File 'app/models/translation.rb', line 78

def display_name
  key
end