lib/gettext/po_entry.rb in gettext-3.0.3 vs lib/gettext/po_entry.rb in gettext-3.0.4
- old
+ new
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
#
-# Copyright (C) 2012-2013 Kouhei Sutou <kou@clear-code.com>
+# Copyright (C) 2012-2014 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2010 masone (Christian Felder) <ema@rh-productions.ch>
# Copyright (C) 2009 Masao Mutoh
#
# License: Ruby's or LGPL
#
@@ -56,21 +56,23 @@
attr_accessor :separator
attr_accessor :msgctxt
attr_accessor :references # ["file1:line1", "file2:line2", ...]
attr_accessor :translator_comment
attr_accessor :extracted_comment
- attr_accessor :flag
+ # @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 = []
- @flag = nil
+ @flags = []
@previous = nil
@msgctxt = nil
@msgid = nil
@msgid_plural = nil
@msgstr = nil
@@ -85,10 +87,28 @@
@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
@@ -97,11 +117,11 @@
separator == other.separator and
msgctxt == other.msgctxt and
translator_comment == other.translator_comment and
extracted_comment == other.extracted_comment and
references == other.references and
- flag == other.flag and
+ flags == other.flags and
previous == other.previous and
comment == other.comment
end
def type=(type)
@@ -175,10 +195,16 @@
# 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
+
def [](number)
param = @param_type[number]
raise ParseError, 'no more string parameters expected' unless param
send param
end
@@ -216,36 +242,43 @@
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 = fill_default_option_values(options)
+ @options = normalize_options(options)
end
def format
if @entry.obsolete?
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
+ str = format_comments
# msgctxt, msgid, msgstr
if @entry.msgctxt?
if @entry.msgctxt.nil?
no_msgctxt_message = "This POEntry is a kind of msgctxt " +
@@ -283,19 +316,70 @@
encode(str)
end
private
- def fill_default_option_values(options)
+ def normalize_options(options)
options = options.dup
- if options[:include_reference_comment].nil?
- options[:include_reference_comment] = true
+ 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 = ""
+ 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
@@ -324,11 +408,15 @@
end
formatted_reference
end
def format_flag_comment
- format_comment(FLAG_MARK, @entry.flag)
+ formatted_flags = ""
+ @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
@@ -363,14 +451,19 @@
end
formatted_comment
end
def format_message(message)
- return "\"\"\n" if message.nil?
+ empty_formatted_message = "\"\"\n"
+ return empty_formatted_message if message.nil?
chunks = wrap_message(message)
+ return empty_formatted_message if chunks.empty?
+
formatted_message = ""
- formatted_message << "\"\"\n" if chunks.size > 1
+ 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