summaryrefslogtreecommitdiff
path: root/lib/gitlab/i18n/po_entry.rb
blob: aabb477bbea07f4a7937148a7d41993a58c09139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Gitlab
  module I18n
    class PoEntry
      attr_reader :entry_data

      def initialize(entry_data)
        @entry_data = entry_data
      end

      def msgid
        entry_data[:msgid]
      end

      def metadata?
        msgid.empty?
      end

      def plural_id
        entry_data[:msgid_plural]
      end

      def plural?
        plural_id.present?
      end

      def singular_translation
        plural? ? entry_data['msgstr[0]'] : entry_data[:msgstr]
      end

      def all_translations
        @all_translations ||= entry_data.fetch_values(*translation_keys)
      end

      def plural_translations
        return [] unless plural?

        # The singular translation is used if there's only translation. This is
        # the case for languages without plurals.
        return all_translations if all_translations.size == 1

        entry_data.fetch_values(*plural_translation_keys)
      end

      def flag
        entry_data[:flag]
      end

      private

      def plural_translation_keys
        @plural_translation_keys ||= translation_keys.select do |key|
          plural_index = key.scan(/\d+/).first.to_i
          plural_index > 0
        end
      end

      def translation_keys
        @translation_keys ||= if plural?
                                entry_data.keys.select { |key| key =~ /msgstr\[\d+\]/ }
                              else
                                [:msgstr]
                              end
      end
    end
  end
end