Module: ActiveTranslation::Translatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_translation/translatable.rb

Instance Method Summary collapse

Instance Method Details

#fully_translated?(attribute_types = :auto) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/active_translation/translatable.rb', line 88

def fully_translated?(attribute_types = :auto)
  case attribute_types
  when :auto, :auto_only
    !translations_missing?
  when :manual, :manual_only
    !manual_translations_missing?
  when :all, :include_manual
    !translations_missing? && !manual_translations_missing?
  else
    raise ArgumentError, "acceptable arguments are [:auto, :auto_only, :manual, :manual_only, :all, :include_manual]"
  end
end

#manual_translations_missing?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_translation/translatable.rb', line 101

def manual_translations_missing?
  return false unless conditions_met?

  translatable_locales.each do |locale|
    translation_config[:manual_attributes].each do |attribute|
      next if read_attribute(attribute).blank?

      return true unless translation = translations.find_by(locale: locale)
      return true unless translation.translated_attributes.keys.include?(attribute)
    end
  end

  false
end

#outdated_translationsObject



116
117
118
# File 'lib/active_translation/translatable.rb', line 116

def outdated_translations
  translations.select { _1.outdated? }
end

#translatable_localesObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_translation/translatable.rb', line 120

def translatable_locales
  case translation_config[:locales]
  when Symbol
    if translation_config[:locales] == :all
      I18n.available_locales - [ I18n.default_locale ]
    else
      send(translation_config[:locales])
    end
  when Proc
    instance_exec(&translation_config[:locales])
  when Array
    translation_config[:locales]
  end
end

#translate!Object



149
150
151
152
153
# File 'lib/active_translation/translatable.rb', line 149

def translate!
  translatable_locales.each do |locale|
    TranslationJob.perform_later(self, locale.to_s, translation_checksum)
  end
end

#translate_attribute(attribute, locale) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/active_translation/translatable.rb', line 161

def translate_attribute(attribute, locale)
  return nil if send(attribute).nil?

  cached_translation = Cache.lookup(
    locale:,
    text: send(attribute),
  )

  translated_text = cached_translation || ActiveTranslation::GoogleTranslate.translate(target_language_code: locale, text: send(attribute))

  should_cache = case translation_config[:cache]
  when TrueClass then true
  when String, Symbol then attribute.to_s == translation_config[:cache].to_s
  when Array then translation_config[:cache].map(&:to_s).include?(attribute.to_s)
  else false
  end

  if should_cache
    Cache.add!(
      locale:,
      original_text: send(attribute),
      translated_text:,
    )
  end

  translated_text
end

#translate_if_neededObject



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/active_translation/translatable.rb', line 135

def translate_if_needed
  translations.delete_all and return unless conditions_met?

  return unless translatable_attributes_changed? || condition_checks_changed? || translations_outdated? || translations_missing?

  translatable_locales.each do |locale|
    translation = translations.find_or_create_by(locale: locale.to_s)

    if translation.new_record? || translation.outdated?
      TranslationJob.perform_later(self, locale.to_s, translation_checksum)
    end
  end
end

#translate_now!(locales = translatable_locales) ⇒ Object



155
156
157
158
159
# File 'lib/active_translation/translatable.rb', line 155

def translate_now!(locales = translatable_locales)
  Array(locales).each do |locale|
    TranslationJob.perform_now(self, locale.to_s, translation_checksum)
  end
end

#translation_cached?(attribute, locale) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
# File 'lib/active_translation/translatable.rb', line 189

def translation_cached?(attribute, locale)
  ActiveTranslation::Cache.find_by(
    checksum: text_checksum(send(attribute)),
    locale:,
  )
end

#translation_checksumObject



196
197
198
199
# File 'lib/active_translation/translatable.rb', line 196

def translation_checksum
  values = translatable_attribute_names.map { |attr| read_attribute(attr).to_s }
  Digest::MD5.hexdigest(values.join)
end

#translations_missing?Boolean

translations are “missing” if they are not manual, the translatable attribute isn’t blank and there’s no translation for that attribute for all locales

Returns:

  • (Boolean)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/active_translation/translatable.rb', line 203

def translations_missing?
  return false unless conditions_met?

  translatable_locales.each do |locale|
    translatable_attribute_names.each do |attribute|
      next if read_attribute(attribute).blank?

      return true unless translation = translations.find_by(locale: locale)
      return true unless translation.translated_attributes.keys.include?(attribute)
    end
  end

  false
end

#translations_outdated?Boolean

Returns:

  • (Boolean)


218
219
220
221
222
223
# File 'lib/active_translation/translatable.rb', line 218

def translations_outdated?
  return false unless conditions_met?
  return true if translations.map(&:outdated?).any?

  false
end