File: tasks.rb

package info (click to toggle)
ruby-i18n-spec 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 224 kB
  • sloc: ruby: 478; makefile: 3
file content (76 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download
1
2
3
4
5
6
7
8
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'i18n-spec'

I18nSpec::LOG_DETAIL_PREDICATE = "  - "

namespace :'i18n-spec' do
  desc "Checks the validity of a locale file"
  task :validate do
    if ARGV[1].nil?
      puts "You must specifiy a file path or a folder path"
      return
    elsif File.directory?(ARGV[1])
      filepaths = Dir.glob("#{ARGV[1]}/*.yml")
    else
      filepaths = [ARGV[1]]
    end

    filepaths.each do |filepath|
      heading filepath
      fatals, errors, warnings = [0, 0, 0]
      locale_file = I18nSpec::LocaleFile.new(filepath)
      unless locale_file.is_parseable?
        log :fatal, 'could not be parsed', format_str(locale_file.errors[:unparseable])
        fatals += 1
        break
      end

      unless locale_file.invalid_pluralization_keys.empty?
        log :error, 'invalid pluralization keys', format_array(locale_file.errors[:invalid_pluralization_keys])
        errors += 1
      end

      log :ok if fatals + errors + warnings == 0
    end
  end

  desc "Checks for missing translations between the default and the translated locale file"
  task :completeness do
    if ARGV[1].nil? || ARGV[2].nil?
      puts "You must specify a default locale file and translated file or a folder of translated files"
    elsif File.directory?(ARGV[2])
      translated_filepaths = Dir.glob("#{ARGV[2]}/*.yml")
    else
      translated_filepaths = [ARGV[2]]
    end
    default_locale = I18nSpec::LocaleFile.new(ARGV[1])
    translated_filepaths.each do |translated_filepath|
      heading translated_filepath
      locale_file = I18nSpec::LocaleFile.new(translated_filepath)
      misses = default_locale.flattened_translations.keys.reject do |key|
        locale_file.flattened_translations.keys.include?(key)
      end
      if misses.empty?
        log :complete
      else
        misses.each { |miss| log :missing, miss }
      end
    end
  end

  def log(level, msg='', detail=nil)
    puts "- *" << level.to_s.upcase << '* ' << msg 
    puts detail if detail
  end

  def heading(str='')
    puts "\n### " << str << "\n\n"
  end

  def format_array(array)
    [I18nSpec::LOG_DETAIL_PREDICATE, array.join(I18nSpec::LOG_DETAIL_PREDICATE)].join
  end

  def format_str(str)
    [I18nSpec::LOG_DETAIL_PREDICATE, str].join
  end
end