Class: Translatomatic::String
- Inherits:
-
Object
- Object
- Translatomatic::String
- Defined in:
- lib/translatomatic/string.rb
Overview
A string object with an associated locale.
Class Attribute Summary collapse
-
.script_data ⇒ Object
readonly
Returns the value of attribute script_data.
Instance Attribute Summary collapse
-
#locale ⇒ Translatomatic::Locale
readonly
The string’s locale.
-
#offset ⇒ Number
readonly
If this string is a substring of another string, returns the starting offset of this string in the original.
-
#parent ⇒ Translatomatic::String
readonly
If this string is a substring of another string, returns the original string.
-
#value ⇒ String
readonly
The string.
Instance Method Summary collapse
-
#==(other) ⇒ boolean
True if other is a String with the same value and locale.
-
#empty? ⇒ boolean
True if the string is empty.
-
#eql?(other) ⇒ boolean
True if other is a String with the same value and locale.
-
#initialize(value, locale, options = {}) ⇒ String
constructor
A new instance of String.
-
#length ⇒ Number
The length of the string.
-
#match(pattern) ⇒ MatchData
Invokes value.match.
-
#sentences ⇒ Array<Translatomatic::String] List of sentences
Find all sentences in the string.
-
#substring? ⇒ boolean
True if this string is a substring of another string.
-
#substrings(regex) ⇒ Array<Translatomatic::String] List of substrings
Find all substrings matching the given regex.
-
#to_s ⇒ String
The value of the string.
-
#type ⇒ Symbol
The type of string, corresponding to TMX segtype.
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, = {}) @value = value.to_s || '' @locale = Translatomatic::Locale.parse(locale) @offset = [:offset] || 0 @parent = [:parent] end |
Class Attribute Details
.script_data ⇒ Object (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
#locale ⇒ Translatomatic::Locale (readonly)
Returns The string’s locale.
9 10 11 |
# File 'lib/translatomatic/string.rb', line 9 def locale @locale end |
#offset ⇒ Number (readonly)
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 |
#parent ⇒ Translatomatic::String (readonly)
Returns 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 |
#value ⇒ String (readonly)
Returns The string.
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.
98 99 100 |
# File 'lib/translatomatic/string.rb', line 98 def ==(other) eql?(other) end |
#empty? ⇒ boolean
Returns 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.
93 94 95 |
# File 'lib/translatomatic/string.rb', line 93 def eql?(other) other.kind_of?(Translatomatic::String) && other.hash == hash end |
#length ⇒ Number
Returns 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
44 45 46 |
# File 'lib/translatomatic/string.rb', line 44 def match(pattern) @value.match(pattern) end |
#sentences ⇒ Array<Translatomatic::String] List of sentences
Find all sentences in the string
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.
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
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_s ⇒ String
Returns The value of the string.
27 28 29 |
# File 'lib/translatomatic/string.rb', line 27 def to_s @value end |
#type ⇒ Symbol
Returns The type of string, corresponding to TMX segtype.
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 |