Class: Translatomatic::Text
- Inherits:
-
Object
- Object
- Translatomatic::Text
- Defined in:
- lib/translatomatic/text.rb
Overview
A text string with an associated locale and other attributes
Class Attribute Summary collapse
-
.script_data ⇒ Object
readonly
Returns the value of attribute script_data.
Instance Attribute Summary collapse
-
#context ⇒ Array<String>
Disambiguating context string(s).
-
#locale ⇒ Translatomatic::Locale
readonly
The text locale.
-
#offset ⇒ Number
readonly
If this text is a substring of another text, returns the starting offset of this text in the original.
-
#parent ⇒ Translatomatic::Text
readonly
If this text is a substring of another text, returns the original text.
-
#preserve_regex ⇒ Regexp
Regexp that matches parts of the text to preserve.
-
#value ⇒ String
readonly
The text content.
Class Method Summary collapse
-
.[](value, locale) ⇒ Object
Create a new text.
Instance Method Summary collapse
-
#==(other) ⇒ boolean
True if other is a Text with the same value and locale.
-
#dup ⇒ Text
A copy of this text.
-
#eql?(other) ⇒ boolean
True if other is a Text with the same value and locale.
-
#escape(skip = '') ⇒ Translatomatic::Text
Escape unprintable characters such as newlines.
-
#gsub(pattern, replacement = nil) ⇒ Text
A copy of this text with all occurrences of pattern substituted for the replacement text.
-
#initialize(value, locale, options = {}) ⇒ Text
constructor
Creates a new text.
-
#match(pattern) ⇒ MatchData
Invokes value.match.
-
#sentences ⇒ Array<Translatomatic::Text] List of sentences
Find all sentences in the text.
-
#substring? ⇒ boolean
True if this text is a substring of another text.
-
#substrings(regex) ⇒ Array<Translatomatic::Text] List of substrings
Find all substrings matching the given regex.
-
#to_s ⇒ String
The value of the text.
-
#to_str ⇒ String
Value.to_str.
-
#type ⇒ Symbol
The type of text, corresponding to TMX segtype.
-
#unescape ⇒ Translatomatic::Text
Unescape character escapes such as “n” to their character equivalents.
Constructor Details
#initialize(value, locale, options = {}) ⇒ Text
Creates a new text
38 39 40 41 42 43 44 45 |
# File 'lib/translatomatic/text.rb', line 38 def initialize(value, locale, = {}) @value = value.to_s || '' @locale = Translatomatic::Locale.parse(locale) @offset = [:offset] || 0 @parent = [:parent] @context = [:context] @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_data ⇒ Object (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
#context ⇒ Array<String>
Returns Disambiguating context string(s).
19 20 21 |
# File 'lib/translatomatic/text.rb', line 19 def context @context end |
#locale ⇒ Translatomatic::Locale (readonly)
Returns The text locale.
8 9 10 |
# File 'lib/translatomatic/text.rb', line 8 def locale @locale end |
#offset ⇒ Number (readonly)
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 |
#parent ⇒ Translatomatic::Text (readonly)
Returns 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_regex ⇒ Regexp
Returns 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 |
#value ⇒ String (readonly)
Returns 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.
119 120 121 |
# File 'lib/translatomatic/text.rb', line 119 def ==(other) eql?(other) end |
#dup ⇒ Text
Returns 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.
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.
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.
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
55 56 57 |
# File 'lib/translatomatic/text.rb', line 55 def match(pattern) @value.match(pattern) end |
#sentences ⇒ Array<Translatomatic::Text] List of sentences
Find all sentences in the text
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.
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
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_s ⇒ String
Returns The value of the text.
65 66 67 |
# File 'lib/translatomatic/text.rb', line 65 def to_s @value end |
#to_str ⇒ String
Returns value.to_str.
70 71 72 |
# File 'lib/translatomatic/text.rb', line 70 def to_str @value.to_str end |
#type ⇒ Symbol
Returns The type of text, corresponding to TMX segtype.
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 |
#unescape ⇒ Translatomatic::Text
Unescape character escapes such as “n” to their character equivalents.
139 140 141 |
# File 'lib/translatomatic/text.rb', line 139 def unescape self.class.new(StringEscaping.unescape(@value), locale) end |