class Translatomatic::Text
A text string with an associated locale and other attributes
Constants
- SCRIPT_DATA
Attributes
@return [Array<String>] Disambiguating context string(s)
@return [Translatomatic::Locale] The text locale
@return [Number] If this text is a substring of another text,
returns the starting offset of this text in the original.
@return [Translatomatic::Text] If this text is a substring of
another text, returns the original text. Otherwise, returns nil.
@return [Regexp] Regexp that matches parts of the text to preserve
@return [String] The text content
Public Class Methods
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
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
(see eql?
)
# File lib/translatomatic/text.rb, line 119 def ==(other) eql?(other) end
@return [Text] A copy of this text
# File lib/translatomatic/text.rb, line 48 def dup copy_self_with_value(value) end
@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 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
@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
@!visibility private
# File lib/translatomatic/text.rb, line 124 def hash value.hash # [value, locale].hash end
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
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
@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
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
@return [String] The value of the text
# File lib/translatomatic/text.rb, line 65 def to_s @value end
@return [String] value.to_str
# File lib/translatomatic/text.rb, line 70 def to_str @value.to_str end
@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 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
# 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
# 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
# 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
# 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
# File lib/translatomatic/text.rb, line 245 def respond_to_missing?(name, include_private = false) @value.respond_to?(name) || super end
# File lib/translatomatic/text.rb, line 240 def script_data data = self.class.script_data data[locale.language] || data['default'] end
# 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