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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/generators/beautiful_locale_generator.rb', line 53
def regenerate_app_locale
require 'net/http'
app_path = (Rails.root || engine_opt)
app_name = Rails.application.class.name.split('::').first.downcase
prefix = engine_opt.blank? ? '' : "#{engine_opt.camelize}::"
already_processed = {}
hi18n = {}
list_locales.each do |locale_str|
locale = locale_str.downcase
already_processed[locale] ||= {}
filepath = File.join(app_path, 'config', 'locales', "#{app_name}.#{locale}.yml")
begin
if File.exist?(filepath)
hi18n = YAML.load_file(filepath)
end
rescue
puts "Error loading locale file (YAML invalid?) : #{filepath}"
end
hi18n[locale] ||= { 'app' => {} }
hi18n[locale]['app'] ||= { 'models' => {} }
hi18n[locale]['app']['models'] ||= {}
hi18n[locale]['app']['models'].each do |modelname, hshtranslations|
hshtranslations['bs_attributes'].each do |attr, translated_attr|
already_processed[locale][attr] = translated_attr
end
end
Dir.glob("app/models/**/*").each do |model_file|
puts model_file
next if File.directory?(model_file) or
File.basename(model_file).first == '.' or
model_file.include?('/concerns/') or
model_file.include?('pdf_report.rb') or
model_file.include?('application_record.rb')
model = File.basename(model_file, File.extname(model_file))
klass = "#{prefix}#{model}".camelize.constantize
sorted_attr = klass.attribute_names.sort
newmodel = !hi18n[locale]['app']['models'].has_key?(model)
hi18n[locale]['app']['models'][model] ||= {
'bs_caption' => model,
'bs_caption_plural' => model.pluralize,
'bs_attributes' => {},
}
if newmodel then
bs_caption = ""
begin
bs_caption = translate_string(locale, model)
rescue Exception => e
puts "Erreur traduction #{e.backtrace}"
bs_caption = model
end
bs_caption_plural = ""
begin
bs_caption_plural = translate_string(locale, model.pluralize)
rescue Exception => e
puts "Erreur traduction #{e.backtrace}"
bs_caption_plural = model.pluralize
end
hi18n[locale]['app']['models'][model]['bs_caption'] = bs_caption
hi18n[locale]['app']['models'][model]['bs_caption_plural'] = bs_caption_plural
end
hi18n[locale]['app']['models'][model]['bs_attributes'] ||= {}
sorted_attr.each do |k|
if hi18n[locale]['app']['models'][model]['bs_attributes'][k].blank?
if already_processed[locale][k].blank?
begin
attr_translate = translate_string(locale, k)
already_processed[locale][k] = attr_translate
rescue
puts "Plantage translate API"
attr_translate = k
end
else
attr_translate = already_processed[locale][k]
end
else
attr_translate = hi18n[locale]['app']['models'][model]['bs_attributes'][k]
end
hi18n[locale]['app']['models'][model]['bs_attributes'][k] = attr_translate
end
end
File.unlink(filepath) if File.exist?(filepath)
file = File.open(filepath, "w")
file.write(hi18n[locale].to_yaml)
file.close
end
end
|