Class: I18nVerify::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n-verify/support.rb

Instance Method Summary collapse

Constructor Details

#initialize(filenames) ⇒ Checker

Returns a new instance of Checker.



53
54
55
# File 'lib/i18n-verify/support.rb', line 53

def initialize(filenames)
  @translations = Translations.new(filenames)
end

Instance Method Details

#duplicates(locales_requested = []) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/i18n-verify/support.rb', line 103

def duplicates(locales_requested = [])
  locales = @translations.collect{|tr| tr[:locale]}.uniq
  locales_to_check = locales_requested.empty? ? locales : (locales & locales_requested)

  puts "Checking locales #{locales_to_check.inspect} out of #{locales.inspect} for redundancy"

  # collect and print duplicate translations
  locales_to_check.each do |locale|
    puts "#{locale}:"
    translations_by_key = @translations.select {|t| t[:locale] == locale}.uniq.group_by {|t| t[:key]}
    translations_by_key.reject {|t| t[1].count == 1}.each_pair do |key, translations|
      puts " #{key}: #{translations.collect{|t| t[:filename]}.join(", ")}"
    end
  end
end

#find_key(regexp = Regexp.new(''), group_by_filename = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/i18n-verify/support.rb', line 57

def find_key(regexp=Regexp.new(''), group_by_filename=false)
  # select translations with matching keys
  matching_translations = @translations.select {|t| [t[:locale],t[:key]].join('.') =~ regexp}

  # print matching translations
  if group_by_filename
    matching_translations.group_by {|t| t[:filename]}.each_pair do |filename, translations|
      puts "#{filename}:"
      translations.each do |t|
        puts " #{[t[:locale],t[:key]].join('.')}: #{t[:translation]}\n"
      end
    end
  else
    matching_translations.each do |t|
      puts "#{t[:filename]} # #{[t[:locale],t[:key]].join('.')}: #{t[:translation]}\n"
    end
  end
end

#is_complete?(locales_requested = []) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/i18n-verify/support.rb', line 76

def is_complete?(locales_requested = [])      
  # check each pair of locales
  #   if the first one has keys the second one doesn't => these are the incomplete translations
  locales = @translations.collect{|tr| tr[:locale]}.uniq
  locales_to_check = locales_requested.empty? ? locales : (locales & locales_requested)

  if locales_to_check.size <= 1
    puts "Need at least two locales; found #{locales_to_check.size}: #{locales_to_check.join(',')}"
  else
    puts "Checking locales #{locales_to_check.inspect} out of #{locales.inspect} for completeness"
    locales_to_check.permutation.each do |first, second|
      first_translations = @translations.select {|translation| translation[:locale] == first}
      second_translations = @translations.select {|translation| translation[:locale] == second}

      differences = first_translations.select {|f| f if second_translations.none? {|s| f[:key]==s[:key]} }.compact
      if differences.empty?
        puts "#{first} => #{second}: complete\n"
      else
        puts "Missing from #{second} vs. #{first}:\n"
        differences.each do |difference|
          puts " " + [difference[:locale], difference[:key]].join('.') + " defined for #{first} in #{difference[:filename]}\n"
        end
      end
    end
  end
end

#spell(locales_requested = []) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/i18n-verify/support.rb', line 119

def spell(locales_requested = [])
  locales = @translations.collect{|tr| tr[:locale]}.uniq
  locales_to_check = locales_requested.empty? ? locales : (locales & locales_requested)

  puts "Checking spelling in locales #{locales_to_check.inspect} out of #{locales.inspect}"
  
  # take each translation in turn and check spelling
  @translations.each do |translation|
    next unless locales_to_check.include? translation[:locale]    # skip if in a locale not to be checked
    
    text = translation[:translation]
    next unless text.is_a? String     # skip if not a string (perhaps an array?)
    text.gsub!(/%\{[^}]*\}/, "")      # remove translation params
    
    # make aspell check the spelling
    result = %x[echo "#{text}" |  aspell -a --ignore-case --lang="#{translation[:locale]}" --dont-suggest]

    # process result
    # aspell returns lines in the format of '# <word> <position>' for each misspelled word
    result.lines do |line|
      parts = line.split(" ")
      if parts[0]=='#'      # misspelling found
        # print: misspelled word | file | (full) key | position
        puts "#{parts[1]} | #{translation[:filename]} | #{[translation[:locale],translation[:key]].join('.')} | #{parts[2]}"
      end
    end
  end
end