summaryrefslogtreecommitdiff
path: root/lib/tasks/gettext.rake
blob: c42715ac9b7cd08b6e6f5faabb9d8e3643842e92 (plain)
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
require "gettext_i18n_rails/tasks"

namespace :gettext do
  # Customize list of translatable files
  # See: https://github.com/grosser/gettext_i18n_rails#customizing-list-of-translatable-files
  def files_to_translate
    folders = %W(app lib config #{locale_path}).join(',')
    exts = %w(rb erb haml slim rhtml js jsx vue handlebars hbs mustache).join(',')

    Dir.glob(
      "{#{folders}}/**/*.{#{exts}}"
    )
  end

  task :compile do
    # See: https://gitlab.com/gitlab-org/gitlab-ce/issues/33014#note_31218998
    FileUtils.touch(File.join(Rails.root, 'locale/gitlab.pot'))

    Rake::Task['gettext:pack'].invoke
    Rake::Task['gettext:po_to_json'].invoke
  end

  desc 'Lint all po files in `locale/'
  task lint: :environment do
    FastGettext.silence_errors
    failed_files = []
    files = Dir.glob("locale/*/gitlab.po")
    pot_file = 'locale/gitlab.pot'

    errors_for_pot = Gitlab::PoLinter.new(pot_file).errors
    if errors_for_pot.any?
      failed_files << pot_file
      report_errors_for_file(pot_file, errors_for_pot)
    end

    files.each do |file|
      locale = File.dirname(file).split('/').last
      errors_for_file = Gitlab::PoLinter.new(file, locale).errors
      if errors_for_file.any?
        failed_files << file
        report_errors_for_file(file, errors_for_file)
      end
    end

    raise "Not all PO-files are valid: #{failed_files.to_sentence}" if failed_files.any?

    puts 'All PO files are valid.'
  end

  def report_errors_for_file(file, errors_for_file)
    puts "Errors in `#{file}`:"

    errors_for_file.each do |message_id, errors|
      puts "  #{message_id}"
      errors.each do |error|
        spaces = ' ' * 4
        error = error.lines.join("#{spaces}")
        puts "#{spaces}#{error}"
      end
    end
  end
end