debian-mirror-gitlab/lib/tasks/gettext.rake

134 lines
3.6 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
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
2018-11-08 19:23:39 +05:30
folders = %W(ee app lib config #{locale_path}).join(',')
2017-09-10 17:25:29 +05:30
exts = %w(rb erb haml slim rhtml js jsx vue handlebars hbs mustache).join(',')
2017-08-17 22:00:37 +05:30
Dir.glob(
"{#{folders}}/**/*.{#{exts}}"
)
end
2017-09-10 17:25:29 +05:30
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:po_to_json'].invoke
end
2018-03-17 18:26:18 +05:30
2019-09-04 21:01:54 +05:30
desc 'Regenerate gitlab.pot file'
2018-11-18 11:00:15 +05:30
task :regenerate do
pot_file = 'locale/gitlab.pot'
# Remove all translated files, this speeds up finding
FileUtils.rm Dir['locale/**/gitlab.*']
# remove the `pot` file to ensure it's completely regenerated
FileUtils.rm_f pot_file
Rake::Task['gettext:find'].invoke
# leave only the required changes.
`git checkout -- locale/*/gitlab.po`
# Remove timestamps from the pot file
pot_content = File.read pot_file
pot_content.gsub!(/^"POT?\-(?:Creation|Revision)\-Date\:.*\n/, '')
File.write pot_file, pot_content
puts <<~MSG
All done. Please commit the changes to `locale/gitlab.pot`.
MSG
end
2018-03-17 18:26:18 +05:30
desc 'Lint all po files in `locale/'
task lint: :environment do
require 'simple_po_parser'
require 'gitlab/utils'
FastGettext.silence_errors
files = Dir.glob(Rails.root.join('locale/*/gitlab.po'))
linters = files.map do |file|
locale = File.basename(File.dirname(file))
Gitlab::I18n::PoLinter.new(file, locale)
end
pot_file = Rails.root.join('locale/gitlab.pot')
linters.unshift(Gitlab::I18n::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
2018-11-08 19:23:39 +05:30
task :updated_check do
2018-11-18 11:00:15 +05:30
pot_file = 'locale/gitlab.pot'
2018-11-08 19:23:39 +05:30
# Removing all pre-translated files speeds up `gettext:find` as the
# files don't need to be merged.
# Having `LC_MESSAGES/gitlab.mo files present also confuses the output.
FileUtils.rm Dir['locale/**/gitlab.*']
2018-11-18 11:00:15 +05:30
FileUtils.rm_f pot_file
2018-11-08 19:23:39 +05:30
# `gettext:find` writes touches to temp files to `stderr` which would cause
# `static-analysis` to report failures. We can ignore these.
2019-02-15 15:39:39 +05:30
silence_stderr do
2018-11-08 19:23:39 +05:30
Rake::Task['gettext:find'].invoke
end
2018-11-18 11:00:15 +05:30
pot_diff = `git diff -- #{pot_file} | grep -E '^(\\+|-)msgid'`.strip
2018-11-08 19:23:39 +05:30
# reset the locale folder for potential next tasks
`git checkout -- locale`
if pot_diff.present?
raise <<~MSG
2018-11-18 11:00:15 +05:30
Newly translated strings found, please add them to `#{pot_file}` by running:
2018-11-08 19:23:39 +05:30
2018-11-18 11:00:15 +05:30
bin/rake gettext:regenerate
2018-11-08 19:23:39 +05:30
2018-11-18 11:00:15 +05:30
Then commit and push the resulting changes to `#{pot_file}`.
2018-11-08 19:23:39 +05:30
The diff was:
#{pot_diff}
MSG
end
end
2018-03-17 18:26:18 +05:30
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
2018-11-20 20:47:30 +05:30
2019-02-15 15:39:39 +05:30
def silence_stderr(&block)
2018-11-20 20:47:30 +05:30
old_stderr = $stderr.dup
$stderr.reopen(File::NULL)
$stderr.sync = true
yield
ensure
$stderr.reopen(old_stderr)
old_stderr.close
end
2017-08-17 22:00:37 +05:30
end