debian-mirror-gitlab/lib/gitlab/i18n/translation_entry.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
2.8 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module I18n
class TranslationEntry
PERCENT_REGEX = /(?:^|[^%])%(?!{\w*}|[a-z%])/.freeze
2020-10-24 23:57:45 +05:30
ANGLE_BRACKET_REGEX = /[<>]/.freeze
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
attr_reader :nplurals, :entry_data
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
def initialize(entry_data:, nplurals:)
2018-03-17 18:26:18 +05:30
@entry_data = entry_data
@nplurals = nplurals
end
def msgid
2018-11-08 19:23:39 +05:30
@msgid ||= Array(entry_data[:msgid]).join
2018-03-17 18:26:18 +05:30
end
def plural_id
2018-11-08 19:23:39 +05:30
@plural_id ||= Array(entry_data[:msgid_plural]).join
2018-03-17 18:26:18 +05:30
end
def has_plural?
plural_id.present?
end
def singular_translation
2018-11-08 19:23:39 +05:30
all_translations.first.to_s if has_singular_translation?
2018-03-17 18:26:18 +05:30
end
def all_translations
2018-11-08 19:23:39 +05:30
@all_translations ||= translation_entries.map { |translation| Array(translation).join }
2018-03-17 18:26:18 +05:30
end
def translated?
all_translations.any?
end
def plural_translations
return [] unless has_plural?
return [] unless translated?
@plural_translations ||= if has_singular_translation?
all_translations.drop(1)
else
all_translations
end
end
def flag
entry_data[:flag]
end
def has_singular_translation?
nplurals > 1 || !has_plural?
end
2018-11-08 19:23:39 +05:30
def msgid_has_multiple_lines?
entry_data[:msgid].is_a?(Array)
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
def plural_id_has_multiple_lines?
entry_data[:msgid_plural].is_a?(Array)
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
def translations_have_multiple_lines?
translation_entries.any? { |translation| translation.is_a?(Array) }
2018-03-17 18:26:18 +05:30
end
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-10-24 23:57:45 +05:30
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
2018-03-17 18:26:18 +05:30
private
2020-10-24 23:57:45 +05:30
def contains_angle_brackets?(string)
string =~ ANGLE_BRACKET_REGEX
end
2018-11-08 19:23:39 +05:30
def translation_entries
@translation_entries ||= entry_data.fetch_values(*translation_keys)
.reject(&:empty?)
end
2018-03-17 18:26:18 +05:30
def translation_keys
@translation_keys ||= entry_data.keys.select { |key| key.to_s =~ /\Amsgstr(\[\d+\])?\z/ }
end
end
end
end