Class: BetterTranslate::Analyzer::CodeScanner
- Inherits:
-
Object
- Object
- BetterTranslate::Analyzer::CodeScanner
- Defined in:
- lib/better_translate/analyzer/code_scanner.rb
Overview
Scans code files to find i18n key references
Supports multiple patterns:
- t('key') / t("key")
- I18n.t(:key) / I18n.t('key')
- <%= t('key') %> in ERB
Constant Summary collapse
- I18N_PATTERNS =
I18n patterns to match
Matches:
- t('users.greeting')
- t("users.greeting")
- I18n.t(:users.greeting)
- I18n.t('users.greeting')
- I18n.translate('users.greeting')
[ /\bt\(['"]([a-z0-9_.]+)['"]/i, # t('key') or t("key") /\bI18n\.t\(:([a-z0-9_.]+)/i, # I18n.t(:key) /\bI18n\.t\(['"]([a-z0-9_.]+)['"]/i, # I18n.t('key') /\bI18n\.translate\(['"]([a-z0-9_.]+)['"]/i # I18n.translate('key') ].freeze
- SCANNABLE_EXTENSIONS =
File extensions to scan
%w[.rb .erb .html.erb .haml .slim].freeze
Instance Attribute Summary collapse
-
#files_scanned ⇒ Array<String>
readonly
List of scanned files.
-
#keys ⇒ Set
readonly
Found i18n keys.
-
#path ⇒ String
readonly
Path to scan (file or directory).
Instance Method Summary collapse
-
#collect_files ⇒ Array<String>
private
Collect all scannable files from path.
-
#initialize(path) ⇒ CodeScanner
constructor
Initialize scanner with path.
-
#key_count ⇒ Integer
Get count of unique keys found.
-
#scan ⇒ Set
Scan path and extract i18n keys.
-
#scan_file(file) ⇒ Object
private
Scan single file and extract keys.
-
#scannable_file?(file) ⇒ Boolean
private
Check if file should be scanned.
-
#validate_path! ⇒ Object
private
Validate that path exists.
Constructor Details
#initialize(path) ⇒ CodeScanner
Initialize scanner with path
49 50 51 52 53 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 49 def initialize(path) @path = path @keys = Set.new @files_scanned = [] end |
Instance Attribute Details
#files_scanned ⇒ Array<String> (readonly)
Returns List of scanned files.
43 44 45 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 43 def files_scanned @files_scanned end |
#keys ⇒ Set (readonly)
Returns Found i18n keys.
40 41 42 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 40 def keys @keys end |
#path ⇒ String (readonly)
Returns Path to scan (file or directory).
37 38 39 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 37 def path @path end |
Instance Method Details
#collect_files ⇒ Array<String> (private)
Collect all scannable files from path
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 104 def collect_files if File.file?(path) return [path] if scannable_file?(path) return [] end Dir.glob(File.join(path, "**", "*")).select do |file| File.file?(file) && scannable_file?(file) end end |
#key_count ⇒ Integer
Get count of unique keys found
81 82 83 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 81 def key_count @keys.size end |
#scan ⇒ Set
Scan path and extract i18n keys
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 65 def scan validate_path! files = collect_files files.each do |file| scan_file(file) @files_scanned << file end @keys end |
#scan_file(file) ⇒ Object (private)
Scan single file and extract keys
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 129 def scan_file(file) content = File.read(file) # Remove commented lines to avoid false positives lines = content.split("\n") active_lines = lines.reject { |line| line.strip.start_with?("#", "//", "<!--") } active_content = active_lines.join("\n") I18N_PATTERNS.each do |pattern| active_content.scan(pattern) do |match| key = match.is_a?(Array) ? match.first : match @keys.add(key) if key end end rescue StandardError => e # Skip files that can't be read warn "Warning: Could not scan #{file}: #{e.}" if ENV["VERBOSE"] end |
#scannable_file?(file) ⇒ Boolean (private)
Check if file should be scanned
121 122 123 |
# File 'lib/better_translate/analyzer/code_scanner.rb', line 121 def scannable_file?(file) SCANNABLE_EXTENSIONS.any? { |ext| file.end_with?(ext) } end |