gettext.rake 1.7 KB
Newer Older
1 2 3 4 5 6
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
R
Ruben Davila 已提交
7
    folders = %W(app lib config #{locale_path}).join(',')
8
    exts = %w(rb erb haml slim rhtml js jsx vue handlebars hbs mustache).join(',')
9 10 11 12 13

    Dir.glob(
      "{#{folders}}/**/*.{#{exts}}"
    )
  end
14 15 16 17 18 19 20 21

  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
B
Bob Van Landuyt 已提交
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

  desc 'Lint all po files in `locale/'
  task lint: :environment do
    FastGettext.silence_errors
    files = Dir.glob(Rails.root.join('locale/*/gitlab.po'))

    linters = files.map do |file|
      locale = File.basename(File.dirname(file))

      Gitlab::PoLinter.new(file, locale)
    end

    pot_file = Rails.root.join('locale/gitlab.pot')
    linters.unshift(Gitlab::PoLinter.new(pot_file))

    failed_linters = linters.select { |linter| linter.errors.any? }

    if failed_linters.empty?
      puts 'All PO files are valid.'
    else
      failed_linters.each do |linter|
        report_errors_for_file(linter.po_path, linter.errors)
      end

      raise "Not all PO-files are valid: #{failed_linters.map(&:po_path).to_sentence}"
    end
  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
62
end