11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/l10n/column_translation.rb', line 11
def translates(*attrs)
self.translated_attributes = (translated_attributes + attrs).map(&:to_sym).uniq
definition_context = Proc.new do |attr_name|
define_method "#{attr_name}_t" do
read_attribute("#{attr_name}#{I18n.translation_suffix}")
end
define_method "#{attr_name}_t_with_fallback" do
default_value = read_attribute(attr_name)
if I18n.default?
localized_value = default_value
else
localized_value = read_attribute("#{attr_name}_#{I18n.language_code}")
localized_value = default_value if localized_value.blank?
end
localized_value
end
define_method "#{attr_name}_t=" do |value|
write_attribute "#{attr_name}#{I18n.translation_suffix}", value
end
define_method "#{attr_name}_#{I18n.default_language_code}" do
read_attribute attr_name
end
define_method "#{attr_name}_#{I18n.default_language_code}=" do |attr_value|
write_attribute attr_name, attr_value
end
define_method "#{attr_name}_translations" do
translations = { I18n.default_language_code => read_attribute(attr_name) }
I18n.translation_language_codes.each do |language_code|
translations[language_code] = read_attribute("#{attr_name}_#{language_code}")
end
translations
end
define_method "#{attr_name}_translations=" do |translations|
translations = translations.dup
if all = translations.delete(:all)
send("#{attr_name}=", all)
I18n.translation_language_codes.each do |language_code|
method_name = "#{attr_name}_#{language_code}="
send(method_name, all) if respond_to?(method_name)
end
end
translations.each do |language_code, value|
method_name = "#{attr_name}_#{language_code}="
send(method_name, value) if respond_to?(method_name)
end
end
end
self.translated_attributes.each { |attr_name| definition_context.call attr_name }
end
|