class Translatomatic::TextCollection

A collection of texts

Attributes

originals[R]

Public Class Methods

new(texts = []) click to toggle source
# File lib/translatomatic/text_collection.rb, line 6
def initialize(texts = [])
  texts = [texts] unless texts.is_a?(Array)
  texts = texts.select { |i| translatable?(i) }
  texts = textify(texts) # convert to Text objects
  @originals = texts
  @sentences = find_sentences(texts) # convert to sentences
  contexts = find_contexts(texts)
  @all_texts = @sentences + contexts
  group_by_locale(@all_texts)
end

Public Instance Methods

count() click to toggle source

@return [Number] The total number of texts in the collection,

equal to the number of sentences and context strings.
# File lib/translatomatic/text_collection.rb, line 26
def count
  @all_texts.length
end
each_locale() { |locale, list| ... } click to toggle source

Iterate over texts in the collection grouped by locale

# File lib/translatomatic/text_collection.rb, line 18
def each_locale
  @by_locale.each do |locale, list|
    yield locale, list
  end
end

Private Instance Methods

build_text(value, locale = Locale.default) click to toggle source
# File lib/translatomatic/text_collection.rb, line 57
def build_text(value, locale = Locale.default)
  return nil if value.nil?
  if value.is_a?(Translatomatic::Text)
    value
  else
    Translatomatic::Text.new(value, locale)
  end
end
find_contexts(texts) click to toggle source
# File lib/translatomatic/text_collection.rb, line 45
def find_contexts(texts)
  texts.collect { |i| build_text(i.context) }.flatten.uniq.compact
end
find_sentences(texts) click to toggle source
# File lib/translatomatic/text_collection.rb, line 41
def find_sentences(texts)
  texts.collect(&:sentences).flatten.uniq
end
group_by_locale(texts) click to toggle source
# File lib/translatomatic/text_collection.rb, line 49
def group_by_locale(texts)
  @by_locale = {}
  texts.each do |text|
    list = @by_locale[text.locale] ||= []
    list << text
  end
end
textify(texts) click to toggle source
# File lib/translatomatic/text_collection.rb, line 37
def textify(texts)
  texts.collect { |i| build_text(i) }
end
translatable?(text) click to toggle source
# File lib/translatomatic/text_collection.rb, line 32
def translatable?(text)
  # don't translate numbers
  text && !text.match(/\A\s*\z/) && !text.match(/\A[\d,]+\z/)
end