Class: Translatomatic::String

Inherits:
Object
  • Object
show all
Defined in:
lib/translatomatic/string.rb

Defined Under Namespace

Classes: Script

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, locale, options = {}) ⇒ String



18
19
20
21
22
23
# File 'lib/translatomatic/string.rb', line 18

def initialize(value, locale, options = {})
  @value = value || ''
  @locale = Translatomatic::Locale.parse(locale)
  @offset = options[:offset] || 0
  @parent = options[:parent]
end

Class Attribute Details

.script_dataObject (readonly)

Returns the value of attribute script_data.



131
132
133
# File 'lib/translatomatic/string.rb', line 131

def script_data
  @script_data
end

Instance Attribute Details

#localeTranslatomatic::Locale (readonly)



8
9
10
# File 'lib/translatomatic/string.rb', line 8

def locale
  @locale
end

#offsetNumber (readonly)



16
17
18
# File 'lib/translatomatic/string.rb', line 16

def offset
  @offset
end

#parentTranslatomatic::String (readonly)



12
13
14
# File 'lib/translatomatic/string.rb', line 12

def parent
  @parent
end

#valueString (readonly)



5
6
7
# File 'lib/translatomatic/string.rb', line 5

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
# File 'lib/translatomatic/string.rb', line 83

def ==(other)
  eql?(other)
end

#empty?Boolean



34
35
36
# File 'lib/translatomatic/string.rb', line 34

def empty?
  @value.empty?
end

#eql?(other) ⇒ Boolean



79
80
81
# File 'lib/translatomatic/string.rb', line 79

def eql?(other)
  other.kind_of?(Translatomatic::String) && other.hash == hash
end

#hashObject



87
88
89
# File 'lib/translatomatic/string.rb', line 87

def hash
  [value, locale].hash
end

#lengthObject



30
31
32
# File 'lib/translatomatic/string.rb', line 30

def length
  @value.length
end

#match(regex) ⇒ Object



38
39
40
# File 'lib/translatomatic/string.rb', line 38

def match(regex)
  @value.match(regex)
end

#sentencesArray<Translatomatic::String] List of sentences

Find all sentences in the string



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/translatomatic/string.rb', line 60

def sentences
  sentences = @value.scan(sentence_regex)
  strings = []
  offset = 0
  sentences.each do |sentence|
    # find leading and trailing whitespace

    next if sentence.length == 0

    parts = sentence.match(/^(\s*)(.*?)(\s*)$/).to_a
    value = parts[2]
    offset += parts[1].length  # leading whitespace

    strings << self.class.new(value, locale, offset: offset, parent: self)
    offset += value.length + parts[3].length
  end

  # return [self] if there's only one sentence and it's equal to self

  strings.length == 1 && strings[0].eql?(self) ? [self] : strings
end

#substring?boolean



43
44
45
# File 'lib/translatomatic/string.rb', line 43

def substring?
  @parent ? true : false
end

#to_sString



26
27
28
# File 'lib/translatomatic/string.rb', line 26

def to_s
  @value
end

#typeSymbol

Returns The type of string, corresponding to TMX segtype.



49
50
51
52
53
54
55
56
# File 'lib/translatomatic/string.rb', line 49

def type
  if sentences.length >= 2
    :paragraph
  else
    script = script_data
    @value.strip.match(/#{script.delimiter}\s*$/) ? :sentence : :phrase
  end
end