1 require 'active_record'
2
3 module I18n
4 module Backend
5 # ActiveRecord model used to store actual translations to the database.
6 #
7 # This model expects a table like the following to be already set up in
8 # your the database:
9 #
10 # create_table :translations do |t|
11 # t.string :locale
12 # t.string :key
13 # t.text :value
14 # t.text :interpolations
15 # t.boolean :is_proc, :default => false
16 # end
17 #
18 # This model supports to named scopes :locale and :lookup. The :locale
19 # scope simply adds a condition for a given locale:
20 #
21 # I18n::Backend::ActiveRecord::Translation.locale(:en).all
22 # # => all translation records that belong to the :en locale
23 #
24 # The :lookup scope adds a condition for looking up all translations
25 # that either start with the given keys (joined by an optionally given
26 # separator or I18n.default_separator) or that exactly have this key.
27 #
28 # # with translations present for :"foo.bar" and :"foo.baz"
29 # I18n::Backend::ActiveRecord::Translation.lookup(:foo)
30 # # => an array with both translation records :"foo.bar" and :"foo.baz"
31 #
32 # I18n::Backend::ActiveRecord::Translation.lookup([:foo, :bar])
33 # I18n::Backend::ActiveRecord::Translation.lookup(:"foo.bar")
34 # # => an array with the translation record :"foo.bar"
35 #
36 # When the StoreProcs module was mixed into this model then Procs will
37 # be stored to the database as Ruby code and evaluated when :value is
38 # called.
39 #
40 # Translation = I18n::Backend::ActiveRecord::Translation
41 # Translation.create \
42 # :locale => 'en'
43 # :key => 'foo'
44 # :value => lambda { |key, options| 'FOO' }
45 # Translation.find_by_locale_and_key('en', 'foo').value
46 # # => 'FOO'
47 class ActiveRecord
48 class Translation < ::ActiveRecord::Base
49 set_table_name 'translations'
50 attr_protected :is_proc, :interpolations
51
52 serialize :value
53 serialize :interpolations, Array
54
55 scope_method = ::ActiveRecord::VERSION::MAJOR == 2 ? :named_scope : :scope
56
57 send scope_method, :locale, lambda { |locale|
58 { :conditions => { :locale => locale.to_s } }
59 }
60
61 send scope_method, :lookup, lambda { |keys, *separator|
62 column_name = connection.quote_column_name('key')
63 keys = Array(keys).map! { |key| key.to_s }
64
65 unless separator.empty?
66 warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
67 "You can change the internal separator by overwriting FLATTEN_SEPARATOR."
68 end
69
70 namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
71 { :conditions => ["#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace] }
72 }
73
74 def self.available_locales
75 Translation.find(:all, :select => 'DISTINCT locale').map { |t| t.locale.to_sym }
76 end
77
78 def interpolates?(key)
79 self.interpolations.include?(key) if self.interpolations
80 end
81
82 def value
83 if is_proc
84 Kernel.eval(read_attribute(:value))
85 else
86 value = read_attribute(:value)
87 value == 'f' ? false : value
88 end
89 end
90 end
91 end
92 end
93 end