Module: Translatable

Defined in:
lib/translatable/active_record/attributes.rb,
lib/translatable.rb,
lib/translatable/version.rb,
lib/translatable/active_record.rb,
lib/translatable/active_record/macro.rb,
lib/translatable/active_record/adapter.rb,
lib/translatable/active_record/relation.rb,
lib/translatable/active_record/translate.rb,
lib/translatable/active_record/class_methods.rb,
lib/translatable/active_record/instance_methods.rb

Overview

Helper class for storing values per locale. Used by Globalize::Adapter to stash and cache attribute values.

Defined Under Namespace

Modules: ActiveRecord

Constant Summary collapse

VERSION =
'0.4.0'

Class Method Summary collapse

Class Method Details

.add_translatable(klass) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/translatable.rb', line 40

def add_translatable(klass)
  if @@translatable.has_key? klass.name
    klass.translated_attribute_names.each do |attr|
      @@translatable[klass.name] << attr unless @@translatable[klass.name].include?(attr)
    end
  else
    @@translatable[klass.name] = klass.translated_attribute_names
  end

  unless klass.translated_serialized_attributes.nil?
    @@translatable[klass.name].map! do |attr|
      serialized = klass.translated_serialized_attributes.reject{|k,v| k != attr}
      serialized.empty? ? attr : serialized
    end
  end
end

.listObject

Hash of models that are translatable (values are the attrs)



36
37
38
# File 'lib/translatable.rb', line 36

def list
  @@translatable ||= Hash.new
end

.localeObject



9
10
11
# File 'lib/translatable.rb', line 9

def locale
  read_locale || I18n.locale
end

.locale=(locale) ⇒ Object



13
14
15
# File 'lib/translatable.rb', line 13

def locale=(locale)
  set_locale(locale)
end

.translation_classObject



31
32
33
# File 'lib/translatable.rb', line 31

def translation_class
  @@translation_class ||= nil
end

.translation_class_name=(klass) ⇒ Object



28
29
30
# File 'lib/translatable.rb', line 28

def translation_class_name=(klass)
  @@translation_class = klass.constantize
end

.with_locale(locale, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/translatable.rb', line 17

def with_locale(locale, &block)
  previous_locale = read_locale
  begin
    set_locale(locale)
    result = yield(locale)
  ensure
    set_locale(previous_locale)
  end
  result
end