class Translatomatic::Text

A text string with an associated locale and other attributes

Constants

SCRIPT_DATA

Attributes

script_data[R]
context[RW]

@return [Array<String>] Disambiguating context string(s)

locale[R]

@return [Translatomatic::Locale] The text locale

offset[R]

@return [Number] If this text is a substring of another text,

returns the starting offset of this text in the original.
parent[R]

@return [Translatomatic::Text] If this text is a substring of

another text, returns the original text. Otherwise, returns nil.
preserve_regex[RW]

@return [Regexp] Regexp that matches parts of the text to preserve

value[R]

@return [String] The text content

Public Class Methods

[](value, locale) click to toggle source

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

Translatomatic::Text object with the same locale.
# 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
new(value, locale, options = {}) click to toggle source

Creates a new text @param value [String] A string @param locale [String] A locale

# 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

Public Instance Methods

==(other) click to toggle source

(see eql?)

# File lib/translatomatic/text.rb, line 119
def ==(other)
  eql?(other)
end
dup() click to toggle source

@return [Text] A copy of this text

# File lib/translatomatic/text.rb, line 48
def dup
  copy_self_with_value(value)
end
eql?(other) click to toggle source

@return [boolean] true if other is a {Translatomatic::Text} with

the same value and locale.
# 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 = '') click to toggle source

Escape unprintable characters such as newlines. @return [Translatomatic::Text] The text with

special characters escaped.
# File lib/translatomatic/text.rb, line 132
def escape(skip = '')
  self.class.new(StringEscaping.escape(@value, skip), locale)
end
gsub(pattern, replacement = nil) { |last_match| ... } click to toggle source

@return [Text] A copy of this text with all occurrences of pattern

substituted for the replacement text.
# 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
hash() click to toggle source

@!visibility private

# File lib/translatomatic/text.rb, line 124
def hash
  value.hash
  # [value, locale].hash
end
match(pattern) click to toggle source

Invokes value.match @param pattern [Regexp,String] The regex pattern to match @return [MatchData] Object describing the match, or nil if no match

# File lib/translatomatic/text.rb, line 55
def match(pattern)
  @value.match(pattern)
end
sentences() click to toggle source

Find all sentences in the text @return [Array<Translatomatic::Text] List of sentences

# File lib/translatomatic/text.rb, line 98
def sentences
  substrings(sentence_regex)
end
substring?() click to toggle source

@return [boolean] true if this text is a substring of another text

# File lib/translatomatic/text.rb, line 60
def substring?
  @parent ? true : false
end
substrings(regex) click to toggle source

Find all substrings matching the given regex @return [Array<Translatomatic::Text] List of substrings

# 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_s() click to toggle source

@return [String] The value of the text

# File lib/translatomatic/text.rb, line 65
def to_s
  @value
end
to_str() click to toggle source

@return [String] value.to_str

# File lib/translatomatic/text.rb, line 70
def to_str
  @value.to_str
end
type() click to toggle source

@return [Symbol] The type of text, corresponding to TMX segtype. @see xml.coverpages.org/tmxSpec971212.html#SEGTYPE

# 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
unescape() click to toggle source

Unescape character escapes such as ā€œnā€ to their character equivalents. @return [Translatomatic::Text] The text with

escaped characters replaced with actual characters.
# File lib/translatomatic/text.rb, line 139
def unescape
  self.class.new(StringEscaping.unescape(@value), locale)
end

Private Instance Methods

copy_self_with_value(new_value) click to toggle source
# File lib/translatomatic/text.rb, line 197
def copy_self_with_value(new_value)
  copy = self.class.new(new_value, @locale, @options)
  copy.preserve_regex = preserve_regex
  copy.context = context
  copy
end
match_to_substring(match) click to toggle source
# File lib/translatomatic/text.rb, line 204
def match_to_substring(match)
  substring = match.to_s
  return nil if substring.empty?

  # find leading and trailing whitespace
  parts = substring.match(/\A(\s*)(.*?)(\s*)\z/m).to_a
  value = parts[2]
  offset = match.offset(0)[0]
  offset += parts[1].length # leading whitespace
  string = self.class.new(value, locale, offset: offset, parent: self)
  string.preserve_regex = preserve_regex
  string.context = context
  string
end
matches(s, re) click to toggle source
# File lib/translatomatic/text.rb, line 219
def matches(s, re)
  start_at = 0
  matches = []
  while (m = s.match(re, start_at))
    break if m.to_s.empty?
    matches.push(m)
    start_at = m.end(0)
  end
  matches
end
method_missing(name, *args) click to toggle source
Calls superclass method
# 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
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/translatomatic/text.rb, line 245
def respond_to_missing?(name, include_private = false)
  @value.respond_to?(name) || super
end
script_data() click to toggle source
# File lib/translatomatic/text.rb, line 240
def script_data
  data = self.class.script_data
  data[locale.language] || data['default']
end
sentence_regex() click to toggle source
# File lib/translatomatic/text.rb, line 230
def sentence_regex
  script = script_data
  if script.trailing_space
    /.*?(?:#{script.delimiter}\s+|\z|\n)/m
  else
    # no trailing space after delimiter
    /.*?(?:#{script.delimiter}|\z|\n)/m
  end
end