Module: I18n::Tasks::MissingKeys

Included in:
BaseTask
Defined in:
lib/i18n/tasks/missing_keys.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

MISSING_TYPES =
%w[
  used
  diff
  plural
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.missing_keys_typesObject



12
13
14
# File 'lib/i18n/tasks/missing_keys.rb', line 12

def self.missing_keys_types
  @missing_keys_types ||= MISSING_TYPES
end

Instance Method Details

#collapse_same_key_in_locales!(forest) {|::I18n::Tasks::Data::Tree::Node| ... } ⇒ Object

Parameters:

Yields:

Yield Returns:

  • (Boolean)

    whether to collapse the node



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
# File 'lib/i18n/tasks/missing_keys.rb', line 131

def collapse_same_key_in_locales!(forest)
  locales_and_node_by_key = {}
  to_remove               = []
  forest.each do |root|
    locale = root.key
    root.keys do |key, node|
      next unless yield node

      if locales_and_node_by_key.key?(key)
        locales_and_node_by_key[key][0] << locale
      else
        locales_and_node_by_key[key] = [[locale], node]
      end
      to_remove << node
    end
  end
  forest.remove_nodes_and_emptied_ancestors! to_remove
  locales_and_node_by_key.each_with_object({}) do |(key, (locales, node)), inv|
    (inv[locales.sort.join('+')] ||= []) << [key, node]
  end.map do |locales, keys_nodes|
    keys_nodes.each do |(key, node)|
      forest["#{locales}.#{key}"] = node
    end
  end
  forest
end

#eq_base_keys(opts = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/i18n/tasks/missing_keys.rb', line 31

def eq_base_keys(opts = {})
  locales = Array(opts[:locales]).presence || self.locales
  (locales - [base_locale]).inject(empty_forest) do |tree, locale|
    tree.merge! equal_values_tree(locale, base_locale)
  end
end

#equal_values_tree(locale, compare_to = base_locale) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/i18n/tasks/missing_keys.rb', line 116

def equal_values_tree(locale, compare_to = base_locale)
  base = data[compare_to].first.children
  data[locale].select_keys(root: false) do |key, node|
    other_node = base[key]
    other_node && !node.reference? && node.value == other_node.value && !ignore_key?(key, :eq_base, locale)
  end.set_root_key!(locale, type: :eq_base)
end

#load_rails_i18n_pluralization!(locale) ⇒ Object

Loads rails-i18n pluralization config for the given locale.



90
91
92
93
# File 'lib/i18n/tasks/missing_keys.rb', line 90

def load_rails_i18n_pluralization!(locale)
  path = File.join(Gem::Specification.find_by_name('rails-i18n').gem_dir, 'rails', 'pluralization', "#{locale}.rb")
  eval(File.read(path), binding, path) # rubocop:disable Security/Eval
end

#locale_key_missing?(locale, key) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/i18n/tasks/missing_keys.rb', line 124

def locale_key_missing?(locale, key)
  !key_value?(key, locale) && !external_key?(key, locale) && !ignore_key?(key, :missing)
end

#missing_diff_forest(locales, base = base_locale) ⇒ Object



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

def missing_diff_forest(locales, base = base_locale)
  tree = empty_forest
  # present in base but not locale
  (locales - [base]).each do |locale|
    tree.merge! missing_diff_tree(locale, base)
  end
  if locales.include?(base)
    # present in locale but not base
    (self.locales - [base]).each do |locale|
      tree.merge! missing_diff_tree(base, locale)
    end
  end
  tree
end

#missing_diff_tree(locale, compared_to = base_locale) ⇒ Object

keys present in compared_to, but not in locale



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/i18n/tasks/missing_keys.rb', line 96

def missing_diff_tree(locale, compared_to = base_locale)
  data[compared_to].select_keys do |key, _node|
    locale_key_missing? locale, depluralize_key(key, compared_to)
  end.set_root_key!(locale, type: :missing_diff).keys do |_key, node|
    # change path and locale to base
    data = { locale: locale, missing_diff_locale: node.data[:locale] }
    if node.data.key?(:path)
      data[:path] = LocalePathname.replace_locale(node.data[:path], node.data[:locale], locale)
    end
    node.data.update data
  end
end

#missing_keys(locales: nil, types: nil, base_locale: nil) ⇒ Siblings

Parameters:

  • types (:used, :diff, :plural) (defaults to: nil)

    all if ‘nil`.

Returns:

  • (Siblings)


22
23
24
25
26
27
28
29
# File 'lib/i18n/tasks/missing_keys.rb', line 22

def missing_keys(locales: nil, types: nil, base_locale: nil)
  locales ||= self.locales
  types   ||= missing_keys_types
  base = base_locale || self.base_locale
  types.inject(empty_forest) do |f, type|
    f.merge! send(:"missing_#{type}_forest", locales, base)
  end
end

#missing_keys_typesObject



16
17
18
# File 'lib/i18n/tasks/missing_keys.rb', line 16

def missing_keys_types
  MissingKeys.missing_keys_types
end

#missing_plural_forest(locales, _base = base_locale) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/i18n/tasks/missing_keys.rb', line 59

def missing_plural_forest(locales, _base = base_locale)
  locales.each_with_object(empty_forest) do |locale, forest|
    required_keys = required_plural_keys_for_locale(locale)
    next if required_keys.empty?

    tree = empty_forest
    plural_nodes data[locale] do |node|
      children = node.children
      present_keys = Set.new(children.map { |c| c.key.to_sym })
      next if ignore_key?(node.full_key(root: false), :missing)
      next if present_keys.superset?(required_keys)

      tree[node.full_key] = node.derive(
        value: children.to_hash,
        children: nil,
        data: node.data.merge(missing_keys: (required_keys - present_keys).to_a)
      )
    end
    tree.set_root_key!(locale, type: :missing_plural)
    forest.merge!(tree)
  end
end

#missing_used_forest(locales, _base = base_locale) ⇒ Object



53
54
55
56
57
# File 'lib/i18n/tasks/missing_keys.rb', line 53

def missing_used_forest(locales, _base = base_locale)
  locales.inject(empty_forest) do |forest, locale|
    forest.merge! missing_used_tree(locale)
  end
end

#missing_used_tree(locale) ⇒ Object

keys used in the code missing translations in locale



110
111
112
113
114
# File 'lib/i18n/tasks/missing_keys.rb', line 110

def missing_used_tree(locale)
  used_tree(strict: true).select_keys do |key, _node|
    locale_key_missing?(locale, key)
  end.set_root_key!(locale, type: :missing_used)
end

#required_plural_keys_for_locale(locale) ⇒ Object



82
83
84
85
86
87
# File 'lib/i18n/tasks/missing_keys.rb', line 82

def required_plural_keys_for_locale(locale)
  @plural_keys_for_locale ||= {}
  return @plural_keys_for_locale[locale] if @plural_keys_for_locale.key?(locale)

  @plural_keys_for_locale[locale] = plural_keys_for_locale(locale)
end