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
path: root/lib
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2018-05-14 16:28:39 +0300
committerBob Van Landuyt <bob@vanlanduyt.co>2018-06-15 15:58:47 +0300
commit17fc178cb5a68be787cb3fa243aed4cf1abb24a3 (patch)
treee49c6f9d71db4c8bacefe45afbe99b660737e0d9 /lib
parentf5d45e0632a2b7accbd5b2daae6966755865d50b (diff)
Correctly translate all forms in tests
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/i18n/metadata_entry.rb11
-rw-r--r--lib/gitlab/i18n/po_linter.rb59
-rw-r--r--lib/gitlab/i18n/translation_entry.rb6
-rw-r--r--lib/tasks/gettext.rake8
4 files changed, 60 insertions, 24 deletions
diff --git a/lib/gitlab/i18n/metadata_entry.rb b/lib/gitlab/i18n/metadata_entry.rb
index 35d57459a3d..36fc1bcdcb7 100644
--- a/lib/gitlab/i18n/metadata_entry.rb
+++ b/lib/gitlab/i18n/metadata_entry.rb
@@ -3,16 +3,25 @@ module Gitlab
class MetadataEntry
attr_reader :entry_data
+ # Avoid testing too many plurals if `nplurals` was incorrectly set.
+ # Based on info on https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
+ # which mentions special cases for numbers ending in 2 digits
+ MAX_FORMS_TO_TEST = 101
+
def initialize(entry_data)
@entry_data = entry_data
end
- def expected_plurals
+ def expected_forms
return nil unless plural_information
plural_information['nplurals'].to_i
end
+ def forms_to_test
+ @forms_to_test ||= [expected_forms, MAX_FORMS_TO_TEST].compact.min
+ end
+
private
def plural_information
diff --git a/lib/gitlab/i18n/po_linter.rb b/lib/gitlab/i18n/po_linter.rb
index ba08ef59776..d8e7269a2c2 100644
--- a/lib/gitlab/i18n/po_linter.rb
+++ b/lib/gitlab/i18n/po_linter.rb
@@ -36,7 +36,7 @@ module Gitlab
end
@translation_entries = entries.map do |entry_data|
- Gitlab::I18n::TranslationEntry.new(entry_data, metadata_entry.expected_plurals)
+ Gitlab::I18n::TranslationEntry.new(entry_data, metadata_entry.expected_forms)
end
nil
@@ -84,25 +84,25 @@ module Gitlab
end
def validate_number_of_plurals(errors, entry)
- return unless metadata_entry&.expected_plurals
+ return unless metadata_entry&.expected_forms
return unless entry.translated?
- if entry.has_plural? && entry.all_translations.size != metadata_entry.expected_plurals
- errors << "should have #{metadata_entry.expected_plurals} "\
- "#{'translations'.pluralize(metadata_entry.expected_plurals)}"
+ if entry.has_plural? && entry.all_translations.size != metadata_entry.expected_forms
+ errors << "should have #{metadata_entry.expected_forms} "\
+ "#{'translations'.pluralize(metadata_entry.expected_forms)}"
end
end
def validate_newlines(errors, entry)
- if entry.msgid_contains_newlines?
+ if entry.msgid_has_multiple_lines?
errors << 'is defined over multiple lines, this breaks some tooling.'
end
- if entry.plural_id_contains_newlines?
+ if entry.plural_id_has_multiple_lines?
errors << 'plural is defined over multiple lines, this breaks some tooling.'
end
- if entry.translations_contain_newlines?
+ if entry.translations_have_multiple_lines?
errors << 'has translations defined over multiple lines, this breaks some tooling.'
end
end
@@ -179,16 +179,41 @@ module Gitlab
end
def numbers_covering_all_plurals
- @numbers_covering_all_plurals ||= Array.new(metadata_entry.expected_plurals) do |index|
- number_for_pluralization(index)
+ @numbers_covering_all_plurals ||= calculate_numbers_covering_all_plurals
+ end
+
+ def calculate_numbers_covering_all_plurals
+ required_numbers = []
+ discovered_indexes = []
+ counter = 0
+
+ while discovered_indexes.size < metadata_entry.forms_to_test && counter < Gitlab::I18n::MetadataEntry::MAX_FORMS_TO_TEST
+ index_for_count = index_for_pluralization(counter)
+
+ unless discovered_indexes.include?(index_for_count)
+ discovered_indexes << index_for_count
+ required_numbers << counter
+ end
+
+ counter += 1
end
+
+ required_numbers
end
- def number_for_pluralization(counter)
- pluralization_result = FastGettext.pluralisation_rule.call(counter)
+ def index_for_pluralization(counter)
+ # This calls the C function that defines the pluralization rule, it can
+ # return a boolean (`false` represents 0, `true` represents 1) or an integer
+ # that specifies the plural form to be used for the given number
+ pluralization_result = Gitlab::I18n.with_locale(locale) do
+ FastGettext.pluralisation_rule.call(counter)
+ end
- if pluralization_result.is_a?(TrueClass) || pluralization_result.is_a?(FalseClass)
- counter
+ case pluralization_result
+ when false
+ 0
+ when true
+ 1
else
pluralization_result
end
@@ -211,11 +236,13 @@ module Gitlab
end
def validate_unnamed_variables(errors, variables)
- if variables.any? { |name| unnamed_variable?(name) } && variables.any? { |name| !unnamed_variable?(name) }
+ unnamed_variables, named_variables = variables.partition { |name| unnamed_variable?(name) }
+
+ if unnamed_variables.any? && named_variables.any?
errors << 'is combining named variables with unnamed variables'
end
- if variables.select { |variable_name| unnamed_variable?(variable_name) }.size > 1
+ if unnamed_variables.size > 1
errors << 'is combining multiple unnamed variables'
end
end
diff --git a/lib/gitlab/i18n/translation_entry.rb b/lib/gitlab/i18n/translation_entry.rb
index 13c544aed27..54adb98f42d 100644
--- a/lib/gitlab/i18n/translation_entry.rb
+++ b/lib/gitlab/i18n/translation_entry.rb
@@ -53,15 +53,15 @@ module Gitlab
nplurals > 1 || !has_plural?
end
- def msgid_contains_newlines?
+ def msgid_has_multiple_lines?
entry_data[:msgid].is_a?(Array)
end
- def plural_id_contains_newlines?
+ def plural_id_has_multiple_lines?
entry_data[:msgid_plural].is_a?(Array)
end
- def translations_contain_newlines?
+ def translations_have_multiple_lines?
translation_entries.any? { |translation| translation.is_a?(Array) }
end
diff --git a/lib/tasks/gettext.rake b/lib/tasks/gettext.rake
index efe2997d78e..d52c419d66b 100644
--- a/lib/tasks/gettext.rake
+++ b/lib/tasks/gettext.rake
@@ -51,7 +51,7 @@ namespace :gettext do
end
task :updated_check do
- # Removeing all pre-translated files speeds up `gettext:find` as the
+ # Removing all pre-translated files speeds up `gettext:find` as the
# files don't need to be merged.
`rm locale/*/gitlab.po`
@@ -62,15 +62,15 @@ namespace :gettext do
changed_files = `git diff --name-only`.lines.map(&:strip)
# reset the locale folder for potential next tasks
- `git checkout locale`
+ `git checkout -- locale`
if changed_files.include?('locale/gitlab.pot')
raise <<~MSG
Newly translated strings found, please add them to `gitlab.pot` by running:
- bundle exec rake gettext:find; git checkout locale/*/gitlab.po;
+ bundle exec rake gettext:find; git checkout -- locale/*/gitlab.po;
- Then check in the resulting `locale/gitlab.pot`
+ Then commit and push the resulting changes to `locale/gitlab.pot`.
MSG
end