class LostInTranslations::Translator::Base

Public Class Methods

assign_translation(object, field, value, locale) click to toggle source
# File lib/lost_in_translations/translator/base.rb, line 14
def self.assign_translation(object, field, value, locale)
  data = translation_data(object)

  translations = data[locale.to_sym] || data[locale.to_s]

  translations ||= data[locale.to_sym] = {}

  translations[field.to_sym] = value
end
check_forcing_locale(object, locale) click to toggle source
# File lib/lost_in_translations/translator/base.rb, line 48
def self.check_forcing_locale(object, locale)
  force_locale_field = object.class.force_locale_field

  if object.respond_to?(force_locale_field)
    force_locale = object.send(force_locale_field)

    return force_locale unless force_locale.to_s.length < 2
  end

  locale
end
translate(object, field, original_locale) click to toggle source
# File lib/lost_in_translations/translator/base.rb, line 4
def self.translate(object, field, original_locale)
  locale = check_forcing_locale(object, original_locale)

  data = translation_data(object)

  translations = data[locale.to_sym] || data[locale.to_s] || {}

  translations[field.to_sym] || translations[field.to_s]
end
translation_data(object) click to toggle source
# File lib/lost_in_translations/translator/base.rb, line 24
def self.translation_data(object)
  translation_data_field = translation_data_field(object)

  data = object.send(translation_data_field)

  if data.nil? && object.respond_to?("#{translation_data_field}=")
    data = object.send("#{translation_data_field}=", {})
  end

  data
end
translation_data_field(object) click to toggle source
# File lib/lost_in_translations/translator/base.rb, line 36
def self.translation_data_field(object)
  translation_data_field = object.class.translation_data_field

  unless object.respond_to?(translation_data_field)
    raise \
      NotImplementedError,
      "#{object.class.name} does not implement .#{translation_data_field}"
  end

  translation_data_field
end