BigW Consortium Gitlab

gettext.rake 1.76 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
Ruben Davila committed
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
22 23 24

  desc 'Lint all po files in `locale/'
  task lint: :environment do
25 26
    require 'simple_po_parser'

27 28 29 30 31 32
    FastGettext.silence_errors
    files = Dir.glob(Rails.root.join('locale/*/gitlab.po'))

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

33
      Gitlab::I18n::PoLinter.new(file, locale)
34 35 36
    end

    pot_file = Rails.root.join('locale/gitlab.pot')
37
    linters.unshift(Gitlab::I18n::PoLinter.new(pot_file))
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

    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
64
end