lib/globalize/localization/db_translate.rb
#Alternative storage mechanism storing the translations in the models
#own tables.
#
#<i>i.e. Globalize::DbTranslate.keep_translations_in_model is true</i>
Alternative storage mechanism storing the translations in the models
own tables.


Alternative storage mechanism storing the translations in the models own tables .
def translate_internal(facets, options)
          facets_string = "[" + facets.map {|facet| ":#{facet}"}.join(", ") + "]"
          class_eval %{
            @@facet_options = {}
            @@globalize_facets = #{facets_string}
            class << self
              def globalize_facets
                @@globalize_facets
              end
              #Returns the localized column name of the supplied attribute for the
              #current locale
              #
              #Useful when you have to build up sql by hand or for AR::Base::find conditions
              #
              #  e.g. Product.find(:all , :conditions = ["\#{Product.localized_facet(:name)} = ?", name])
              #
              # Note: <i>Used when Globalize::DbTranslate.keep_translations_in_model is true</i>
              def localized_facet(facet)
                unless Locale.base?
                  "\#{facet}_\#{Locale.language.code}"
                else
                  facet.to_s
                end
              end
              alias_method :globalize_old_method_missing, :method_missing unless
                respond_to? :globalize_old_method_missing
            end
            def globalize_facets_hash
              @@globalize_facets_hash ||= globalize_facets.inject({}) {|hash, facet|
                hash[facet.to_s] = true; hash
              }
            end
            def non_localized_fields
              @@non_localized_fields ||=
                column_names.map {|cn| cn.intern } - globalize_facets
            end
            #Is field translated?
            #Returns true if translated
            #Warning! Depends on Locale.switch_locale
            def translated?(facet, locale_code = nil)
              localized_method = "\#{facet}_\#{Locale.language.code}"
              Locale.switch_locale(locale_code) do
                localized_method = "\#{facet}_\#{Locale.language.code}"
              end if locale_code
              value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
              return !value.nil?
            end
            extend  Globalize::DbTranslate::InternalStorageClassMethods
          }
          facets.each do |facet|
            bidi = (!(options[facet] && !options[facet][:bidi_embed])).to_s
            class_eval %{
              #Handle facet-specific options (.e.g a bidirectional setting)
              @@facet_options[:#{facet}] ||= {}
              @@facet_options[:#{facet}][:bidi] = #{bidi}
              #Accessor that proxies to the right accessor for the current locale
              def #{facet}
                value = nil
                unless Locale.base?
                  localized_method = "#{facet}_\#{Locale.language.code}"
                  value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
                  value = value ? value : read_attribute(:#{facet}) if #{options[:base_as_default]}
                else
                  value = read_attribute(:#{facet})
                end
                value.nil? ? nil : add_bidi(value, :#{facet})
              end
              #Accessor before typecasting that proxies to the right accessor for the current locale
              def #{facet}_before_type_cast
                unless Locale.base?
                  localized_method = "#{facet}_\#{Locale.language.code}_before_type_cast"
                  value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
                  value = value ? value : read_attribute_before_type_cast('#{facet}') if #{options[:base_as_default]}
                  return value
                else
                  value = read_attribute_before_type_cast('#{facet}')
                end
                value.nil? ? nil : add_bidi(value, :#{facet})
              end
              #Write to appropriate localized attribute
              def #{facet}=(value)
                unless Locale.base?
                  localized_method = "#{facet}_\#{Locale.language.code}"
                  write_attribute(localized_method.to_sym, value) if respond_to?(localized_method.to_sym)
                else
                  write_attribute(:#{facet}, value)
                end
              end
              #Is field translated?
              #Returns true if untranslated
              def #{facet}_is_base?
                localized_method = "#{facet}_\#{Locale.language.code}"
                value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
                return value.nil?
              end
              #Read base language attribute directly
              def _#{facet}
                value = read_attribute(:#{facet})
                value.nil? ? nil : add_bidi(value, :#{facet})
              end
              #Read base language attribute directly without typecasting
              def _#{facet}_before_type_cast
                read_attribute_before_type_cast('#{facet}')
              end
              #Write base language attribute directly
              def _#{facet}=(value)
                write_attribute(:#{facet}, value)
              end
              def add_bidi(value, facet)
                return value unless Locale.active?
                value.direction = self.send("\#{facet}_is_base?") ?
                  (Locale.base_language ? Locale.base_language.direction : nil) :
                  (Locale.active ? Locale.language.direction : nil)
                  # insert bidi embedding characters, if necessary
                  if @@facet_options[facet][:bidi] &&
                      Locale.language && Locale.language.direction && value.direction
                    if Locale.language.direction == 'ltr' && value.direction == 'rtl'
                      bidi_str = "\xe2\x80\xab" + value + "\xe2\x80\xac"
                      bidi_str.direction = value.direction
                      return bidi_str
                    elsif Locale.language.direction == 'rtl' && value.direction == 'ltr'
                      bidi_str = "\xe2\x80\xaa" + value + "\xe2\x80\xac"
                      bidi_str.direction = value.direction
                      return bidi_str
                    end
                  end
                  return value
              end
              protected :add_bidi
            }
          end
        end
def translate_internal ( facets , options ) facets_string = "[" + facets . map { | facet | ":#{facet}" } . join ( ", " ) + "]" class_eval %{
            @@facet_options = {}
            @@globalize_facets = #{facets_string}

            class << self

              def globalize_facets
                @@globalize_facets
              end


              #Returns the localized column name of the supplied attribute for the
              #current locale
              #
              #Useful when you have to build up sql by hand or for AR::Base::find conditions
              #
              #  e.g. Product.find(:all , :conditions = ["\#{Product.localized_facet(:name)} = ?", name])
              #
              # Note: <i>Used when Globalize::DbTranslate.keep_translations_in_model is true</i>
              def localized_facet(facet)
                unless Locale.base?
                  "\#{facet}_\#{Locale.language.code}"
                else
                  facet.to_s
                end
              end

              alias_method :globalize_old_method_missing, :method_missing unless
                respond_to? :globalize_old_method_missing
            end

            def globalize_facets_hash
              @@globalize_facets_hash ||= globalize_facets.inject({}) {|hash, facet|
                hash[facet.to_s] = true; hash
              }
            end

            def non_localized_fields
              @@non_localized_fields ||=
                column_names.map {|cn| cn.intern } - globalize_facets
            end

            #Is field translated?
            #Returns true if translated
            #Warning! Depends on Locale.switch_locale
            def translated?(facet, locale_code = nil)
              localized_method = "\#{facet}_\#{Locale.language.code}"

              Locale.switch_locale(locale_code) do
                localized_method = "\#{facet}_\#{Locale.language.code}"
              end if locale_code

              value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
              return !value.nil?
            end

            extend  Globalize::DbTranslate::InternalStorageClassMethods
          } facets . each do | facet | bidi = ( ! ( options [ facet ] && ! options [ facet ] [ :bidi_embed ] ) ) . to_s class_eval %{

              #Handle facet-specific options (.e.g a bidirectional setting)
              @@facet_options[:#{facet}] ||= {}
              @@facet_options[:#{facet}][:bidi] = #{bidi}

              #Accessor that proxies to the right accessor for the current locale
              def #{facet}
                value = nil
                unless Locale.base?
                  localized_method = "#{facet}_\#{Locale.language.code}"
                  value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
                  value = value ? value : read_attribute(:#{facet}) if #{options[:base_as_default]}
                else
                  value = read_attribute(:#{facet})
                end
                value.nil? ? nil : add_bidi(value, :#{facet})
              end

              #Accessor before typecasting that proxies to the right accessor for the current locale
              def #{facet}_before_type_cast
                unless Locale.base?
                  localized_method = "#{facet}_\#{Locale.language.code}_before_type_cast"
                  value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
                  value = value ? value : read_attribute_before_type_cast('#{facet}') if #{options[:base_as_default]}
                  return value
                else
                  value = read_attribute_before_type_cast('#{facet}')
                end
                value.nil? ? nil : add_bidi(value, :#{facet})
              end

              #Write to appropriate localized attribute
              def #{facet}=(value)
                unless Locale.base?
                  localized_method = "#{facet}_\#{Locale.language.code}"
                  write_attribute(localized_method.to_sym, value) if respond_to?(localized_method.to_sym)
                else
                  write_attribute(:#{facet}, value)
                end
              end

              #Is field translated?
              #Returns true if untranslated
              def #{facet}_is_base?
                localized_method = "#{facet}_\#{Locale.language.code}"
                value = send(localized_method.to_sym) if respond_to?(localized_method.to_sym)
                return value.nil?
              end

              #Read base language attribute directly
              def _#{facet}
                value = read_attribute(:#{facet})
                value.nil? ? nil : add_bidi(value, :#{facet})
              end

              #Read base language attribute directly without typecasting
              def _#{facet}_before_type_cast
                read_attribute_before_type_cast('#{facet}')
              end

              #Write base language attribute directly
              def _#{facet}=(value)
                write_attribute(:#{facet}, value)
              end

              def add_bidi(value, facet)
                return value unless Locale.active?
                value.direction = self.send("\#{facet}_is_base?") ?
                  (Locale.base_language ? Locale.base_language.direction : nil) :
                  (Locale.active ? Locale.language.direction : nil)

                  # insert bidi embedding characters, if necessary
                  if @@facet_options[facet][:bidi] &&
                      Locale.language && Locale.language.direction && value.direction
                    if Locale.language.direction == 'ltr' && value.direction == 'rtl'
                      bidi_str = "\xe2\x80\xab" + value + "\xe2\x80\xac"
                      bidi_str.direction = value.direction
                      return bidi_str
                    elsif Locale.language.direction == 'rtl' && value.direction == 'ltr'
                      bidi_str = "\xe2\x80\xaa" + value + "\xe2\x80\xac"
                      bidi_str.direction = value.direction
                      return bidi_str
                    end
                  end
                  return value
              end

              protected :add_bidi
            } end end
Alternative storage mechanism storing the translations in the models
own tables.
Alternative storage mechanism storing the translations in the models own tables .
facets
<not_specific>
options
<not_specific>