Class: Translatomatic::String

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

Overview

A string object with an associated locale.

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.



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

def initialize(value, locale, options = {})
  @value = value.to_s || ''
  @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.



148
149
150
# File 'lib/translatomatic/string.rb', line 148

def script_data
  @script_data
end

Instance Attribute Details

#localeTranslatomatic::Locale (readonly)

Returns The string’s locale.

Returns:



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

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.



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

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.



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

def parent
  @parent
end

#valueString (readonly)

Returns The string.

Returns:



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ boolean

Returns true if other is a Translatomatic::String with the same value and locale.

Returns:



98
99
100
# File 'lib/translatomatic/string.rb', line 98

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

#empty?boolean

Returns True if the string is empty.

Returns:

  • (boolean)

    True if the string is empty



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

def empty?
  @value.empty?
end

#eql?(other) ⇒ boolean

Returns true if other is a Translatomatic::String with the same value and locale.

Returns:



93
94
95
# File 'lib/translatomatic/string.rb', line 93

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

#lengthNumber

Returns The length of the string.

Returns:

  • (Number)

    The length of the string



32
33
34
# File 'lib/translatomatic/string.rb', line 32

def length
  @value.length
end

#match(pattern) ⇒ MatchData

Invokes value.match

Parameters:

  • pattern (Regexp, String)

    The regex pattern to match

Returns:

  • (MatchData)

    Object describing the match, or nil if no match



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

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

#sentencesArray<Translatomatic::String] List of sentences

Find all sentences in the string

Returns:



66
67
68
# File 'lib/translatomatic/string.rb', line 66

def sentences
  substrings(sentence_regex)
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



49
50
51
# File 'lib/translatomatic/string.rb', line 49

def substring?
  @parent ? true : false
end

#substrings(regex) ⇒ Array<Translatomatic::String] List of substrings

Find all substrings matching the given regex

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/translatomatic/string.rb', line 72

def substrings(regex)
  matches = matches(@value, regex)
  strings = []
  matches.each do |match|
    substring = match.to_s
    # find leading and trailing whitespace
    next if substring.length == 0

    parts = substring.match(/\A(\s*)(.*?)(\s*)\z/m).to_a
    value = parts[2]
    offset = match.offset(0)[0]
    offset += parts[1].length  # leading whitespace
    strings << self.class.new(value, locale, offset: offset, parent: self)
  end

  # return [self] if there's only one substring and it's equal to self
  strings.length == 1 && strings[0].eql?(self) ? [self] : strings
end

#to_sString

Returns The value of the string.

Returns:

  • (String)

    The value of the string



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

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:



55
56
57
58
59
60
61
62
# File 'lib/translatomatic/string.rb', line 55

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