2020-11-22 19:09:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-25 22:47:49 -04:00
|
|
|
require "gettext_i18n_rails/tasks"
|
|
|
|
|
|
|
|
namespace :gettext do
|
2017-06-01 16:07:56 -04:00
|
|
|
task :compile do
|
2019-09-18 10:02:45 -04:00
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab-foss/issues/33014#note_31218998
|
2020-11-22 19:09:02 -05:00
|
|
|
FileUtils.touch(pot_file_path)
|
2017-06-01 16:07:56 -04:00
|
|
|
|
|
|
|
Rake::Task['gettext:po_to_json'].invoke
|
|
|
|
end
|
2017-07-20 11:32:17 -04:00
|
|
|
|
2019-06-07 10:38:01 -04:00
|
|
|
desc 'Regenerate gitlab.pot file'
|
2018-07-06 08:40:23 -04:00
|
|
|
task :regenerate do
|
2020-11-22 19:09:02 -05:00
|
|
|
ensure_locale_folder_presence!
|
|
|
|
|
|
|
|
# Clean up folders that do not contain a gitlab.po file
|
|
|
|
Pathname.new(locale_path).children.each do |child|
|
|
|
|
next unless child.directory?
|
|
|
|
|
|
|
|
folder_path = child.to_path
|
|
|
|
|
|
|
|
if File.exist?("#{folder_path}/gitlab.po")
|
|
|
|
# remove all translated files to speed up finding
|
|
|
|
FileUtils.rm Dir["#{folder_path}/gitlab.*"]
|
|
|
|
else
|
|
|
|
# remove empty translation folders so we don't generate un-needed .po files
|
|
|
|
puts "Deleting #{folder_path} as it does not contain a 'gitlab.po' file."
|
|
|
|
|
|
|
|
FileUtils.rm_r folder_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-06 08:40:23 -04:00
|
|
|
# remove the `pot` file to ensure it's completely regenerated
|
2020-11-22 19:09:02 -05:00
|
|
|
FileUtils.rm_f(pot_file_path)
|
2018-07-06 08:40:23 -04:00
|
|
|
|
|
|
|
Rake::Task['gettext:find'].invoke
|
|
|
|
|
|
|
|
# leave only the required changes.
|
2020-10-12 11:08:32 -04:00
|
|
|
unless system(*%w(git -c core.hooksPath=/dev/null checkout -- locale/*/gitlab.po))
|
2020-09-03 23:08:22 -04:00
|
|
|
raise 'failed to cleanup generated locale/*/gitlab.po files'
|
|
|
|
end
|
2018-07-06 08:40:23 -04:00
|
|
|
|
2020-11-22 19:09:02 -05:00
|
|
|
raise 'gitlab.pot file not generated' unless File.exist?(pot_file_path)
|
|
|
|
|
2018-07-10 11:50:17 -04:00
|
|
|
# Remove timestamps from the pot file
|
2020-11-22 19:09:02 -05:00
|
|
|
pot_content = File.read pot_file_path
|
2018-07-10 11:50:17 -04:00
|
|
|
pot_content.gsub!(/^"POT?\-(?:Creation|Revision)\-Date\:.*\n/, '')
|
2020-11-22 19:09:02 -05:00
|
|
|
File.write pot_file_path, pot_content
|
2018-07-10 11:50:17 -04:00
|
|
|
|
2018-07-06 08:40:23 -04:00
|
|
|
puts <<~MSG
|
|
|
|
All done. Please commit the changes to `locale/gitlab.pot`.
|
|
|
|
|
|
|
|
MSG
|
|
|
|
end
|
|
|
|
|
2017-07-20 11:32:17 -04:00
|
|
|
desc 'Lint all po files in `locale/'
|
|
|
|
task lint: :environment do
|
2017-09-05 10:09:58 -04:00
|
|
|
require 'simple_po_parser'
|
2017-11-17 07:27:16 -05:00
|
|
|
require 'gitlab/utils'
|
2017-09-05 10:09:58 -04:00
|
|
|
|
2017-07-20 11:32:17 -04:00
|
|
|
FastGettext.silence_errors
|
|
|
|
files = Dir.glob(Rails.root.join('locale/*/gitlab.po'))
|
|
|
|
|
|
|
|
linters = files.map do |file|
|
|
|
|
locale = File.basename(File.dirname(file))
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
Gitlab::I18n::PoLinter.new(po_path: file, locale: locale)
|
2017-07-20 11:32:17 -04:00
|
|
|
end
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
linters.unshift(Gitlab::I18n::PoLinter.new(po_path: pot_file_path))
|
2017-07-20 11:32:17 -04:00
|
|
|
|
|
|
|
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-05-07 12:39:46 -04:00
|
|
|
task :updated_check do
|
2018-05-14 09:28:39 -04:00
|
|
|
# Removing all pre-translated files speeds up `gettext:find` as the
|
2018-05-07 12:39:46 -04:00
|
|
|
# files don't need to be merged.
|
2018-06-19 13:13:11 -04:00
|
|
|
# Having `LC_MESSAGES/gitlab.mo files present also confuses the output.
|
|
|
|
FileUtils.rm Dir['locale/**/gitlab.*']
|
2020-11-22 19:09:02 -05:00
|
|
|
FileUtils.rm_f pot_file_path
|
2018-05-07 12:39:46 -04:00
|
|
|
|
|
|
|
# `gettext:find` writes touches to temp files to `stderr` which would cause
|
2018-06-19 13:13:11 -04:00
|
|
|
# `static-analysis` to report failures. We can ignore these.
|
2018-11-29 21:00:04 -05:00
|
|
|
silence_stderr do
|
2018-06-19 13:13:11 -04:00
|
|
|
Rake::Task['gettext:find'].invoke
|
|
|
|
end
|
2018-05-07 12:39:46 -04:00
|
|
|
|
2020-11-22 19:09:02 -05:00
|
|
|
pot_diff = `git diff -- #{pot_file_path} | grep -E '^(\\+|-)msgid'`.strip
|
2018-05-07 12:39:46 -04:00
|
|
|
|
|
|
|
# reset the locale folder for potential next tasks
|
2018-05-14 09:28:39 -04:00
|
|
|
`git checkout -- locale`
|
2018-05-07 12:39:46 -04:00
|
|
|
|
2018-06-19 13:13:11 -04:00
|
|
|
if pot_diff.present?
|
2018-05-07 12:39:46 -04:00
|
|
|
raise <<~MSG
|
2020-11-22 19:09:02 -05:00
|
|
|
Changes in translated strings found, please update file `#{pot_file_path}` by running:
|
2018-05-07 12:39:46 -04:00
|
|
|
|
2018-07-06 08:40:23 -04:00
|
|
|
bin/rake gettext:regenerate
|
2018-05-07 12:39:46 -04:00
|
|
|
|
2020-11-22 19:09:02 -05:00
|
|
|
Then commit and push the resulting changes to `#{pot_file_path}`.
|
2018-05-07 12:39:46 -04:00
|
|
|
|
2018-06-19 13:13:11 -04:00
|
|
|
The diff was:
|
|
|
|
|
|
|
|
#{pot_diff}
|
2018-05-07 12:39:46 -04:00
|
|
|
MSG
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-22 19:09:02 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
# Customize list of translatable files
|
|
|
|
# See: https://github.com/grosser/gettext_i18n_rails#customizing-list-of-translatable-files
|
|
|
|
def files_to_translate
|
|
|
|
folders = %W(ee 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
|
|
|
|
|
|
|
|
# Disallow HTML from translatable strings
|
|
|
|
# See: https://docs.gitlab.com/ee/development/i18n/externalization.html#html
|
|
|
|
def html_todolist
|
|
|
|
return @html_todolist if defined?(@html_todolist)
|
|
|
|
|
|
|
|
@html_todolist = YAML.safe_load(File.read(Rails.root.join('lib/gitlab/i18n/html_todo.yml')))
|
|
|
|
end
|
|
|
|
|
2017-07-20 11:32:17 -04:00
|
|
|
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-08-26 13:41:18 -04:00
|
|
|
|
2018-11-29 21:00:04 -05:00
|
|
|
def silence_stderr(&block)
|
2018-08-26 13:41:18 -04:00
|
|
|
old_stderr = $stderr.dup
|
|
|
|
$stderr.reopen(File::NULL)
|
|
|
|
$stderr.sync = true
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$stderr.reopen(old_stderr)
|
|
|
|
old_stderr.close
|
|
|
|
end
|
2020-11-22 19:09:02 -05:00
|
|
|
|
|
|
|
def ensure_locale_folder_presence!
|
|
|
|
unless Dir.exist?(locale_path)
|
|
|
|
raise <<~MSG
|
|
|
|
Cannot find '#{locale_path}' folder. Please ensure you're running this task from the gitlab repo.
|
|
|
|
|
|
|
|
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-04-25 22:47:49 -04:00
|
|
|
end
|