Class: GetText::TranslationTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/translation_target.rb

Overview

Contains data related to the expression or sentence that is to be translated (translation target). Implements a sort of state machine to assist the parser.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_type) ⇒ TranslationTarget

Returns a new instance of TranslationTarget.



12
13
14
15
16
# File 'lib/gettext/translation_target.rb', line 12

def initialize(new_type)
  @type = new_type
  @occurrences = Array.new
  @param_number = 0
end

Instance Attribute Details

#extracted_commentObject

optional attributes



10
11
12
# File 'lib/gettext/translation_target.rb', line 10

def extracted_comment
  @extracted_comment
end

#msgctxtObject

optional attributes



10
11
12
# File 'lib/gettext/translation_target.rb', line 10

def msgctxt
  @msgctxt
end

#msgidObject

obligatory attributes



9
10
11
# File 'lib/gettext/translation_target.rb', line 9

def msgid
  @msgid
end

#occurrencesObject

obligatory attributes



9
10
11
# File 'lib/gettext/translation_target.rb', line 9

def occurrences
  @occurrences
end

#pluralObject

optional attributes



10
11
12
# File 'lib/gettext/translation_target.rb', line 10

def plural
  @plural
end

#typeObject

obligatory attributes



9
10
11
# File 'lib/gettext/translation_target.rb', line 9

def type
  @type
end

Instance Method Details

#add_extracted_comment(new_comment) ⇒ Object

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



47
48
49
50
# File 'lib/gettext/translation_target.rb', line 47

def add_extracted_comment(new_comment)
  @extracted_comment = @extracted_comment.to_s + new_comment
  to_s
end

#advance_to_next_attributeObject



41
42
43
# File 'lib/gettext/translation_target.rb', line 41

def advance_to_next_attribute
  @param_number += 1
end

#escaped(param_name) ⇒ Object

Returns a parameter representation suitable for po-files and other purposes.



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

def escaped(param_name)
  orig = self.send param_name
  orig.gsub(/"/, '\"').gsub(/\r/, '')
end

#matches?(other) ⇒ Boolean

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/gettext/translation_target.rb', line 61

def matches?(other)
  other.msgid == self.msgid && other.msgctxt == self.msgctxt
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.

Raises:



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

def merge(other)
  return self if other.nil?
  raise ParseError, "Translation targets do not match: \n" \
  "  self: #{self.inspect}\n  other: '#{other.inspect}'" unless matches?(other)
  if other.plural && !self.plural
    res = other
    res.occurrences.concat self.occurrences
  else
    res = self
    res.occurrences.concat other.occurrences
  end
  res
end

#set_current_attribute(str) ⇒ Object

Supports parsing by setting attributes by and by.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gettext/translation_target.rb', line 19

def set_current_attribute(str)
  case @param_number 
  when 0
    set_string_value :msgid, str
  when 1
    case type
    when :plural
      set_string_value :plural, str
    when :msgctxt, :msgctxt_plural
      set_string_value :msgctxt, str
    else
      raise ParseError, 'no more string parameters expected'
    end
  when 2
    if :msgctxt_plural
      set_string_value plural, str
    else
      raise ParseError, 'no more string parameters expected'
    end
  end
end