Class: Translatomatic::Text

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

Overview

A text string with an associated locale and other attributes

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new text

Parameters:

  • value (String)

    A string

  • locale (String)

    A locale



38
39
40
41
42
43
44
45
# File 'lib/translatomatic/text.rb', line 38

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/translatomatic/text.rb', line 249

def method_missing(name, *args)
  if @value.respond_to?(name)
    result = @value.send(name, *args)
    if result.is_a?(String)
      # convert to text object
      copy_self_with_value(result)
    else
      result
    end
  else
    super
  end
end

Class Attribute Details

.script_dataObject (readonly)

Returns the value of attribute script_data.



184
185
186
# File 'lib/translatomatic/text.rb', line 184

def script_data
  @script_data
end

Instance Attribute Details

#contextArray<String>

Returns Disambiguating context string(s).

Returns:

  • (Array<String>)

    Disambiguating context string(s)



19
20
21
# File 'lib/translatomatic/text.rb', line 19

def context
  @context
end

#localeTranslatomatic::Locale (readonly)

Returns The text locale.

Returns:



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

def locale
  @locale
end

#offsetNumber (readonly)

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

Returns:

  • (Number)

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



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

def offset
  @offset
end

#parentTranslatomatic::Text (readonly)

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

Returns:

  • (Translatomatic::Text)

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



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

def parent
  @parent
end

#preserve_regexRegexp

Returns Regexp that matches parts of the text to preserve.

Returns:

  • (Regexp)

    Regexp that matches parts of the text to preserve



22
23
24
# File 'lib/translatomatic/text.rb', line 22

def preserve_regex
  @preserve_regex
end

#valueString (readonly)

Returns The text content.

Returns:

  • (String)

    The text content



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

def value
  @value
end

Class Method Details

.[](value, locale) ⇒ Object

Create a new text. Returns value if value is already a

Translatomatic::Text object with the same locale.


26
27
28
29
30
31
32
33
# File 'lib/translatomatic/text.rb', line 26

def self.[](value, locale)
  locale = Translatomatic::Locale.parse(locale)
  if value.is_a?(Translatomatic::Text) && value.locale == locale
    value
  else
    new(value, locale)
  end
end

Instance Method Details

#==(other) ⇒ boolean

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

Returns:



119
120
121
# File 'lib/translatomatic/text.rb', line 119

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

#dupText

Returns A copy of this text.

Returns:

  • (Text)

    A copy of this text



48
49
50
# File 'lib/translatomatic/text.rb', line 48

def dup
  copy_self_with_value(value)
end

#eql?(other) ⇒ boolean

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

Returns:



113
114
115
116
# File 'lib/translatomatic/text.rb', line 113

def eql?(other)
  (other.is_a?(Translatomatic::Text) || other.is_a?(::String)) &&
    other.hash == hash
end

#escape(skip = '') ⇒ Translatomatic::Text

Escape unprintable characters such as newlines.

Returns:



132
133
134
# File 'lib/translatomatic/text.rb', line 132

def escape(skip = '')
  self.class.new(StringEscaping.escape(@value, skip), locale)
end

#gsub(pattern, replacement = nil) ⇒ Text

Returns A copy of this text with all occurrences of pattern substituted for the replacement text.

Returns:

  • (Text)

    A copy of this text with all occurrences of pattern substituted for the replacement text.



76
77
78
79
80
81
82
83
# File 'lib/translatomatic/text.rb', line 76

def gsub(pattern, replacement = nil)
  new_value = if block_given?
                @value.gsub(pattern) { yield Regexp.last_match }
              else
                @value.gsub(pattern, replacement)
              end
  copy_self_with_value(new_value)
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



55
56
57
# File 'lib/translatomatic/text.rb', line 55

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

#sentencesArray<Translatomatic::Text] List of sentences

Find all sentences in the text

Returns:



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

def sentences
  substrings(sentence_regex)
end

#substring?boolean

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

Returns:

  • (boolean)

    true if this text is a substring of another text



60
61
62
# File 'lib/translatomatic/text.rb', line 60

def substring?
  @parent ? true : false
end

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

Find all substrings matching the given regex

Returns:



104
105
106
107
108
109
# File 'lib/translatomatic/text.rb', line 104

def substrings(regex)
  matches = matches(@value, regex)
  strings = matches.collect { |i| match_to_substring(i) }.compact
  # 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 text.

Returns:

  • (String)

    The value of the text



65
66
67
# File 'lib/translatomatic/text.rb', line 65

def to_s
  @value
end

#to_strString

Returns value.to_str.

Returns:

  • (String)

    value.to_str



70
71
72
# File 'lib/translatomatic/text.rb', line 70

def to_str
  @value.to_str
end

#typeSymbol

Returns The type of text, corresponding to TMX segtype.

Returns:

  • (Symbol)

    The type of text, corresponding to TMX segtype.

See Also:



87
88
89
90
91
92
93
94
# File 'lib/translatomatic/text.rb', line 87

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

#unescapeTranslatomatic::Text

Unescape character escapes such as “n” to their character equivalents.

Returns:



139
140
141
# File 'lib/translatomatic/text.rb', line 139

def unescape
  self.class.new(StringEscaping.unescape(@value), locale)
end