Class: I18n::Tasks::Reports::Terminal

Inherits:
Base
  • Object
show all
Includes:
HighlightOther, HighlightUnderline
Defined in:
lib/i18n/tasks/reports/terminal.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Modules: HighlightOther, HighlightUnderline

Constant Summary

Constants included from Logging

Logging::MUTEX, Logging::PROGRAM_NAME

Instance Attribute Summary

Attributes inherited from Base

#task

Instance Method Summary collapse

Methods included from HighlightUnderline

#highlight_string

Methods included from HighlightOther

#highlight_string

Methods inherited from Base

#initialize

Methods included from Logging

log_error, log_stderr, log_verbose, log_warn, program_name, warn_deprecated

Constructor Details

This class inherits a constructor from I18n::Tasks::Reports::Base

Instance Method Details

#check_normalized_results(non_normalized) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/i18n/tasks/reports/terminal.rb', line 119

def check_normalized_results(non_normalized)
  if non_normalized.empty?
    print_success "All data is normalized"
    return
  end
  log_stderr Rainbow("The following data requires normalization:").yellow
  puts non_normalized
  log_stderr Rainbow("Run `i18n-tasks normalize` to fix").yellow
end

#cp_results(results) ⇒ Object



113
114
115
116
117
# File 'lib/i18n/tasks/reports/terminal.rb', line 113

def cp_results(results)
  results.each do |(from, to)|
    print_info "#{Rainbow(from).cyan} #{Rainbow("+").yellow.bright} #{Rainbow(to).green}"
  end
end

#eq_base_keys(tree = task.eq_base_keys) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/i18n/tasks/reports/terminal.rb', line 79

def eq_base_keys(tree = task.eq_base_keys)
  keys = tree.root_key_value_data(true)
  if keys.present?
    print_title eq_base_title(keys)
    print_locale_key_value_data_table keys
  else
    print_info Rainbow("No translations are the same as base value").cyan
  end
end

#forest_stats(forest, stats = task.forest_stats(forest)) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/i18n/tasks/reports/terminal.rb', line 93

def forest_stats(forest, stats = task.forest_stats(forest))
  text = if stats[:locale_count] == 1
    I18n.t("i18n_tasks.data_stats.text_single_locale", **stats)
  else
    I18n.t("i18n_tasks.data_stats.text", **stats)
  end
  title = Rainbow(I18n.t("i18n_tasks.data_stats.title", **stats.slice(:locales))).bright
  print_info "#{Rainbow(title).cyan} #{Rainbow(text).cyan}"
end

#inconsistent_interpolations(forest = task.inconsistent_interpolations) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/i18n/tasks/reports/terminal.rb', line 27

def inconsistent_interpolations(forest = task.inconsistent_interpolations)
  if forest.present?
    print_title inconsistent_interpolations_title(forest)
    show_tree(forest)
  else
    print_success I18n.t("i18n_tasks.inconsistent_interpolations.none")
  end
end

#missing_keys(forest = task.missing_keys) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/i18n/tasks/reports/terminal.rb', line 9

def missing_keys(forest = task.missing_keys)
  forest = collapse_missing_tree! forest
  if forest.present?
    print_title missing_title(forest)
    print_table headings: [Rainbow(I18n.t("i18n_tasks.common.locale")).cyan.bright,
      Rainbow(I18n.t("i18n_tasks.common.key")).cyan.bright,
      I18n.t("i18n_tasks.missing.details_title")] do |t|
      t.rows = sort_by_attr!(forest_to_attr(forest)).map do |a|
        [{value: Rainbow(format_locale(a[:locale])).cyan, alignment: :center},
          format_key(a[:key], a[:data]),
          missing_key_info(a)]
      end
    end
  else
    print_success I18n.t("i18n_tasks.missing.none")
  end
end

#mv_results(results) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/i18n/tasks/reports/terminal.rb', line 103

def mv_results(results)
  results.each do |(from, to)|
    if to
      print_info "#{Rainbow(from).cyan} #{Rainbow("").yellow.bright} #{Rainbow(to).cyan}"
    else
      print_info "#{Rainbow(from).red}#{Rainbow(" 🗑").red.bright}"
    end
  end
end

#reserved_interpolations(forest = task.reserved_interpolations) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/i18n/tasks/reports/terminal.rb', line 36

def reserved_interpolations(forest = task.reserved_interpolations)
  if forest.present?
    print_title reserved_interpolations_title(forest)
    print_table headings: [Rainbow(I18n.t("i18n_tasks.common.locale")).cyan.bright,
      Rainbow(I18n.t("i18n_tasks.common.key")).cyan.bright,
      I18n.t("i18n_tasks.reserved_interpolations.details_title")] do |t|
      t.rows = sort_by_attr!(forest_to_attr(forest)).map do |a|
        [{value: Rainbow(format_locale(a[:locale])).cyan, alignment: :center},
          format_key(a[:key], a[:data]),
          a[:value].join(", ")]
      end
    end
  else
    print_success I18n.t("i18n_tasks.reserved_interpolations.none")
  end
end

#show_tree(tree) ⇒ Object



89
90
91
# File 'lib/i18n/tasks/reports/terminal.rb', line 89

def show_tree(tree)
  print_locale_key_value_data_table tree.root_key_value_data(true)
end

#unused_keys(tree = task.unused_keys) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/i18n/tasks/reports/terminal.rb', line 69

def unused_keys(tree = task.unused_keys)
  keys = tree.root_key_value_data(true)
  if keys.present?
    print_title unused_title(keys)
    print_locale_key_value_data_table keys
  else
    print_success I18n.t("i18n_tasks.unused.none")
  end
end

#used_keys(used_tree = task.used_tree) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/i18n/tasks/reports/terminal.rb', line 53

def used_keys(used_tree = task.used_tree)
  # For the used tree we may have usage nodes that are not leaves as references.
  keys_nodes = used_tree.nodes.select { |node| node.data[:occurrences].present? }.map do |node|
    [node.full_key(root: false), node]
  end
  print_title used_title(keys_nodes, used_tree.first.root.data[:key_filter])
  # Group multiple nodes
  if keys_nodes.present?
    keys_nodes.sort! { |a, b| a[0] <=> b[0] }.each do |key, node|
      print_occurrences node, key
    end
  else
    print_error I18n.t("i18n_tasks.usages.none")
  end
end