Class: RailsTextExtractor

Inherits:
BaseTextExtractor show all
Defined in:
lib/core/rails_text_extractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseTextExtractor

#html_text

Constructor Details

#initialize(translation_store = RailsTranslationStore.new) ⇒ RailsTextExtractor

Returns a new instance of RailsTextExtractor.



4
5
6
7
8
9
10
# File 'lib/core/rails_text_extractor.rb', line 4

def initialize( translation_store = RailsTranslationStore.new )
  @key_store = []
  @translation_store = translation_store
  @current_text = []
  @current_nodes = []
  @current_variables = []
end

Instance Attribute Details

#translation_storeObject (readonly)

Returns the value of attribute translation_store.



2
3
4
# File 'lib/core/rails_text_extractor.rb', line 2

def translation_store
  @translation_store
end

Instance Method Details

#add_html_text(text_node) ⇒ Object

This takes in a text node and returns one or more nodes that will then be output. The nodes that are output should implement node_name and text_value



75
76
77
# File 'lib/core/rails_text_extractor.rb', line 75

def add_html_text( text_node )
  @current_text << text_node
end

#add_non_text(non_text_node) ⇒ Object



80
81
82
# File 'lib/core/rails_text_extractor.rb', line 80

def add_non_text( non_text_node )
  @current_text << non_text_node
end

#add_variable(variable_name, variable_value) ⇒ Object

This is used to add a new variable into the current html element



53
54
55
56
57
# File 'lib/core/rails_text_extractor.rb', line 53

def add_variable( variable_name, variable_value)
  variable_node = RailsTextVariableNode.new( variable_name, variable_value )
  @current_text << variable_node
  @current_variables << variable_node
end

#completed_text_extractionObject

This is called when text extraction has finished



85
86
# File 'lib/core/rails_text_extractor.rb', line 85

def completed_text_extraction
end

#end_html_textObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/core/rails_text_extractor.rb', line 22

def end_html_text
  whitespace, @current_text = strip_ending_whitespace_nodes( @current_text )

  unless( @current_text.empty? && @current_variables.empty? )
    total_text = @current_text.inject("") { |all_text, node| all_text + node.text_value }
    @current_nodes << HerbErbTextCallNode.new( [total_text], @key_store, "t '.", "'", @current_variables )
    @translation_store.add_translation( @current_nodes.last.key_value, @current_nodes.last.original_text )
  end
  
  # Just reset everything here to be cautious
  to_return = @current_nodes
  @current_nodes = []
  @current_text = []
  
  return to_return + whitespace
end

#pluralize(pluralize_node, variable_name) ⇒ Object



39
40
41
42
# File 'lib/core/rails_text_extractor.rb', line 39

def pluralize( pluralize_node, variable_name )
  add_variable( variable_name,
                pluralize_node.text_value )
end

#start_html_textObject



16
17
18
19
20
# File 'lib/core/rails_text_extractor.rb', line 16

def start_html_text
  @current_text = []
  @current_nodes = []
  @current_variables = []
end

#starting_text_extractionObject

This is called when text extraction has begun



13
14
# File 'lib/core/rails_text_extractor.rb', line 13

def starting_text_extraction
end

#translate_method_call_text(method_to_translate) ⇒ Object



67
68
69
# File 'lib/core/rails_text_extractor.rb', line 67

def translate_method_call_text( method_to_translate )
  translate_text( method_to_translate )
end

#translate_text(text_to_translate) ⇒ Object

This is called to just produce a translated text node without erb braces around it.



61
62
63
64
65
# File 'lib/core/rails_text_extractor.rb', line 61

def translate_text( text_to_translate )
  call_node = HerbErbTextCallNode.new( [text_to_translate], @key_store, "t '.", "'", [], false )
  @translation_store.add_translation( call_node.key_value, call_node.original_text )
  call_node
end

#white_space(node) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/core/rails_text_extractor.rb', line 44

def white_space( node )
  if( @current_text.empty? )
    @current_nodes << node
  else
    @current_text << node
  end
end