Class: BetterTranslate::Generators::AnalyzeGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/better_translate/analyze/analyze_generator.rb

Overview

Rails generator for analyzing YAML locale files

Provides statistics about translation files: string count, structure, etc.

Examples:

Analyze a file

rails generate better_translate:analyze config/locales/en.yml

Instance Method Summary collapse

Instance Method Details

#analyze_filevoid

This method returns an undefined value.

Analyze YAML file



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/better_translate/analyze/analyze_generator.rb', line 30

def analyze_file
  full_path = Rails.root.join(file_path)

  unless File.exist?(full_path)
    say "File not found: #{full_path}", :red
    return
  end

  say "Analyzing: #{file_path}", :green
  say "=" * 60

  begin
    data = YAML.load_file(full_path)

    # Flatten to count strings
    flattened = BetterTranslate::Utils::HashFlattener.flatten(data)

    say ""
    say "Total strings: #{flattened.size}", :cyan
    say ""

    # Show structure
    say "Structure:", :yellow
    show_structure(data, 0)

    say ""
    say "=" * 60
    say "Analysis complete", :green
  rescue StandardError => e
    say "Error analyzing file: #{e.message}", :red
  end
end

#count_strings(hash) ⇒ Integer (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Count strings in nested hash

Parameters:

  • hash (Hash)

    Hash to count

Returns:

  • (Integer)

    String count



90
91
92
# File 'lib/generators/better_translate/analyze/analyze_generator.rb', line 90

def count_strings(hash)
  BetterTranslate::Utils::HashFlattener.flatten(hash).size
end

#show_structure(hash, level) ⇒ void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Show nested structure

Parameters:

  • hash (Hash)

    Hash to show

  • level (Integer)

    Nesting level



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/generators/better_translate/analyze/analyze_generator.rb', line 72

def show_structure(hash, level)
  hash.each do |key, value|
    indent = "  " * level
    if value.is_a?(Hash)
      say "#{indent}#{key}/ (#{count_strings(value)} strings)", :white
      show_structure(value, level + 1)
    else
      say "#{indent}#{key}: #{value.to_s[0..50]}#{value.to_s.length > 50 ? "..." : ""}", :white
    end
  end
end