2017-08-30 07:34:13 +00:00
|
|
|
module Gitlab
|
|
|
|
module I18n
|
2017-08-31 11:39:41 +00:00
|
|
|
class TranslationEntry
|
|
|
|
attr_reader :nplurals, :entry_data
|
|
|
|
|
|
|
|
def initialize(entry_data, nplurals)
|
|
|
|
@entry_data = entry_data
|
|
|
|
@nplurals = nplurals
|
|
|
|
end
|
|
|
|
|
2017-08-30 07:34:13 +00:00
|
|
|
def msgid
|
|
|
|
entry_data[:msgid]
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_id
|
|
|
|
entry_data[:msgid_plural]
|
|
|
|
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
|
2017-08-31 11:39:41 +00:00
|
|
|
all_translations.first if has_singular_translation?
|
2017-08-30 07:34:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_translations
|
2017-08-31 11:39:41 +00:00
|
|
|
@all_translations ||= entry_data.fetch_values(*translation_keys)
|
|
|
|
.reject(&:empty?)
|
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
|
|
|
|
|
2017-08-30 07:53:23 +00: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-30 07:34:13 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def translation_keys
|
2017-08-31 11:39:41 +00:00
|
|
|
@translation_keys ||= if has_plural?
|
2017-08-30 07:34:13 +00:00
|
|
|
entry_data.keys.select { |key| key =~ /msgstr\[\d+\]/ }
|
|
|
|
else
|
|
|
|
[:msgstr]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|