| Module | Globalize::DbTranslate::ClassMethods |
| In: |
vendor/plugins/globalize/lib/globalize/localization/db_translate.rb
|
Specifies fields that can be translated. These are normal ActiveRecord fields, with corresponding database columns, but they are shadowed by translations in a special translation table. All the translation stuff is done behind the scenes.
class Product < ActiveRecord::Base
translates :name, :description
end
Locale.set("en_US")
product.name -> guitar
Locale.set("es_ES")
product.name -> guitarra
# File vendor/plugins/globalize/lib/globalize/localization/db_translate.rb, line 40
40: def translates(*facets)
41:
42: # parse out options hash
43: options = facets.pop if facets.last.kind_of? Hash
44: options ||= {}
45:
46: facets_string = "[" + facets.map {|facet| ":#{facet}"}.join(", ") + "]"
47: class_eval "@@facet_options = {}\nattr_writer :fully_loaded\ndef fully_loaded?; @fully_loaded; end\n@@globalize_facets = \#{facets_string}\n@@preload_facets ||= @@globalize_facets\nclass << self\n\ndef sqlite?; connection.kind_of? ActiveRecord::ConnectionAdapters::SQLiteAdapter end\n\ndef globalize_facets\n@@globalize_facets\nend\n\ndef globalize_facets_hash\n@@globalize_facets_hash ||= globalize_facets.inject({}) {|hash, facet|\nhash[facet.to_s] = true; hash\n}\nend\n\ndef untranslated_fields\n@@untranslated_fields ||=\ncolumn_names.map {|cn| cn.intern } - globalize_facets\nend\n\ndef preload_facets; @@preload_facets; end\ndef postload_facets\n@@postload_facets ||= @@globalize_facets - @@preload_facets\nend\nalias_method :globalize_old_find, :find unless\nrespond_to? :globalize_old_find\nend\nalias_method :globalize_old_reload, :reload\nalias_method :globalize_old_destroy, :destroy\nalias_method :globalize_old_create_or_update, :create_or_update\n\ninclude Globalize::DbTranslate::TranslateObjectMethods\nextend Globalize::DbTranslate::TranslateClassMethods\n\n"
48:
49: facets.each do |facet|
50: bidi = (!(options[facet] && !options[facet][:bidi_embed])).to_s
51: class_eval "@@facet_options[:\#{facet}] ||= {}\n@@facet_options[:\#{facet}][:bidi] = \#{bidi}\n\ndef \#{facet}\nif not_original_language\nraise WrongLanguageError.new(@original_language, Locale.language)\nend\nload_other_translations if\n!fully_loaded? && !self.class.preload_facets.include?(:\#{facet})\nresult = read_attribute(:\#{facet})\nreturn nil if result.nil?\nresult.direction = \#{facet}_is_base? ?\nLocale.base_language.direction :\n@original_language.direction\n\n# insert bidi embedding characters, if necessary\nif @@facet_options[:\#{facet}][:bidi] &&\nLocale.language && Locale.language.direction && result.direction\nif Locale.language.direction == 'ltr' && result.direction == 'rtl'\nbidi_str = \"\\xe2\\x80\\xab\" + result + \"\\xe2\\x80\\xac\"\nbidi_str.direction = result.direction\nreturn bidi_str\nelsif Locale.language.direction == 'rtl' && result.direction == 'ltr'\nbidi_str = \"\\xe2\\x80\\xaa\" + result + \"\\xe2\\x80\\xac\"\nbidi_str.direction = result.direction\nreturn bidi_str\nend\nend\n\nreturn result\nend\n\ndef \#{facet}=(arg)\nraise WrongLanguageError.new(@original_language, Locale.language) if\nnot_original_language\nwrite_attribute(:\#{facet}, arg)\nend\n\ndef \#{facet}_is_base?\nself['\#{facet}_not_base'].nil?\nend\n"
52: end
53:
54: end
Optionally specifies translated fields to be preloaded on find. For instance, in a product catalog, you may want to do a find of the first 10 products:
Product.find(:all, :limit => 10, :order => "name")
But you wouldn’t want to load the complete descriptions and specs of all the products, just the names and summaries. So you’d specify:
class Product < ActiveRecord::Base
translates :name, :summary, :description, :specs
translates_preload :name, :summary
...
end
By default (if no translates_preload is specified), Globalize will preload the first field given to translates. It will also fully load on a find(:first) or when :translate_all => true is given as a find option.
# File vendor/plugins/globalize/lib/globalize/localization/db_translate.rb, line 158
158: def translates_preload(*facets)
159: module_eval "@@preload_facets = facets\n"
160: end