Class: NuixTranslator
- Inherits:
-
Object
- Object
- NuixTranslator
- Defined in:
- Ruby/translation.nuixscript/language_translation.rb
Overview
Base class for translating items.
Direct Known Subclasses
ClearTranslations, GoogleEasyTranslate, MicrosoftCognitiveServices
Constant Summary collapse
- SCRIPT_DIR =
File.join(File.dirname(__FILE__), 'Translators')
Class Method Summary collapse
-
.translators ⇒ Hash
The NuixTranslator subclasses.
Instance Method Summary collapse
-
#add_translation ⇒ Object
protected
Adds translation options to main tab of dialog.
-
#advance(index, status) ⇒ nil, true
protected
Advances progress.
-
#get_new_text(original, translated) ⇒ String
protected
Returns new text with translation appended.
-
#get_original_text(item) ⇒ String
protected
Returns original text if it had been translated.
-
#initialize(name, languages) ⇒ NuixTranslator
constructor
Creates a new Translator.
-
#progress_dialog(progress, title) ⇒ Object
protected
Initializes progress dialog @progress.
-
#run(items) ⇒ Object
protected
Shows dialog and updates @settings with input results.
-
#translate(item, translated) ⇒ Object
protected
Updates item with translation.
-
#translation_message ⇒ String
private
The translation message.
Constructor Details
#initialize(name, languages) ⇒ NuixTranslator
Creates a new Translator.
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
.translators ⇒ Hash
The NuixTranslator subclasses.
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_translation ⇒ Object (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.
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.
75 76 77 |
# File 'Ruby/translation.nuixscript/language_translation.rb', line 75 def get_new_text(original, translated) original + "\n----------#{}----------\n" + translated end |
#get_original_text(item) ⇒ String (protected)
Returns original text if it had been translated.
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.
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.
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.
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 = item.getCustomMetadata.putText(field_name, translated) end end |
#translation_message ⇒ String (private)
The translation message.
138 139 140 |
# File 'Ruby/translation.nuixscript/language_translation.rb', line 138 def "Translation to #{@settings['translation_language']}" end |