Class: I18nize::Comparer

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

Overview

Small utility to compare translations from one locale to another

Class Method Summary collapse

Class Method Details

.missing_keys(to_locale:, from_locale: "en") ⇒ Object



41
42
43
# File 'lib/i18nize/comparer.rb', line 41

def self.missing_keys(to_locale:, from_locale: "en")
  missing_translations(from_locale: from_locale, to_locale: to_locale).keys_by_file
end

.missing_translations(to_locale:, from_locale: "en") ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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
# File 'lib/i18nize/comparer.rb', line 9

def self.missing_translations(to_locale:, from_locale: "en") # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  from_data = Loader.load_per_file(locale: from_locale)
  to_data   = Loader.load_per_file(locale: to_locale)

  result = {}

  from_data.each do |from_path, from_keys|
    dirname  = File.dirname(from_path)
    filename = File.basename(from_path)

    locale_re = /(^|[._])#{Regexp.escape(from_locale)}\.(ya?ml)$/
    expected_to_filename = filename.sub(locale_re) { "#{Regexp.last_match(1)}#{to_locale}.#{Regexp.last_match(2)}" }

    to_path = if dirname == "." || dirname.empty?
                expected_to_filename
              else
                File.join(dirname, expected_to_filename)
              end

    to_keys = to_data[to_path] || {}

    missing = from_keys.reject do |key, _|
      to_key = key.sub(/^#{Regexp.escape(from_locale)}\./, "#{to_locale}.")
      to_keys.key?(to_key)
    end

    result[from_path] = missing unless missing.empty?
  end

  MissingSet.new(from_locale: from_locale, to_locale: to_locale, data: result)
end