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

Returns a new instance of 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)

Returns The string’s locale.

Returns:



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

def locale
  @locale
end

#offsetNumber (readonly)

If this string is a substring of another string, returns the starting offset of this string in the original.

Returns:

  • (Number)

    If this string is a substring of another string, returns the starting offset of this string in the original.



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

def offset
  @offset
end

#parentTranslatomatic::String (readonly)

Returns If this string is a substring of another string, returns the original string. Otherwise, returns nil.

Returns:

  • (Translatomatic::String)

    If this string is a substring of another string, returns the original string. Otherwise, returns nil.



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

def parent
  @parent
end

#valueString (readonly)

Returns The string.

Returns:



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

Returns:

  • (Boolean)


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

def empty?
  @value.empty?
end

#eql?(other) ⇒ Boolean

Returns:

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

Returns:



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

Returns true if this string is a substring of another string.

Returns:

  • (boolean)

    true if this string is a substring of another string



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

def substring?
  @parent ? true : false
end

#to_sString

Returns The value of the string.

Returns:

  • (String)

    The value of the string



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.

Returns:

  • (Symbol)

    The type of string, corresponding to TMX segtype.

See Also:



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