Class: GetText::POEntry::Formatter

Inherits:
Object
  • Object
show all
Includes:
GetText::POFormat
Defined in:
lib/gettext/po_entry.rb

Constant Summary collapse

DEFAULT_MAX_LINE_WIDTH =
78

Constants included from GetText::POFormat

GetText::POFormat::EXTRACTED_COMMENT_MARK, GetText::POFormat::FLAG_MARK, GetText::POFormat::PREVIOUS_COMMENT_MARK, GetText::POFormat::REFERENCE_COMMENT_MARK, GetText::POFormat::TRANSLATOR_COMMENT_MARK

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, options = {}) ⇒ Formatter

Returns a new instance of Formatter.

Parameters:

  • entry (POEntry)

    The entry to be formatted.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :include_translator_comment (Bool) — default: true

    Includes translator comments in formatted string if true.

  • :include_extracted_comment (Bool) — default: true

    Includes extracted comments in formatted string if true.

  • :include_reference_comment (Bool) — default: true

    Includes reference comments in formatted string if true.

  • :include_flag_comment (Bool) — default: true

    Includes flag comments in formatted string if true.

  • :include_previous_comment (Bool) — default: true

    Includes previous comments in formatted string if true.

  • :include_all_comments (Bool) — default: 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.

  • :max_line_width (Integer) — default: 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.

  • :use_one_line_per_reference (Bool) — default: false

    Whether each reference comment uses one line or not. If this is true, :max_line_width is ignored for reference comment.

  • :encoding (Encoding) — default: nil

    Encodes to the specific encoding.



290
291
292
293
# File 'lib/gettext/po_entry.rb', line 290

def initialize(entry, options={})
  @entry = entry
  @options = normalize_options(options)
end

Class Method Details

.escape(string) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/gettext/po_entry.rb', line 241

def escape(string)
  return "" if string.nil?

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

Instance Method Details

#formatObject



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
# File 'lib/gettext/po_entry.rb', line 295

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