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
|
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
attr_reader :nplurals, :entry_data
|
|
|
|
|
|
|
|
def initialize(entry_data, nplurals)
|
|
|
|
@entry_data = entry_data
|
|
|
|
@nplurals = nplurals
|
|
|
|
end
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
def msgid
|
|
|
|
entry_data[:msgid]
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_id
|
|
|
|
entry_data[:msgid_plural]
|
|
|
|
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
|
2017-08-31 07:39:41 -04:00
|
|
|
all_translations.first if has_singular_translation?
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_translations
|
2017-08-31 07:39:41 -04:00
|
|
|
@all_translations ||= entry_data.fetch_values(*translation_keys)
|
|
|
|
.reject(&:empty?)
|
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
|
|
|
|
|
2017-08-30 03:53:23 -04:00
|
|
|
def msgid_contains_newlines?
|
|
|
|
msgid.is_a?(Array)
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_id_contains_newlines?
|
|
|
|
plural_id.is_a?(Array)
|
|
|
|
end
|
|
|
|
|
|
|
|
def translations_contain_newlines?
|
|
|
|
all_translations.any? { |translation| translation.is_a?(Array) }
|
|
|
|
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
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
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
|