# File lib/gettext/po_entry.rb, line 185 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
@param [POEntry] entry The entry to be formatted. @param [Hash] options @option options [Bool] :include_reference_comment (true)
Includes reference comments in formatted string if true.
@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.
# File lib/gettext/po_entry.rb, line 216 def initialize(entry, options={}) @entry = entry @options = fill_default_option_values(options) end
# File lib/gettext/po_entry.rb, line 221 def format # extracted comments if @entry.msgid == :last return format_obsolete_comment(@entry.comment) end str = "" str << format_translator_comment str << format_extracted_comment if @options[:include_reference_comment] str << format_reference_comment end str << format_flag_comment str << format_previous_comment # 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("\0000", -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
# File lib/gettext/po_entry.rb, line 387 def encode(string) encoding = @options[:encoding] return string if encoding.nil? string.encode(encoding) end
# File lib/gettext/po_entry.rb, line 367 def escape(string) self.class.escape(string) end
# File lib/gettext/po_entry.rb, line 276 def fill_default_option_values(options) options = options.dup if options[:include_reference_comment].nil? options[:include_reference_comment] = true end options[:max_line_width] ||= DEFAULT_MAX_LINE_WIDTH options end
# File lib/gettext/po_entry.rb, line 324 def format_comment(mark, comment) return "" if comment.nil? formatted_comment = "" 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
# File lib/gettext/po_entry.rb, line 289 def format_extracted_comment format_comment(EXTRACTED_COMMENT_MARK, @entry.extracted_comment) end
# File lib/gettext/po_entry.rb, line 316 def format_flag_comment format_comment(FLAG_MARK, @entry.flag) end
# File lib/gettext/po_entry.rb, line 355 def format_message(message) return "\"\"\n" if message.nil? chunks = wrap_message(message) formatted_message = "" formatted_message << "\"\"\n" if chunks.size > 1 chunks.each do |chunk| formatted_message << "\"#{escape(chunk)}\"\n" end formatted_message end
# File lib/gettext/po_entry.rb, line 338 def format_obsolete_comment(comment) mark = "#~" return "" if comment.nil? formatted_comment = "" comment.each_line do |comment_line| if /\A#[^~]/ =~ comment_line or comment_line.start_with?(mark) formatted_comment << comment_line elsif comment_line == "\n" formatted_comment << "\n" else formatted_comment << "#{mark} #{comment_line.strip}\n" end end formatted_comment end
# File lib/gettext/po_entry.rb, line 320 def format_previous_comment format_comment(PREVIOUS_COMMENT_MARK, @entry.previous) end
# File lib/gettext/po_entry.rb, line 293 def format_reference_comment max_line_width = @options[:max_line_width] formatted_reference = "" 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
# File lib/gettext/po_entry.rb, line 285 def format_translator_comment format_comment("#", @entry.translator_comment) end
# File lib/gettext/po_entry.rb, line 371 def wrap_message(message) return [message] if message.empty? max_line_width = @options[:max_line_width] return [message] if max_line_width <= 0 chunks = [] message.each_line do |line| # TODO: use character width instead of the number of characters line.scan(/.{1,#{max_line_width}}/) do |chunk| chunks << chunk end end chunks end