Module: HasManyTranslations::Creation::InstanceMethods

Defined in:
lib/has_many_translations/creation.rb

Overview

Instance methods that determine whether to save a translation and actually perform the save.

Instance Method Summary collapse

Instance Method Details

#allowed_localesObject

def force_on_update?

  return false
end


75
76
77
78
# File 'lib/has_many_translations/creation.rb', line 75

def allowed_locales
  t = TranslationSpec.first(:conditions => {:translated_id => self.id,  :translated_type  => self.class.to_s})
  t.blank? ? nil : t.codes.split(',').map{|c| c.to_sym}
end

#create_translationObject

Creates a new translation upon updating the parent record.



124
125
126
127
128
# File 'lib/has_many_translations/creation.rb', line 124

def create_translation
  translation.create(translation_attributes)
  #reset_translation_changes
  #reset_translation
end

#create_translation?Boolean

Returns whether a new translation should be created upon updating the parent record.

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
# File 'lib/has_many_translations/creation.rb', line 105

def create_translation?
  #determine if locale parameter is supported by this translated 
  # find out if we have a table created for all locales
  # glob I18n.available_locales with whatever we use for the "study" available_locales
  # I18n.available_locales.include? 
  #!translation_changes.blank?
  true
end

#create_translation_for_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
# File 'lib/has_many_translations/creation.rb', line 114

def create_translation_for_locale?(locale)
  #determine if locale parameter is supported by this translated 
  # find out if we have a table created for all locales
  # glob I18n.available_locales with whatever we use for the "study" available_locales
  # I18n.available_locales.include? 
  locales = Google::Language::Languages.keys & I18n.available_locales.map{|l|l.to_s}
  locales.include?(locales)
end

#hmt_localeObject

def hmt_default_locale

  return default_locale.to_sym if respond_to?(:default_locale)
  return self.class.default_locale.to_sym if self.class.respond_to?(:default_locale)
  I18n.default_locale
end


99
100
101
102
# File 'lib/has_many_translations/creation.rb', line 99

def hmt_locale
  @hmt_locale = respond_to?(:locale) ? self.locale.to_s : self.class.respond_to?(:locale) ? self.class.locale.to_s : I18n.locale.to_s
  
end

#localesObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/has_many_translations/creation.rb', line 208

def locales
  if allowed_locales
    retloc = has_many_translations_options[:default_locales] ? (has_many_translations_options[:default_locales] & allowed_locales.map{|l|l.to_s}) : allowed_locales.map{|l|l.to_s}
  elsif super_locales.present? 
    super_locales.each do |sloc|
      retloc.nil? ? retloc = eval("self.#{sloc}.locales") : retloc | eval("self.#{sloc}.locales")
    end
  end
  retloc ||= (has_many_translations_options[:locales] && I18n && Google) ? (has_many_translations_options[:locales] & Google::Language::Languages.keys) : (Google::Language::Languages.keys & I18n.available_locales.map{|l|l.to_s})
  if has_many_translations_options[:languages]
    retloc = retloc.empty? ? has_many_translations_options[:languages] : (has_many_translations_options[:languages] & retloc) 
  end
  # I18n.available_locales.map(&:to_s)
end

#locales=(codes) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/has_many_translations/creation.rb', line 80

def locales=(codes)
  t = TranslationSpec.first(:conditions => {:translated_id => self.id,  :translated_type  => self.class.to_s})
  unless t.blank?
    t.update_attribute('codes', codes.map{|c|c.to_s}.join(','))
  else
    TranslationSpec.create(:translated_id => self.id, :translated_type => self.class.to_s, :codes => codes.map{|c|c.to_s}.join(','))
  end
end

#localize=(loc) ⇒ Object



89
90
91
# File 'lib/has_many_translations/creation.rb', line 89

def localize=(loc)
  @locale = loc
end

#queue_translation(loc) ⇒ Object



198
199
200
# File 'lib/has_many_translations/creation.rb', line 198

def queue_translation(loc)
  ActiveQueue::Job.new(:val => { :translated_id => self.id, :translated_type => self.class.to_s, :origin_locale => self.hmt_locale, :destination_locale => loc },:job_klass => "TranslationJobs::AutoTranslateJob",:adapter => "resque").enqueue
