Class: Locca::TranslateAction

Inherits:
Object
  • Object
show all
Defined in:
lib/locca/actions/translate_action.rb

Instance Method Summary collapse

Constructor Details

#initialize(project, lang, collection_builder, collection_writer, collection_merger) ⇒ TranslateAction

Returns a new instance of TranslateAction.



30
31
32
33
34
35
36
# File 'lib/locca/actions/translate_action.rb', line 30

def initialize(project, lang, collection_builder, collection_writer, collection_merger)
    @project = project
    @lang = lang
    @collection_merger = collection_merger
    @collection_builder = collection_builder
    @collection_writer = collection_writer
end

Instance Method Details

#executeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/locca/actions/translate_action.rb', line 38

def execute()
	editor = ENV['EDITOR']
			if !editor
				raise ArgumentError, 'EDITOR variable should be defined'
			end

			@project.collection_names().each do |collection_name|
				collection_path = @project.path_for_collection(collection_name, @lang)
        collection = @collection_builder.collection_at_path(collection_path)

				tmpCollection = Collection.new()

				collection.sorted_each do |item|
            if item.translated?
						next
					end

					tmpCollection.add_item(item)
        end

        if tmpCollection.count == 0
        	next
        end

        tmpFile = Tempfile.new("#{collection_name}.#{@lang}.tmp")

        @collection_writer.write_to_path(tmpCollection, tmpFile.path)
        command = "#{editor} #{tmpFile.path}"
				stdout,stderr,status = Open3.capture3(command)
				if status.success?
					translated_collection = @collection_builder.collection_at_path(tmpFile.path)
					@collection_merger.merge(translated_collection, collection, (CollectionMerger::ACTION_UPDATE))
            @collection_writer.write_to_path(collection, collection_path)
				end
				tmpFile.close
				tmpFile.unlink
			end
end