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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

138 lines
3.5 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require "gettext_i18n_rails/tasks"
namespace :gettext do
2017-09-10 17:25:29 +05:30
task :compile do
2019-12-04 20:38:33 +05:30
# See: https://gitlab.com/gitlab-org/gitlab-foss/issues/33014#note_31218998
2021-02-22 17:27:13 +05:30
FileUtils.touch(pot_file_path)
2017-09-10 17:25:29 +05:30
2023-06-20 00:43:36 +05:30
command = [
"node", "./scripts/frontend/po_to_json.js",
"--locale-root", Rails.root.join('locale').to_s,
"--output-dir", Rails.root.join('app/assets/javascripts/locale').to_s
]
abort 'Error: Unable to convert gettext files to js.'.color(:red) unless Kernel.system(*command)
2017-09-10 17:25:29 +05:30
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
2023-06-20 00:43:36 +05:30
require_relative "../../tooling/lib/tooling/gettext_extractor"
2021-02-22 17:27:13 +05:30
ensure_locale_folder_presence!
2018-11-18 11:00:15 +05:30
# remove the `pot` file to ensure it's completely regenerated
2021-02-22 17:27:13 +05:30
FileUtils.rm_f(pot_file_path)
2018-11-18 11:00:15 +05:30
2023-06-20 00:43:36 +05:30
extractor = Tooling::GettextExtractor.new(
glob_base: Rails.root
)
File.write(pot_file_path, extractor.generate_pot)
2018-11-18 11:00:15 +05:30
2021-02-22 17:27:13 +05:30
raise 'gitlab.pot file not generated' unless File.exist?(pot_file_path)
2018-11-18 11:00:15 +05:30
puts <<~MSG
2023-06-20 00:43:36 +05:30
All done. Please commit the changes to `locale/gitlab.pot`.
2018-11-18 11:00:15 +05:30
2023-06-20 00:43:36 +05:30
Tip: For even faster regeneration, directly run the following command:
tooling/bin/gettext_extractor locale/gitlab.pot
2018-11-18 11:00:15 +05:30
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'
2022-01-26 12:08:38 +05:30
require 'parallel'
2018-03-17 18:26:18 +05:30
FastGettext.silence_errors
files = Dir.glob(Rails.root.join('locale/*/gitlab.po'))
linters = files.map do |file|
locale = File.basename(File.dirname(file))
2021-02-22 17:27:13 +05:30
Gitlab::I18n::PoLinter.new(po_path: file, locale: locale)
2018-03-17 18:26:18 +05:30
end
2021-02-22 17:27:13 +05:30
linters.unshift(Gitlab::I18n::PoLinter.new(po_path: pot_file_path))
2018-03-17 18:26:18 +05:30
2022-01-26 12:08:38 +05:30
failed_linters = Parallel
.map(linters, progress: 'Linting po files') { |linter| linter if linter.errors.any? }
.compact
2018-03-17 18:26:18 +05:30
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
2023-06-20 00:43:36 +05:30
task updated_check: [:regenerate] do
2021-02-22 17:27:13 +05:30
pot_diff = `git diff -- #{pot_file_path} | 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
2021-02-22 17:27:13 +05:30
Changes in translated strings found, please update file `#{pot_file_path}` by running:
2018-11-08 19:23:39 +05:30
2023-06-20 00:43:36 +05:30
tooling/bin/gettext_extractor locale/gitlab.pot
2018-11-08 19:23:39 +05:30
2021-02-22 17:27:13 +05:30
Then commit and push the resulting changes to `#{pot_file_path}`.
2018-11-08 19:23:39 +05:30
The diff was:
#{pot_diff}
MSG
end
end
2021-02-22 17:27:13 +05:30
private
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
2021-02-22 17:27:13 +05:30
def ensure_locale_folder_presence!
unless Dir.exist?(locale_path)
raise <<~MSG
2023-06-20 00:43:36 +05:30
Cannot find '#{locale_path}' folder. Please ensure you're running this task from the gitlab repo.
2021-02-22 17:27:13 +05:30
MSG
end
end
def locale_path
@locale_path ||= Rails.root.join('locale')
end
def pot_file_path
@pot_file_path ||= File.join(locale_path, 'gitlab.pot')
end
2017-08-17 22:00:37 +05:30
end