Class: Translatomatic::String
- Inherits:
-
Object
- Object
- Translatomatic::String
- Defined in:
- lib/translatomatic/string.rb
Defined Under Namespace
Classes: Script
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) ⇒ Object
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(value, locale, options = {}) ⇒ String
constructor
A new instance of String.
- #length ⇒ Object
- #match(regex) ⇒ Object
-
#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.
-
#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
18 19 20 21 22 23 |
# File 'lib/translatomatic/string.rb', line 18 def initialize(value, locale, = {}) @value = value || '' @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.
131 132 133 |
# File 'lib/translatomatic/string.rb', line 131 def script_data @script_data end |
Instance Attribute Details
#locale ⇒ Translatomatic::Locale (readonly)
8 9 10 |
# File 'lib/translatomatic/string.rb', line 8 def locale @locale end |
#offset ⇒ Number (readonly)
16 17 18 |
# File 'lib/translatomatic/string.rb', line 16 def offset @offset end |
#parent ⇒ Translatomatic::String (readonly)
12 13 14 |
# File 'lib/translatomatic/string.rb', line 12 def parent @parent end |
#value ⇒ String (readonly)
5 6 7 |
# File 'lib/translatomatic/string.rb', line 5 def value @value end |
Instance Method Details
#==(other) ⇒ Object
83 84 85 |
# File 'lib/translatomatic/string.rb', line 83 def ==(other) eql?(other) end |
#empty? ⇒ Boolean
34 35 36 |
# File 'lib/translatomatic/string.rb', line 34 def empty? @value.empty? end |
#eql?(other) ⇒ Boolean
79 80 81 |
# File 'lib/translatomatic/string.rb', line 79 def eql?(other) other.kind_of?(Translatomatic::String) && other.hash == hash end |
#hash ⇒ Object
87 88 89 |
# File 'lib/translatomatic/string.rb', line 87 def hash [value, locale].hash end |
#length ⇒ Object
30 31 32 |
# File 'lib/translatomatic/string.rb', line 30 def length @value.length end |
#match(regex) ⇒ Object
38 39 40 |
# File 'lib/translatomatic/string.rb', line 38 def match(regex) @value.match(regex) end |
#sentences ⇒ Array<Translatomatic::String] List of sentences
Find all sentences in the string
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/translatomatic/string.rb', line 60 def sentences sentences = @value.scan(sentence_regex) strings = [] offset = 0 sentences.each do |sentence| # find leading and trailing whitespace next if sentence.length == 0 parts = sentence.match(/^(\s*)(.*?)(\s*)$/).to_a value = parts[2] offset += parts[1].length # leading whitespace strings << self.class.new(value, locale, offset: offset, parent: self) offset += value.length + parts[3].length end # return [self] if there's only one sentence and it's equal to self strings.length == 1 && strings[0].eql?(self) ? [self] : strings end |
#substring? ⇒ boolean
43 44 45 |
# File 'lib/translatomatic/string.rb', line 43 def substring? @parent ? true : false end |
#to_s ⇒ String
26 27 28 |
# File 'lib/translatomatic/string.rb', line 26 def to_s @value end |
#type ⇒ Symbol
Returns The type of string, corresponding to TMX segtype.
49 50 51 52 53 54 55 56 |
# File 'lib/translatomatic/string.rb', line 49 def type if sentences.length >= 2 :paragraph else script = script_data @value.strip.match(/#{script.delimiter}\s*$/) ? :sentence : :phrase end end |