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