9
10
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/attribute_localizer.rb', line 9
def localize_attributes(*attributes)
options = attributes.
klass = class << self; self end
class_eval 'after_save :serialize_localizations'
class_eval 'attr_accessor :locale' if options[:locale_virtual]
attributes.each { |attribute| class_eval "attr_accessor :#{attribute}" }
src = attributes.map do |attribute|
str = "def #{attribute}\n"
str << " return @#{attribute} if @#{attribute}\n"
str << " @#{attribute} = I18n.t(\"\#{self.class.send :localization_namespace}.\#{localization_hash_key}.#{attribute}\", :locale => locale, :default => '')\n"
str << " @#{attribute} = nil unless @#{attribute}.present?\n"
str << " @#{attribute}\n"
str << "end\n"
str
end.join("\n")
module_eval src, __FILE__, __LINE__
src = "\n def locale\n \#{options[:locale_virtual] ? '@locale' : 'super'} || I18n.locale\n end\n\n end_src\n\n module_eval src, __FILE__, __LINE__\n\n\n private\n\n klass.send :define_method, 'i18n_file' do |locale|\n \"\#{RAILS_ROOT}/config/locales/\#{localization_namespace}/\#{locale}.yml\"\n end\n\n klass.send :private, 'i18n_file'\n\n src = <<-end_src\n\n def load_locale_data(locale)\n localized_hash = YAML::load_file(i18n_file(locale))\n localized_hash[locale][localization_namespace]\n rescue\n {}\n end\n\n def instantiate(record)\n object = super\n object.instance_variable_get('@attributes').merge!(\#{attributes.map { |attribute| \"'\#{attribute}' => nil\" }.join(', ') })\n object\n end\n\n end_src\n\n klass.class_eval src, __FILE__, __LINE__\n\n klass.send :private, 'load_locale_data'\n klass.send :private, 'instantiate'\n\n klass.send :define_method, 'localization_namespace' do\n to_s.underscore.pluralize\n end\n\n klass.send :private, 'localization_namespace'\n\n klass.send :define_method, 'write_locale_data' do |locale, localized_hash|\n File.open( i18n_file(locale), File::WRONLY|File::TRUNC|File::CREAT ) do |out|\n YAML.dump( { locale => {localization_namespace => localized_hash} }, out )\n end\n end\n\n klass.send :private, 'write_locale_data'\n\n src = <<-end_src\n\n private\n\n def attributes_from_column_definition\n super.merge(\#{attributes.map { |attribute| \"'\#{attribute}' => nil\" }.join(', ') })\n end\n\n def localization_hash_key\n \"_\\\#{id}\".to_sym\n end\n\n def localized_yaml\n {\n \#{attributes.map { |attribute| \":\#{attribute} => \#{attribute}\"}.join(',')}\n }\n end\n\n def serialize_localizations\n localized_hash = self.class.send :load_locale_data, locale\n localized_hash[localization_hash_key] = localized_yaml\n self.class.send :write_locale_data, locale, localized_hash\n end\n\n end_src\n\n module_eval src, __FILE__, __LINE__\n\nend\n"
|