Class: GetText::POEntry

Inherits:
Object
  • Object
show all
Includes:
POEntryForRubyParser
Defined in:
lib/gettext/po_entry.rb,
lib/gettext/tools/parser/ruby.rb

Overview

Contains data related to the expression or sentence that is to be translated.

Defined Under Namespace

Classes: Formatter, InvalidTypeError, NoMsgctxtError, NoMsgidError, NoMsgidPluralError

Constant Summary

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from POEntryForRubyParser

#advance_to_next_attribute, #init_param, #set_current_attribute

Constructor Details

#initialize(type) ⇒ POEntry

Create the object. +type+ should be :normal, :plural, :msgctxt or :msgctxt_plural.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gettext/po_entry.rb', line 68

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

Instance Attribute Details

#commentObject

Returns the value of attribute comment



65
66
67
# File 'lib/gettext/po_entry.rb', line 65

def comment
  @comment
end

#extracted_commentObject

Returns the value of attribute extracted_comment



60
61
62
# File 'lib/gettext/po_entry.rb', line 60

def extracted_comment
  @extracted_comment
end

#flagsArray<String>

Returns The flags for this PO entry.

Returns:

  • (Array<String>)

    The flags for this PO entry.

Since:

  • 3.0.4



63
64
65
# File 'lib/gettext/po_entry.rb', line 63

def flags
  @flags
end

#msgctxtObject

Returns the value of attribute msgctxt



57
58
59
# File 'lib/gettext/po_entry.rb', line 57

def msgctxt
  @msgctxt
end

#msgidObject

Returns the value of attribute msgid



52
53
54
# File 'lib/gettext/po_entry.rb', line 52

def msgid
  @msgid
end

#msgid_pluralObject

Options



55
56
57
# File 'lib/gettext/po_entry.rb', line 55

def msgid_plural
  @msgid_plural
end

#msgstrObject

Returns the value of attribute msgstr



53
54
55
# File 'lib/gettext/po_entry.rb', line 53

def msgstr
  @msgstr
end

#previousObject

Returns the value of attribute previous



64
65
66
# File 'lib/gettext/po_entry.rb', line 64

def previous
  @previous
end

#referencesObject

["file1:line1", "file2:line2", ...]



58
59
60
# File 'lib/gettext/po_entry.rb', line 58

def references
  @references
end

#separatorObject

Returns the value of attribute separator



56
57
58
# File 'lib/gettext/po_entry.rb', line 56

def separator
  @separator
end

#translator_commentObject

Returns the value of attribute translator_comment



59
60
61
# File 'lib/gettext/po_entry.rb', line 59

def translator_comment
  @translator_comment
end

#typeObject

Required



51
52
53
# File 'lib/gettext/po_entry.rb', line 51

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

Checks if the self has same attributes as other.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gettext/po_entry.rb', line 111

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

#[](number) ⇒ Object

Raises:



213
214
215
216
217
# File 'lib/gettext/po_entry.rb', line 213

def [](number)
  param = @param_type[number]
  raise ParseError, 'no more string parameters expected' unless param
  send param
end

#add_comment(new_comment) ⇒ void

This method returns an undefined value.

Support for extracted comments. Explanation s. http://www.gnu.org/software/gettext/manual/gettext.html#Names



84
85
86
87
88
89
90
# File 'lib/gettext/po_entry.rb', line 84

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

#flagString?

Deprecated.

Since 3.0.4. Use #flags instead.

Returns The flag of the PO entry.

Returns:

  • (String, nil)

    The flag of the PO entry.



94
95
96
# File 'lib/gettext/po_entry.rb', line 94

def flag
  @flags.first
end

#flag=(flag) ⇒ Object

Deprecated.

Since 3.0.4. Use #flags= instead.

Set the new flag for the PO entry.

Parameters:

  • flag (String, nil)

    The new flag.



102
103
104
105
106
107
108
# File 'lib/gettext/po_entry.rb', line 102

def flag=(flag)
  if flag.nil?
    @flags = []
  else
    @flags = [flag]
  end
end

#fuzzy?Boolean

Returns true if the entry is fuzzy entry, false otherwise. Fuzzy entry has "fuzzy" flag.

Returns:

  • (Boolean)

    true if the entry is fuzzy entry, false otherwise. Fuzzy entry has "fuzzy" flag.



202
203
204
# File 'lib/gettext/po_entry.rb', line 202

def fuzzy?
  @flags.include?("fuzzy")
end

#header?Boolean

Returns true if the entry is header entry, false otherwise. Header entry is normal type and has empty msgid.

Returns:

  • (Boolean)

    true if the entry is header entry, false otherwise. Header entry is normal type and has empty msgid.



190
191
192
# File 'lib/gettext/po_entry.rb', line 190

def header?
  @type == :normal and @msgid == ""
end

#initialize_oldPOEntry

Create the object. +type+ should be :normal, :plural, :msgctxt or :msgctxt_plural.

Returns:

  • (POEntry)

    a new instance of POEntry



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gettext/tools/parser/ruby.rb', line 104

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

#merge(other) ⇒ Object

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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gettext/po_entry.rb', line 144

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

#mergeable?(other) ⇒ Boolean

Checks if the other translation target is mergeable with the current one. Relevant are msgid and translation context (msgctxt).

Returns:

  • (Boolean)


137
138
139
# File 'lib/gettext/po_entry.rb', line 137

def mergeable?(other)
  other && other.msgid == self.msgid && other.msgctxt == self.msgctxt
end

#msgctxt?Boolean

Returns true if the type is kind of msgctxt.

Returns:

  • (Boolean)


179
180
181
# File 'lib/gettext/po_entry.rb', line 179

def msgctxt?
  [:msgctxt, :msgctxt_plural].include?(@type)
end

#obsolete?Boolean

Returns true if the entry is obsolete entry, false otherwise. Obsolete entry is normal type and has :last msgid.

Returns:

  • (Boolean)

    true if the entry is obsolete entry, false otherwise. Obsolete entry is normal type and has :last msgid.



196
197
198
# File 'lib/gettext/po_entry.rb', line 196

def obsolete?
  @type == :normal and @msgid == :last
end

#plural?Boolean

Returns true if the type is kind of plural.

Returns:

  • (Boolean)


184
185
186
# File 'lib/gettext/po_entry.rb', line 184

def plural?
  [:plural, :msgctxt_plural].include?(@type)
end

#to_s(options = {}) ⇒ Object

Format the po entry in PO format.

Parameters:

  • 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.

  • :encoding (Encoding) — default: nil

    Encodes to the specific encoding.

Raises:



171
172
173
174
175
176
# File 'lib/gettext/po_entry.rb', line 171

def to_s(options={})
  raise(NoMsgidError, "msgid is nil.") unless @msgid

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

#translated?Boolean

Returns true if the entry is translated entry, false otherwise.

Returns:

  • (Boolean)

    true if the entry is translated entry, false otherwise.



207
208
209
210
211
# File 'lib/gettext/po_entry.rb', line 207

def translated?
  return false if fuzzy?
  return false if @msgstr.nil? or @msgstr.empty?
  true
end