File: po_entry.rb

package info (click to toggle)
ruby-gettext 3.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,424 kB
  • sloc: ruby: 9,643; makefile: 8
file content (519 lines) | stat: -rw-r--r-- 15,991 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# Copyright (C) 2012-2019  Sutou Kouhei <kou@clear-code.com>
# Copyright (C) 2010  masone (Christian Felder) <ema@rh-productions.ch>
# Copyright (C) 2009  Masao Mutoh
#
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require "gettext/po_format"

module GetText
  class ParseError < StandardError
  end

  # Contains data related to the expression or sentence that
  # is to be translated.
  class POEntry
    class InvalidTypeError < StandardError
    end

    class NoMsgidError < StandardError
    end

    class NoMsgctxtError < StandardError
    end

    class NoMsgidPluralError < StandardError
    end

    PARAMS = {
      :normal => [:msgid, :separator, :msgstr],
      :plural => [:msgid, :msgid_plural, :separator, :msgstr],
      :msgctxt => [:msgctxt, :msgid, :msgstr],
      :msgctxt_plural => [:msgctxt, :msgid, :msgid_plural, :msgstr]
    }

    # Required
    attr_reader :type          # :normal, :plural, :msgctxt, :msgctxt_plural
    attr_accessor :msgid
    attr_accessor :msgstr
    # Options
    attr_accessor :msgid_plural
    attr_accessor :separator
    attr_accessor :msgctxt
    attr_accessor :references    # ["file1:line1", "file2:line2", ...]
    attr_accessor :translator_comment
    attr_accessor :extracted_comment
    # @return [Array<String>] The flags for this PO entry.
    # @since 3.0.4
    attr_accessor :flags
    attr_accessor :previous
    attr_accessor :comment

    # Create the object. +type+ should be :normal, :plural, :msgctxt or :msgctxt_plural.
    def initialize(type)
      self.type = type
      @translator_comment = nil
      @extracted_comment = nil
      @references = []
      @flags = []
      @previous = nil
      @msgctxt = nil
      @msgid = nil
      @msgid_plural = nil
      @msgstr = nil
    end

    # Support for extracted comments. Explanation s.
    # http://www.gnu.org/software/gettext/manual/gettext.html#Names
    # @return [void]
    def add_comment(new_comment)
      if (new_comment and ! new_comment.empty?)
        @extracted_comment ||= String.new
        @extracted_comment << "\n" unless @extracted_comment.empty?
        @extracted_comment << new_comment
      end
    end

    # @return [String, nil] The flag of the PO entry.
    # @deprecated Since 3.0.4. Use {#flags} instead.
    def flag
      @flags.first
    end

    # Set the new flag for the PO entry.
    #
    # @param [String, nil] flag The new flag.
    # @deprecated Since 3.0.4. Use {#flags=} instead.
    def flag=(flag)
      if flag.nil?
        @flags = []
      else
        @flags = [flag]
      end
    end

    # Checks if the self has same attributes as other.
    def ==(other)
      not other.nil? and
        type == other.type and
        msgid == other.msgid and
        msgstr == other.msgstr and
        msgid_plural == other.msgid_plural and
        separator == other.separator and
        msgctxt == other.msgctxt and
        translator_comment == other.translator_comment and
        extracted_comment == other.extracted_comment and
        references == other.references and
        flags == other.flags and
        previous == other.previous and
        comment == other.comment
    end

    def type=(type)
      unless PARAMS.has_key?(type)
        raise(InvalidTypeError, "\"%s\" is invalid type." % type)
      end
      @type = type
      @param_type = PARAMS[@type]
    end

    # Checks if the other translation target is mergeable with
    # the current one. Relevant are msgid and translation context (msgctxt).
    def mergeable?(other)
      other && other.msgid == self.msgid && other.msgctxt == self.msgctxt
    end

    # Merges two translation targets with the same msgid and returns the merged
    # result. If one is declared as plural and the other not, then the one
    # with the plural wins.
    def merge(other)
      return self unless other
      unless mergeable?(other)
        message = "Translation targets do not match: \n" +
          "  self: #{self.inspect}\n  other: '#{other.inspect}'"
        raise ParseError, message
      end
      if other.msgid_plural && !msgid_plural
        res = other
        unless res.references.include?(references[0])
          res.references += references
          res.add_comment(extracted_comment)
        end
      else
        res = self
        unless res.references.include?(other.references[0])
          res.references += other.references
          res.add_comment(other.extracted_comment)
        end
      end
      res
    end

    # Format the po entry in PO format.
    #
    # @param [Hash] options
    # @option options (see Formatter#initialize)
    def to_s(options={})
      raise(NoMsgidError, "msgid is nil.") unless @msgid

      formatter = Formatter.new(self, options)
      formatter.format
    end

    # Returns true if the type is kind of msgctxt.
    def msgctxt?
      [:msgctxt, :msgctxt_plural].include?(@type)
    end

    # Returns true if the type is kind of plural.
    def plural?
      [:plural, :msgctxt_plural].include?(@type)
    end

    # @return true if the entry is header entry, false otherwise.
    #   Header entry is normal type and has empty msgid.
    def header?
      @type == :normal and @msgid == ""
    end

    # @return true if the entry is obsolete entry, false otherwise.
    #   Obsolete entry is normal type and has :last msgid.
    def obsolete?
      @type == :normal and @msgid == :last
    end

    # @return true if the entry is fuzzy entry, false otherwise.
    #   Fuzzy entry has "fuzzy" flag.
    def fuzzy?
      @flags.include?("fuzzy")
    end

    # @return true if the entry is translated entry, false otherwise.
    def translated?
      return false if fuzzy?
      return false if @msgstr.nil? or @msgstr.empty?
      true
    end

    def [](number_or_param)
      __send__(resolve_param(number_or_param))
    end

    def []=(number_or_param, value)
      __send__("#{resolve_param(number_or_param)}=", value)
    end

    private
    def resolve_param(number_or_param)
      case number_or_param
      when Integer
        param = @param_type[number_or_param]
        raise ParseError, 'no more string parameters expected' unless param
        param
      else
        number_or_param
      end
    end

    # sets or extends the value of a translation target params like msgid,
    # msgctxt etc.
    #   param is symbol with the name of param
    #   value - new value
    def set_value(param, value)
      send "#{param}=", (send(param) || '') + value
    end

    class Formatter
      class << self
        def escape(string)
          return "" if string.nil?

          string.gsub(/([\\"\t\n])/) do
            special_character = $1
            case special_character
            when "\t"
              "\\t"
            when "\n"
              "\\n"
            else
              "\\#{special_character}"
            end
          end
        end
      end

      include POFormat

      DEFAULT_MAX_LINE_WIDTH = 78

      # @param [POEntry] entry The entry to be formatted.
      # @param [Hash] options
      # @option options [Bool] :include_translator_comment (true)
      #   Includes translator comments in formatted string if true.
      # @option options [Bool] :include_extracted_comment (true)
      #   Includes extracted comments in formatted string if true.
      # @option options [Bool] :include_reference_comment (true)
      #   Includes reference comments in formatted string if true.
      # @option options [Bool] :include_flag_comment (true)
      #   Includes flag comments in formatted string if true.
      # @option options [Bool] :include_previous_comment (true)
      #   Includes previous comments in formatted string if true.
      # @option options [Bool] :include_all_comments (true)
      #   Includes all comments in formatted string if true.
      #   Other specific `:include_XXX` options get preference over
      #   this option.
      #   You can remove all comments by specifying this option as
      #   false and omitting other `:include_XXX` options.
      # @option options [Integer] :max_line_width (78)
      #   Wraps long lines that is longer than the `:max_line_width`.
      #   Don't break long lines if `:max_line_width` is less than 0
      #   such as `-1`.
      # @option options [Encoding] :encoding (nil)
      #   Encodes to the specific encoding.
      def initialize(entry, options={})
        @entry = entry
        @options = normalize_options(options)
      end

      def format
        if @entry.obsolete?
          return format_obsolete_comment(@entry.comment)
        end

        str = format_comments

        # msgctxt, msgid, msgstr
        if @entry.msgctxt?
          if @entry.msgctxt.nil?
            no_msgctxt_message = "This POEntry is a kind of msgctxt " +
                                   "but the msgctxt property is nil. " +
                                   "msgid: #{@entry.msgid}"
            raise(NoMsgctxtError, no_msgctxt_message)
          end
          str << "msgctxt " << format_message(@entry.msgctxt)
        end

        str << "msgid " << format_message(@entry.msgid)
        if @entry.plural?
          if @entry.msgid_plural.nil?
            no_plural_message = "This POEntry is a kind of plural " +
                                  "but the msgid_plural property is nil. " +
                                  "msgid: #{@entry.msgid}"
            raise(NoMsgidPluralError, no_plural_message)
          end

          str << "msgid_plural " << format_message(@entry.msgid_plural)

          if @entry.msgstr.nil?
            str << "msgstr[0] \"\"\n"
            str << "msgstr[1] \"\"\n"
          else
            msgstrs = @entry.msgstr.split("\000", -1)
            msgstrs.each_with_index do |msgstr, index|
              str << "msgstr[#{index}] " << format_message(msgstr)
            end
          end
        else
          str << "msgstr "
          str << format_message(@entry.msgstr)
        end

        encode(str)
      end

      private
      def normalize_options(options)
        options = options.dup
        include_comment_keys = [
          :include_translator_comment,
          :include_extracted_comment,
          :include_reference_comment,
          :include_flag_comment,
          :include_previous_comment,
        ]
        if options[:include_all_comments].nil?
          options[:include_all_comments] = true
        end
        default_include_comment_value = options[:include_all_comments]
        include_comment_keys.each do |key|
          options[key] = default_include_comment_value if options[key].nil?
        end
        options[:max_line_width] ||= DEFAULT_MAX_LINE_WIDTH
        options
      end

      def include_translator_comment?
        @options[:include_translator_comment]
      end

      def include_extracted_comment?
        @options[:include_extracted_comment]
      end

      def include_reference_comment?
        @options[:include_reference_comment]
      end

      def include_flag_comment?
        @options[:include_flag_comment]
      end

      def include_previous_comment?
        @options[:include_previous_comment]
      end

      def format_comments
        formatted_comment = String.new
        if include_translator_comment?
          formatted_comment << format_translator_comment
        end
        if include_extracted_comment?
          formatted_comment << format_extracted_comment
        end
        if include_reference_comment?
          formatted_comment << format_reference_comment
        end
        if include_flag_comment?
          formatted_comment << format_flag_comment
        end
        if include_previous_comment?
          formatted_comment << format_previous_comment
        end
        formatted_comment
      end

      def format_translator_comment
        format_comment("#", @entry.translator_comment)
      end

      def format_extracted_comment
        format_comment(EXTRACTED_COMMENT_MARK, @entry.extracted_comment)
      end

      def format_reference_comment
        max_line_width = @options[:max_line_width]
        formatted_reference = String.new
        if not @entry.references.nil? and not @entry.references.empty?
          formatted_reference << REFERENCE_COMMENT_MARK
          line_width = 2
          @entry.references.each do |reference|
            if max_line_width > 0 and
                line_width + reference.size > max_line_width
              formatted_reference << "\n"
              formatted_reference <<  "#{REFERENCE_COMMENT_MARK} #{reference}"
              line_width = 3 + reference.size
            else
              formatted_reference << " #{reference}"
              line_width += 1 + reference.size
            end
          end

          formatted_reference << "\n"
        end
        formatted_reference
      end

      def format_flag_comment
        formatted_flags = String.new
        @entry.flags.each do |flag|
          formatted_flags << format_comment(FLAG_MARK, flag)
        end
        formatted_flags
      end

      def format_previous_comment
        format_comment(PREVIOUS_COMMENT_MARK, @entry.previous)
      end

      def format_comment(mark, comment)
        return "" if comment.nil?

        formatted_comment = String.new
        comment.each_line do |comment_line|
          if comment_line == "\n"
            formatted_comment << "#{mark}\n"
          else
            formatted_comment << "#{mark} #{comment_line.strip}\n"
          end
        end
        formatted_comment
      end

      def format_obsolete_comment(comment)
        mark = "#~"
        return "" if comment.nil?

        formatted_comment = String.new
        comment.each_line do |comment_line|
          if /\A#[^~]/ =~ comment_line or comment_line.start_with?(mark)
            formatted_comment << "#{comment_line.chomp}\n"
          elsif comment_line == "\n"
            formatted_comment << "\n"
          else
            formatted_comment << "#{mark} #{comment_line.strip}\n"
          end
        end
        formatted_comment
      end

      def format_message(message)
        empty_formatted_message = "\"\"\n"
        return empty_formatted_message if message.nil?

        chunks = wrap_message(message)
        return empty_formatted_message if chunks.empty?

        formatted_message = String.new
        if chunks.size > 1 or chunks.first.end_with?("\n")
          formatted_message << empty_formatted_message
        end
        chunks.each do |chunk|
          formatted_message << "\"#{escape(chunk)}\"\n"
        end
        formatted_message
      end

      def escape(string)
        self.class.escape(string)
      end

      def wrap_message(message)
        return [message] if message.empty?

        max_line_width = @options[:max_line_width]

        chunks = []
        message.each_line do |line|
          if max_line_width <= 0
            chunks << line
          else
            # TODO: use character width instead of the number of characters
            line.scan(/.{1,#{max_line_width}}\z|\S{#{max_line_width},}|.{1,#{max_line_width - 1}}\s/m) do |chunk|
              chunks << chunk
            end
          end
        end
        chunks
      end

      def encode(string)
        encoding = @options[:encoding]
        return string if encoding.nil?
        string.encode(encoding)
      end
    end
  end
end