Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tasks/gettext.rake')
-rw-r--r--lib/tasks/gettext.rake105
1 files changed, 71 insertions, 34 deletions
diff --git a/lib/tasks/gettext.rake b/lib/tasks/gettext.rake
index e2c92054d62..e03c78d5a40 100644
--- a/lib/tasks/gettext.rake
+++ b/lib/tasks/gettext.rake
@@ -1,39 +1,38 @@
+# frozen_string_literal: true
+
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
- 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.load_file(Rails.root.join('lib/gitlab/i18n/html_todo.yml'))
- end
-
task :compile do
# See: https://gitlab.com/gitlab-org/gitlab-foss/issues/33014#note_31218998
- FileUtils.touch(File.join(Rails.root, 'locale/gitlab.pot'))
+ FileUtils.touch(pot_file_path)
Rake::Task['gettext:po_to_json'].invoke
end
desc 'Regenerate gitlab.pot file'
task :regenerate do
- pot_file = 'locale/gitlab.pot'
- # Remove all translated files, this speeds up finding
- FileUtils.rm Dir['locale/**/gitlab.*']
+ 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
+
# remove the `pot` file to ensure it's completely regenerated
- FileUtils.rm_f pot_file
+ FileUtils.rm_f(pot_file_path)
Rake::Task['gettext:find'].invoke
@@ -42,10 +41,12 @@ namespace :gettext do
raise 'failed to cleanup generated locale/*/gitlab.po files'
end
+ raise 'gitlab.pot file not generated' unless File.exist?(pot_file_path)
+
# Remove timestamps from the pot file
- pot_content = File.read pot_file
+ pot_content = File.read pot_file_path
pot_content.gsub!(/^"POT?\-(?:Creation|Revision)\-Date\:.*\n/, '')
- File.write pot_file, pot_content
+ File.write pot_file_path, pot_content
puts <<~MSG
All done. Please commit the changes to `locale/gitlab.pot`.
@@ -64,11 +65,10 @@ namespace :gettext do
linters = files.map do |file|
locale = File.basename(File.dirname(file))
- Gitlab::I18n::PoLinter.new(po_path: file, html_todolist: html_todolist, locale: locale)
+ Gitlab::I18n::PoLinter.new(po_path: file, locale: locale)
end
- pot_file = Rails.root.join('locale/gitlab.pot')
- linters.unshift(Gitlab::I18n::PoLinter.new(po_path: pot_file, html_todolist: html_todolist))
+ linters.unshift(Gitlab::I18n::PoLinter.new(po_path: pot_file_path))
failed_linters = linters.select { |linter| linter.errors.any? }
@@ -84,12 +84,11 @@ namespace :gettext do
end
task :updated_check do
- pot_file = 'locale/gitlab.pot'
# 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.*']
- FileUtils.rm_f pot_file
+ FileUtils.rm_f pot_file_path
# `gettext:find` writes touches to temp files to `stderr` which would cause
# `static-analysis` to report failures. We can ignore these.
@@ -97,18 +96,18 @@ namespace :gettext do
Rake::Task['gettext:find'].invoke
end
- pot_diff = `git diff -- #{pot_file} | grep -E '^(\\+|-)msgid'`.strip
+ pot_diff = `git diff -- #{pot_file_path} | grep -E '^(\\+|-)msgid'`.strip
# reset the locale folder for potential next tasks
`git checkout -- locale`
if pot_diff.present?
raise <<~MSG
- Changes in translated strings found, please update file `#{pot_file}` by running:
+ Changes in translated strings found, please update file `#{pot_file_path}` by running:
bin/rake gettext:regenerate
- Then commit and push the resulting changes to `#{pot_file}`.
+ Then commit and push the resulting changes to `#{pot_file_path}`.
The diff was:
@@ -117,6 +116,27 @@ namespace :gettext do
end
end
+ 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
+
def report_errors_for_file(file, errors_for_file)
puts "Errors in `#{file}`:"
@@ -140,4 +160,21 @@ namespace :gettext do
$stderr.reopen(old_stderr)
old_stderr.close
end
+
+ 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
end