Class: NuixTranslator

Inherits:
Object
  • Object
show all
Defined in:
Ruby/translation.nuixscript/language_translation.rb

Overview

Base class for translating items.

Constant Summary collapse

SCRIPT_DIR =
File.join(File.dirname(__FILE__), 'Translators')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, languages) ⇒ NuixTranslator

Creates a new Translator.

Parameters:

  • name (String)

    NuixTranslator subclass name

  • languages (Hash)

    { 'en' => 'english', … }



35
36
37
38
39
40
41
42
43
# File 'Ruby/translation.nuixscript/language_translation.rb', line 35

def initialize(name, languages)
  settings_file = File.join(SCRIPT_DIR, "#{name}.json")
  @options = ['Append Text', 'Add Custom Metadata']
  @langs = languages
  @items = []
  @input = TabbedCustomDialog.new('Language Translation')
  @input.enableStickySettings(settings_file)
  @main_tab = @input.addTab('main_tab', name)
end

Class Method Details

.translatorsHash

The NuixTranslator subclasses.

Returns:

  • (Hash)

    Name' => NuixTranslator subclass



21
22
23
24
25
26
27
28
29
# File 'Ruby/translation.nuixscript/language_translation.rb', line 21

def self.translators
  subs = Dir.glob(File.join(SCRIPT_DIR, '*.rb'))
  subs.each { |f| require f }
  translators = {}
  ObjectSpace.each_object(Class).select { |k| k < self }.each do |c|
    translators[c.name] = c
  end
  translators
end

Instance Method Details

#add_translationObject (protected)

Adds translation options to main tab of dialog.



48
49
50
51
# File 'Ruby/translation.nuixscript/language_translation.rb', line 48

def add_translation
  @main_tab.appendComboBox('translation_language', 'Language', @langs.values)
  @main_tab.appendComboBox('translation_annotate', 'Operation', @options)
end

#advance(index, status) ⇒ nil, true (protected)

Advances progress.

Parameters:

  • index (Integer)

    index of item being processed

  • status (String)

    message for status/log

Returns:

  • (nil, true)

    nil if abort was requested, true otherwise



58
59
60
61
62
63
64
65
66
67
68
# File 'Ruby/translation.nuixscript/language_translation.rb', line 58

def advance(index, status)
  if @progress.abortWasRequested
    @progress.logMessage('Aborting...')
    return nil
  end

  @progress.setMainProgress(index)
  frac = "#{index}/#{@items.size}"
  @progress.setSubStatusAndLogIt("(#{frac}) #{status}")
  true
end

#get_new_text(original, translated) ⇒ String (protected)

Returns new text with translation appended.

Parameters:

  • original (String)

    original text

  • translated (String)

    translated text

Returns:

  • (String)

    of new text



75
76
77
# File 'Ruby/translation.nuixscript/language_translation.rb', line 75

def get_new_text(original, translated)
  original + "\n----------#{translation_message}----------\n" + translated
end

#get_original_text(item) ⇒ String (protected)

Returns original text if it had been translated.

Parameters:

  • item (Item)

Returns:

  • (String)

    of text, to the left of —Tran if matched



83
84
85
86
87
88
89
# File 'Ruby/translation.nuixscript/language_translation.rb', line 83

def get_original_text(item)
  text = item.getTextObject.toString
  mymatch = /(^.*?)\n---+Tran/m.match(text)
  return mymatch[1] unless mymatch.nil?

  text
end

#progress_dialog(progress, title) ⇒ Object (protected)

Initializes progress dialog @progress.

Parameters:

  • progress (ProgressDialog)
  • title (String)


95
96
97
98
99
100
101
102
103
# File 'Ruby/translation.nuixscript/language_translation.rb', line 95

def progress_dialog(progress, title)
  @progress = progress
  @progress.setTitle(title)
  @progress.setTimestampLoggedMessages(true)
  @progress.setMainStatusAndLogIt(title)
  @progress.setMainProgress(0, @items.size)
  @progress.setMainProgressVisible(true)
  @progress.setSubProgressVisible(false)
end

#run(items) ⇒ Object (protected)

Shows dialog and updates @settings with input results.

Parameters:

  • items (Set<Item>)


108
109
110
111
112
113
114
115
116
# File 'Ruby/translation.nuixscript/language_translation.rb', line 108

def run(items)
  @items = items
  return nil if @input.nil?

  @input.display
  return nil unless @input.getDialogResult

  @settings = @input.toMap
end

#translate(item, translated) ⇒ Object (protected)

Updates item with translation.

Parameters:

  • item (Item)

    the item to update

  • translated (String)

    the translated text



122
123
124
125
126
127
128
129
130
131
# File 'Ruby/translation.nuixscript/language_translation.rb', line 122

def translate(item, translated)
  case @settings['translation_annotate']
  when @options[0] # Append Text
    new_text = get_new_text(item.getTextObject.toString, translated)
    item.modify { |m| m.replace_text(new_text) }
  when @options[1] # Add Custom Metadata
    field_name = translation_message
    item..putText(field_name, translated)
  end
end

#translation_messageString (private)

The translation message.

Returns:

  • (String)


138
139
140
# File 'Ruby/translation.nuixscript/language_translation.rb', line 138

def translation_message
  "Translation to #{@settings['translation_language']}"
end