end

#queue_translationsObject



202
203
204
205
206
# File 'lib/has_many_translations/creation.rb', line 202

def queue_translations
  self.locales.each do |loc|
    queue_translation(loc)
  end
end

#super_localesObject



223
224
225
226
227
# File 'lib/has_many_translations/creation.rb', line 223

def super_locales 
  if I18n && Google
    self.class.reflect_on_all_associations(:belongs_to).map{|a|eval("#{a.name.to_s.capitalize}.translated?") ? a.name.to_s : nil}.compact
  end
end

#translated_columnsObject

Returns an array of column names that should be included in the translation

If has_many_translations_options[:only] is specified, only those columns will be translationed. Otherwise, if has_many_translations_options[:except] is specified, all columns will be translationed other than those specified. Without either option, the default is to translation all text & string columns. At any rate, the four “automagic” timestamp columns maintained by Rails are never translationed.



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/has_many_translations/creation.rb', line 186

def translated_columns
  textual_columns = self.class.columns.map{|c|c.type == :string || c.type == :text ? c.name : nil}.compact
  
  if self.has_many_translations_options[:except] 
    textual_columns = textual_columns - self.has_many_translations_options[:except]
  elsif self.has_many_translations_options[:only] 
    textual_columns = textual_columns & self.has_many_translations_options[:only]
  end
  
  return textual_columns
end

#update_all_attributes_translation(loc, origin_locale) ⇒ Object



156
157
158
159
160
# File 'lib/has_many_translations/creation.rb', line 156

def update_all_attributes_translation(loc, origin_locale)
  self.translated_columns.each do |attrib|
    update_translation(attrib, loc, origin_locale)
  end
end

#update_translation(attrib, loc, origin_locale) ⇒ Object

Updates the last translation’s changes by appending the current translation changes.



163
164
165
166
167
# File 'lib/has_many_translations/creation.rb', line 163

def update_translation(attrib, loc, origin_locale)
  if !translations.first(:conditions => {:model_attribute => attrib, :locale_code => loc.to_s}) || self.force_on_update?
    update_translation!(attrib, loc, origin_locale.to_s)
  end
end

#update_translation!(attrib, loc, origin_locale, options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/has_many_translations/creation.rb', line 169

def update_translation!(attrib, loc, origin_locale, options = {})
   @translator ||= Translate::RTranslate.new
   if defined? HmtSettings
     @translator.key = HmtSettings.google_api_key
   end
   translation_val = @translator.translate(try(attrib), :from => origin_locale.to_s, :to => loc.to_s)
   # TODO: Should this be "find_or_create"?
   translations.create(:model_attribute => attrib, :locale_code => loc.to_s, :value => translation_val, :locale_name => Google::Language::Languages[loc.to_s], :machine_translation => true, :origin_locale_code => origin_locale ) unless translation_val.nil? || translation_val.match('Error: ')
end

#update_translation?Boolean

Returns whether the last translation should be updated upon updating the parent record. This method is overridden in HasManyTranslations::Control to account for a control block that merges changes onto the previous translation.

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
# File 'lib/has_many_translations/creation.rb', line 145

def update_translation?
  unless self.translations.blank? || self.translations.first.origin_locale_code == self.hmt_locale
    dirty_translations = self.translations.all(:conditions => {:translated_id => self.id, :locale_code => self.hmt_locale})
    dirty_translations.each do |dt|
      dt.value = try(dt.model_attribute)
      dt.save
    end
    return false
  end
end

#update_translations!Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/has_many_translations/creation.rb', line 131

def update_translations!
   #translated_columns.each do |attrib|
      self.locales.each do |loc|
          # put this in a option check blog to determine if the job should be queued? 
          queue_translation(loc)
          #ActiveQueue::Queue.enqueue(TranslationJobs::AutoTranslateJob,{:translated_id => self.id,:translated_type  => self.type, :origin_locale =>  self.hmt_locale, :destination_locale => loc.to_s})
          #update_all_attributes_translation(loc, self.hmt_locale)
      end
   #end
end