Class: Lokale::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/lokale.rb

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ Reporter

Returns a new instance of Reporter.



193
194
195
# File 'lib/lokale.rb', line 193

def initialize(agent)
  @agent = agent
end

Instance Method Details



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/lokale.rb', line 261

def print_diferences_report
  diferences_repot = ""

  @agent.lfiles.group_by { |f| f.full_name }.each do |file_name, files|
    base_lang = files.any? { |f| f.lang == "Base" } ? "Base" : "en"
    files = files.select { |f| f.lang != base_lang }
    all_keys = files.map(&:keys).compact.map(&:to_set)
    next if all_keys.empty?
    united_keys = all_keys.reduce(:|)
    all_keys.map! { |ks| united_keys - ks }
    next if all_keys.map(&:length).reduce(:+).zero?

    diferences_repot << "Missing keys in file \"#{file_name}\":\n"
    all_keys.zip(files) do |missing_keys, lfile|
      next if missing_keys.size.zero?
      diferences_repot << "*".red + " #{lfile.lang} - #{missing_keys.size} key(s):\n"
      missing_keys.each { |k| diferences_repot << "#{k}\n" }
    end
    diferences_repot << "\n"
  end

  if diferences_repot.empty? 
    puts "Localization files are full.".green
    puts
  else
    puts "Localization files are not full.".red
    puts diferences_repot
    puts
  end
end


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/lokale.rb', line 213

def print_files_table
  languages = @agent.lfiles.map { |e| e.lang }.to_set.to_a
  files = @agent.lfiles.map { |e| e.full_name }.to_set.to_a

  puts "Found #{@agent.lfiles.size} localized files for #{languages.size} languages."

  description_header = "[*]".rpadded(36)
  languages.each { |l| description_header << l.rpadded(8) }
  puts description_header

  files.each do |f|
    description_string = f.rpadded(36)
    languages.each do |l|
      lfile = @agent.lfiles.select { |lf| lf.full_name == f && lf.lang == l }
      description_string << (lfile.empty? ? "-" : lfile[0].parsed.nil? ? "*" : "#{lfile[0].parsed.size}").rpadded(8)
    end
    puts description_string
  end
  puts
end


205
206
207
208
209
210
211
# File 'lib/lokale.rb', line 205

def print_macro_calls_summary
  total_macro_calls = @agent.macros.map(&:total_count).reduce(:+)
  uniq_macro_calls = @agent.macros.map(&:uniq_count).reduce(:+)
  puts "Found #{total_macro_calls} localization macro calls in #{@agent.sfiles_proceeded} files."
  puts "Uniq macro calls: #{uniq_macro_calls}"
  puts
end


234
235
236
237
238
239
# File 'lib/lokale.rb', line 234

def print_macro_table
  @agent.macros.each do |macro|
    puts "#{macro.name}:".rpadded(24) + "total: #{macro.total_count}".rpadded(16) + "uniq: #{macro.uniq_count}"
  end
  puts
end


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/lokale.rb', line 241

def print_repeats_report
  repeats_repot = ""
  @agent.lfiles.each do |lf| 
    repeats = lf.repeats
    next if repeats.count.zero?
    repeats_repot << "#{lf.lang}/#{lf.full_name} repeats:\n"
    repeats_repot << repeats.join("\n")
    repeats_repot << "\n"
  end

  if repeats_repot.empty? 
    puts "Repeats not found.".green
    puts
  else
    puts "Found repeats in strings files.".red
    puts repeats_repot
    puts
  end
end


197
198
199
200
201
202
203
# File 'lib/lokale.rb', line 197

def print_summary
  print_macro_calls_summary
  print_macro_table
  print_files_table
  print_repeats_report
  print_diferences_report
end