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
-
#allowed_locales ⇒ Object
def force_on_update? return false end.
-
#create_translation ⇒ Object
Creates a new translation upon updating the parent record.
-
#create_translation? ⇒ Boolean
Returns whether a new translation should be created upon updating the parent record.
- #create_translation_for_locale?(locale) ⇒ Boolean
-
#hmt_locale ⇒ Object
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.
- #locales ⇒ Object
- #locales=(codes) ⇒ Object
- #localize=(loc) ⇒ Object
- #queue_translation(loc) ⇒ Object
- #queue_translations ⇒ Object
- #super_locales ⇒ Object
-
#translated_columns ⇒ Object
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. - #update_all_attributes_translation(loc, origin_locale) ⇒ Object
-
#update_translation(attrib, loc, origin_locale) ⇒ Object
Updates the last translation’s changes by appending the current translation changes.
- #update_translation!(attrib, loc, origin_locale, options = {}) ⇒ Object
-
#update_translation? ⇒ Boolean
Returns whether the last translation should be updated upon updating the parent record.
- #update_translations! ⇒ Object
Instance Method Details
#allowed_locales ⇒ Object
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_translation ⇒ Object
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.
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
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_locale ⇒ Object
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 |
#locales ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/has_many_translations/creation.rb', line 213 def locales if allowed_locales retloc = [:default_locales] ? ([: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 ||= ([:locales] && I18n && Google) ? ([:locales] & Google::Language::Languages.keys) : (Google::Language::Languages.keys & I18n.available_locales.map{|l|l.to_s}) if [:languages] retloc = retloc.empty? ? [:languages] : ([: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
203 204 205 |
# File 'lib/has_many_translations/creation.rb', line 203 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_translations ⇒ Object
207 208 209 210 211 |
# File 'lib/has_many_translations/creation.rb', line 207 def queue_translations self.locales.each do |loc| queue_translation(loc) end end |
#super_locales ⇒ Object
228 229 230 231 232 |
# File 'lib/has_many_translations/creation.rb', line 228 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_columns ⇒ Object
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.
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/has_many_translations/creation.rb', line 191 def translated_columns textual_columns = self.class.columns.map{|c|c.type == :string || c.type == :text ? c.name : nil}.compact if self.[:except] textual_columns = textual_columns - self.[:except] elsif self.[:only] textual_columns = textual_columns & self.[: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 178 179 180 181 182 |
# File 'lib/has_many_translations/creation.rb', line 169 def update_translation!(attrib, loc, origin_locale, = {}) @translator ||= ToLang.start(HmtSettings.google_api_key) # 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) unless (loc.to_s == origin_locale.to_s) unless try(attrib).nil? translation_val = try(attrib).translate(loc.to_s, :from => origin_locale.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 end 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.
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 |