2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
module Gitlab
|
|
|
|
module I18n
|
2017-08-31 07:39:41 -04:00
|
|
|
class TranslationEntry
|
2017-08-31 14:11:22 -04:00
|
|
|
PERCENT_REGEX = /(?:^|[^%])%(?!{\w*}|[a-z%])/.freeze
|
2020-07-22 08:09:42 -04:00
|
|
|
ANGLE_BRACKET_REGEX = /[<>]/.freeze
|
2017-08-31 14:11:22 -04:00
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
attr_reader :nplurals, :entry_data
|
2017-08-31 07:39:41 -04:00
|
|
|
|
2020-12-09 10:10:12 -05:00
|
|
|
def initialize(entry_data:, nplurals:)
|
2017-08-31 07:39:41 -04:00
|
|
|
@entry_data = entry_data
|
|
|
|
@nplurals = nplurals
|
|
|
|
end
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
def msgid
|
2018-05-07 10:04:11 -04:00
|
|
|
@msgid ||= Array(entry_data[:msgid]).join
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def plural_id
|
2018-05-07 10:04:11 -04:00
|
|
|
@plural_id ||= Array(entry_data[:msgid_plural]).join
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
def has_plural?
|
2017-08-30 03:34:13 -04:00
|
|
|
plural_id.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def singular_translation
|
2018-05-07 10:04:11 -04:00
|
|
|
all_translations.first.to_s if has_singular_translation?
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_translations
|
2018-05-07 10:04:11 -04:00
|
|
|
@all_translations ||= translation_entries.map { |translation| Array(translation).join }
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def translated?
|
|
|
|
all_translations.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_translations
|
2017-08-31 07:39:41 -04:00
|
|
|
return [] unless has_plural?
|
2017-08-30 03:34:13 -04:00
|
|
|
return [] unless translated?
|
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
@plural_translations ||= if has_singular_translation?
|
|
|
|
all_translations.drop(1)
|
|
|
|
else
|
|
|
|
all_translations
|
|
|
|
end
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def flag
|
|
|
|
entry_data[:flag]
|
|
|
|
end
|
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
def has_singular_translation?
|
|
|
|
nplurals > 1 || !has_plural?
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
def msgid_has_multiple_lines?
|
2018-05-07 10:04:11 -04:00
|
|
|
entry_data[:msgid].is_a?(Array)
|
2017-08-30 03:53:23 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
def plural_id_has_multiple_lines?
|
2018-05-07 10:04:11 -04:00
|
|
|
entry_data[:msgid_plural].is_a?(Array)
|
2017-08-30 03:53:23 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
def translations_have_multiple_lines?
|
2018-05-07 10:04:11 -04:00
|
|
|
translation_entries.any? { |translation| translation.is_a?(Array) }
|
2017-08-30 03:53:23 -04:00
|
|
|
end
|
|
|
|
|
2017-08-31 14:11:22 -04:00
|
|
|
def msgid_contains_unescaped_chars?
|
|
|
|
contains_unescaped_chars?(msgid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_id_contains_unescaped_chars?
|
|
|
|
contains_unescaped_chars?(plural_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def translations_contain_unescaped_chars?
|
|
|
|
all_translations.any? { |translation| contains_unescaped_chars?(translation) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def contains_unescaped_chars?(string)
|
|
|
|
string =~ PERCENT_REGEX
|
|
|
|
end
|
|
|
|
|
2020-07-22 08:09:42 -04:00
|
|
|
def msgid_contains_potential_html?
|
|
|
|
contains_angle_brackets?(msgid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_id_contains_potential_html?
|
|
|
|
contains_angle_brackets?(plural_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def translations_contain_potential_html?
|
|
|
|
all_translations.any? { |translation| contains_angle_brackets?(translation) }
|
|
|
|
end
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
private
|
|
|
|
|
2020-07-22 08:09:42 -04:00
|
|
|
def contains_angle_brackets?(string)
|
|
|
|
string =~ ANGLE_BRACKET_REGEX
|
|
|
|
end
|
|
|
|
|
2018-05-07 10:04:11 -04:00
|
|
|
def translation_entries
|
|
|
|
@translation_entries ||= entry_data.fetch_values(*translation_keys)
|
|
|
|
.reject(&:empty?)
|
|
|
|
end
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
def translation_keys
|
2017-08-31 13:00:29 -04:00
|
|
|
@translation_keys ||= entry_data.keys.select { |key| key.to_s =~ /\Amsgstr(\[\d+\])?\z/ }
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|