2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-29 14:08:18 -04:00
|
|
|
require 'securerandom'
|
|
|
|
|
2017-08-25 03:04:50 -04:00
|
|
|
module Gitlab
|
|
|
|
module I18n
|
|
|
|
class PoLinter
|
2018-05-07 10:04:11 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
attr_reader :po_path, :translation_entries, :metadata_entry, :locale
|
2017-08-25 03:04:50 -04:00
|
|
|
|
|
|
|
VARIABLE_REGEX = /%{\w*}|%[a-z]/.freeze
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
def initialize(po_path:, locale: I18n.locale.to_s)
|
2017-08-25 03:04:50 -04:00
|
|
|
@po_path = po_path
|
|
|
|
@locale = locale
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
|
|
|
@errors ||= validate_po
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_po
|
2020-07-22 08:09:42 -04:00
|
|
|
if (parse_error = parse_po)
|
2017-08-25 03:04:50 -04:00
|
|
|
return 'PO-syntax errors' => [parse_error]
|
|
|
|
end
|
|
|
|
|
2020-11-13 10:09:24 -05:00
|
|
|
Gitlab::I18n.with_locale(locale) do
|
|
|
|
validate_entries
|
|
|
|
end
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_po
|
2017-08-31 07:39:41 -04:00
|
|
|
entries = SimplePoParser.parse(po_path)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
|
|
|
# The first entry is the metadata entry if there is one.
|
|
|
|
# This is an entry when empty `msgid`
|
2017-08-31 07:39:41 -04:00
|
|
|
if entries.first[:msgid].empty?
|
|
|
|
@metadata_entry = Gitlab::I18n::MetadataEntry.new(entries.shift)
|
|
|
|
else
|
|
|
|
return 'Missing metadata entry.'
|
|
|
|
end
|
|
|
|
|
|
|
|
@translation_entries = entries.map do |entry_data|
|
2020-07-22 08:09:42 -04:00
|
|
|
Gitlab::I18n::TranslationEntry.new(
|
|
|
|
entry_data: entry_data,
|
2020-12-09 10:10:12 -05:00
|
|
|
nplurals: metadata_entry.expected_forms
|
2020-07-22 08:09:42 -04:00
|
|
|
)
|
2017-08-31 07:39:41 -04:00
|
|
|
end
|
2017-08-30 03:34:13 -04:00
|
|
|
|
2017-08-25 03:04:50 -04:00
|
|
|
nil
|
|
|
|
rescue SimplePoParser::ParserError => e
|
2017-08-30 03:34:13 -04:00
|
|
|
@translation_entries = []
|
2017-08-25 03:04:50 -04:00
|
|
|
e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_entries
|
|
|
|
errors = {}
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
translation_entries.each do |entry|
|
2017-08-25 03:04:50 -04:00
|
|
|
errors_for_entry = validate_entry(entry)
|
2018-05-07 10:04:11 -04:00
|
|
|
errors[entry.msgid] = errors_for_entry if errors_for_entry.any?
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
errors
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_entry(entry)
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
validate_flags(errors, entry)
|
|
|
|
validate_variables(errors, entry)
|
|
|
|
validate_newlines(errors, entry)
|
2017-08-25 09:23:51 -04:00
|
|
|
validate_number_of_plurals(errors, entry)
|
2017-08-31 14:11:22 -04:00
|
|
|
validate_unescaped_chars(errors, entry)
|
2020-07-22 08:09:42 -04:00
|
|
|
validate_html(errors, entry)
|
2018-05-07 10:04:11 -04:00
|
|
|
validate_translation(errors, entry)
|
2017-08-25 03:04:50 -04:00
|
|
|
|
|
|
|
errors
|
|
|
|
end
|
|
|
|
|
2017-08-31 14:11:22 -04:00
|
|
|
def validate_unescaped_chars(errors, entry)
|
|
|
|
if entry.msgid_contains_unescaped_chars?
|
|
|
|
errors << 'contains unescaped `%`, escape it using `%%`'
|
|
|
|
end
|
|
|
|
|
|
|
|
if entry.plural_id_contains_unescaped_chars?
|
|
|
|
errors << 'plural id contains unescaped `%`, escape it using `%%`'
|
|
|
|
end
|
|
|
|
|
|
|
|
if entry.translations_contain_unescaped_chars?
|
|
|
|
errors << 'translation contains unescaped `%`, escape it using `%%`'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-22 08:09:42 -04:00
|
|
|
def validate_html(errors, entry)
|
|
|
|
common_message = 'contains < or >. Use variables to include HTML in the string, or the < and > codes ' \
|
|
|
|
'for the symbols. For more info see: https://docs.gitlab.com/ee/development/i18n/externalization.html#html'
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
if entry.msgid_contains_potential_html?
|
2020-07-22 08:09:42 -04:00
|
|
|
errors << common_message
|
|
|
|
end
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
if entry.plural_id_contains_potential_html?
|
2020-07-22 08:09:42 -04:00
|
|
|
errors << 'plural id ' + common_message
|
|
|
|
end
|
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
if entry.translations_contain_potential_html?
|
2020-07-22 08:09:42 -04:00
|
|
|
errors << 'translation ' + common_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-25 09:23:51 -04:00
|
|
|
def validate_number_of_plurals(errors, entry)
|
2018-05-14 09:28:39 -04:00
|
|
|
return unless metadata_entry&.expected_forms
|
2017-08-25 09:23:51 -04:00
|
|
|
return unless entry.translated?
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
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)}"
|
2017-08-25 09:23:51 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-25 03:04:50 -04:00
|
|
|
|
2017-08-25 09:23:51 -04:00
|
|
|
def validate_newlines(errors, entry)
|
2018-05-14 09:28:39 -04:00
|
|
|
if entry.msgid_has_multiple_lines?
|
2017-08-31 14:11:22 -04:00
|
|
|
errors << 'is defined over multiple lines, this breaks some tooling.'
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
if entry.plural_id_has_multiple_lines?
|
2017-08-31 14:11:22 -04:00
|
|
|
errors << 'plural is defined over multiple lines, this breaks some tooling.'
|
2017-08-30 03:53:23 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
if entry.translations_have_multiple_lines?
|
2017-08-31 14:11:22 -04:00
|
|
|
errors << 'has translations defined over multiple lines, this breaks some tooling.'
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_variables(errors, entry)
|
2017-08-31 07:39:41 -04:00
|
|
|
if entry.has_singular_translation?
|
2018-05-07 11:56:18 -04:00
|
|
|
validate_variables_in_message(errors, entry.msgid, entry.msgid)
|
|
|
|
|
2017-08-25 09:23:51 -04:00
|
|
|
validate_variables_in_message(errors, entry.msgid, entry.singular_translation)
|
|
|
|
end
|
2017-08-25 03:04:50 -04:00
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
if entry.has_plural?
|
2018-05-07 11:56:18 -04:00
|
|
|
validate_variables_in_message(errors, entry.plural_id, entry.plural_id)
|
|
|
|
|
2017-08-25 05:23:48 -04:00
|
|
|
entry.plural_translations.each do |translation|
|
|
|
|
validate_variables_in_message(errors, entry.plural_id, translation)
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_variables_in_message(errors, message_id, message_translation)
|
|
|
|
required_variables = message_id.scan(VARIABLE_REGEX)
|
|
|
|
|
|
|
|
validate_unnamed_variables(errors, required_variables)
|
|
|
|
validate_variable_usage(errors, message_translation, required_variables)
|
|
|
|
end
|
|
|
|
|
2018-05-07 10:04:11 -04:00
|
|
|
def validate_translation(errors, entry)
|
2020-11-13 10:09:24 -05:00
|
|
|
if entry.has_plural?
|
|
|
|
translate_plural(entry)
|
|
|
|
else
|
|
|
|
translate_singular(entry)
|
2018-05-07 10:04:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# `sprintf` could raise an `ArgumentError` when invalid passing something
|
|
|
|
# other than a Hash when using named variables
|
|
|
|
#
|
|
|
|
# `sprintf` could raise `TypeError` when passing a wrong type when using
|
|
|
|
# unnamed variables
|
|
|
|
#
|
|
|
|
# FastGettext::Translation could raise `RuntimeError` (raised as a string),
|
|
|
|
# or as subclassess `NoTextDomainConfigured` & `InvalidFormat`
|
|
|
|
#
|
|
|
|
# `FastGettext::Translation` could raise `ArgumentError` as subclassess
|
|
|
|
# `InvalidEncoding`, `IllegalSequence` & `InvalidCharacter`
|
|
|
|
rescue ArgumentError, TypeError, RuntimeError => e
|
|
|
|
errors << "Failure translating to #{locale}: #{e.message}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def translate_singular(entry)
|
|
|
|
used_variables = entry.msgid.scan(VARIABLE_REGEX)
|
2017-08-25 03:04:50 -04:00
|
|
|
variables = fill_in_variables(used_variables)
|
|
|
|
|
2018-05-07 10:04:11 -04:00
|
|
|
translation = if entry.msgid.include?('|')
|
|
|
|
FastGettext::Translation.s_(entry.msgid)
|
|
|
|
else
|
|
|
|
FastGettext::Translation._(entry.msgid)
|
|
|
|
end
|
2017-08-25 03:04:50 -04:00
|
|
|
|
2018-05-07 10:04:11 -04:00
|
|
|
translation % variables if used_variables.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def translate_plural(entry)
|
|
|
|
numbers_covering_all_plurals.map do |number|
|
|
|
|
translation = FastGettext::Translation.n_(entry.msgid, entry.plural_id, number)
|
2019-08-14 05:46:09 -04:00
|
|
|
index = index_for_pluralization(number)
|
2019-09-10 05:51:37 -04:00
|
|
|
used_variables = index == 0 ? entry.msgid.scan(VARIABLE_REGEX) : entry.plural_id.scan(VARIABLE_REGEX)
|
|
|
|
variables = fill_in_variables(used_variables)
|
2018-05-07 10:04:11 -04:00
|
|
|
|
2019-08-14 05:46:09 -04:00
|
|
|
translation % variables if variables.any?
|
2018-05-07 10:04:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def numbers_covering_all_plurals
|
2018-05-14 09:28:39 -04:00
|
|
|
@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
|
2018-05-07 10:04:11 -04:00
|
|
|
end
|
2018-05-14 09:28:39 -04:00
|
|
|
|
|
|
|
required_numbers
|
2018-05-07 10:04:11 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
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
|
2020-11-13 10:09:24 -05:00
|
|
|
pluralization_result = FastGettext.pluralisation_rule.call(counter)
|
2017-08-25 03:04:50 -04:00
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
case pluralization_result
|
|
|
|
when false
|
|
|
|
0
|
|
|
|
when true
|
|
|
|
1
|
2018-05-07 10:04:11 -04:00
|
|
|
else
|
|
|
|
pluralization_result
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_in_variables(variables)
|
|
|
|
if variables.empty?
|
|
|
|
[]
|
|
|
|
elsif variables.any? { |variable| unnamed_variable?(variable) }
|
|
|
|
variables.map do |variable|
|
2022-04-29 14:08:18 -04:00
|
|
|
variable == '%d' ? random_number : random_string
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
else
|
2022-04-08 05:08:18 -04:00
|
|
|
variables.each_with_object({}) do |variable, hash|
|
2017-08-25 03:04:50 -04:00
|
|
|
variable_name = variable[/\w+/]
|
2022-04-29 14:08:18 -04:00
|
|
|
hash[variable_name] = random_string
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-29 14:08:18 -04:00
|
|
|
def random_number
|
|
|
|
Random.rand(1000)
|
|
|
|
end
|
|
|
|
|
|
|
|
def random_string
|
|
|
|
SecureRandom.alphanumeric(64)
|
|
|
|
end
|
|
|
|
|
2017-08-25 03:04:50 -04:00
|
|
|
def validate_unnamed_variables(errors, variables)
|
2018-05-14 09:28:39 -04:00
|
|
|
unnamed_variables, named_variables = variables.partition { |name| unnamed_variable?(name) }
|
|
|
|
|
|
|
|
if unnamed_variables.any? && named_variables.any?
|
2018-05-07 10:04:11 -04:00
|
|
|
errors << 'is combining named variables with unnamed variables'
|
|
|
|
end
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
if unnamed_variables.size > 1
|
2017-08-25 03:04:50 -04:00
|
|
|
errors << 'is combining multiple unnamed variables'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_variable_usage(errors, translation, required_variables)
|
|
|
|
# We don't need to validate when the message is empty.
|
2018-11-19 09:08:23 -05:00
|
|
|
# In this case we fall back to the default, which has all the
|
2017-08-31 07:39:41 -04:00
|
|
|
# required variables.
|
2017-08-25 03:04:50 -04:00
|
|
|
return if translation.empty?
|
|
|
|
|
|
|
|
found_variables = translation.scan(VARIABLE_REGEX)
|
|
|
|
|
|
|
|
missing_variables = required_variables - found_variables
|
|
|
|
if missing_variables.any?
|
|
|
|
errors << "<#{translation}> is missing: [#{missing_variables.to_sentence}]"
|
|
|
|
end
|
|
|
|
|
|
|
|
unknown_variables = found_variables - required_variables
|
|
|
|
if unknown_variables.any?
|
|
|
|
errors << "<#{translation}> is using unknown variables: [#{unknown_variables.to_sentence}]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unnamed_variable?(variable_name)
|
|
|
|
!variable_name.start_with?('%{')
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_flags(errors, entry)
|
2017-08-25 05:23:48 -04:00
|
|
|
errors << "is marked #{entry.flag}" if entry.flag
|
2017-08-25 03:04:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|