Class: GoogleEasyTranslate
- Inherits:
-
NuixTranslator
- Object
- NuixTranslator
- GoogleEasyTranslate
- Defined in:
- Ruby/translation.nuixscript/Translators/google_easy_translate.rb
Overview
Class using EasyTranslate gem for Google.
Constant Summary collapse
- NAME =
'Google Cloud Translation'.freeze
Constants inherited from NuixTranslator
Class Method Summary collapse
Instance Method Summary collapse
-
#add_detection ⇒ Object
private
Adds detection options to main tab of dialog.
-
#add_translation ⇒ Object
private
Adds optional translation options to main tab of dialog.
-
#detect(item) ⇒ Object
private
Detects language of an item and annotates.
-
#detect_language(text) ⇒ String?
private
Detects language of text using EasyTranslate gem.
-
#easy_detect(text) ⇒ Object
private
Detects language of text using EasyTranslate gem.
-
#easy_translate(text) ⇒ Object
private
Translates text using EasyTranslate gem.
-
#initialize ⇒ GoogleEasyTranslate
constructor
Creates a new NuixTranslator using EasyTranslate gem.
-
#languages ⇒ Hash
private
Langague options available.
-
#option_title ⇒ String
private
The enabled options.
-
#process(item) ⇒ Object
private
Detects and/or translates items, depending on the enabled options.
-
#progress_dialog ⇒ Object
private
Progress dialog loop for processing items.
-
#run(items) ⇒ Object
Runs Translator on the items.
-
#translate(item) ⇒ Object
private
Translates item using EasyTranslate gem.
-
#validate_input(values) ⇒ true, false
private
Validation function for input.
Methods inherited from NuixTranslator
#advance, #get_new_text, #get_original_text, #translation_message, translators
Constructor Details
#initialize ⇒ GoogleEasyTranslate
Creates a new NuixTranslator using EasyTranslate gem.
17 18 19 20 21 22 23 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 17 def initialize super(NAME, languages) @main_tab.appendTextField('api_key', 'API Key', '') add_detection add_translation @input.validateBeforeClosing { |v| validate_input(v) } end |
Class Method Details
.name ⇒ Object
12 13 14 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 12 def self.name NAME end |
Instance Method Details
#add_detection ⇒ Object (private)
Adds detection options to main tab of dialog.
39 40 41 42 43 44 45 46 47 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 39 def add_detection @main_tab.appendSeparator('Detection Options') @main_tab.appendCheckBox('apply_custom_metadata', 'Apply detected language as custom metadata?', false) @main_tab.appendTextField('custom_metadata_field_name', 'Custom Metadata Field Name', 'Detected Language') @main_tab.enabledOnlyWhenChecked('custom_metadata_field_name', 'apply_custom_metadata') @main_tab.appendCheckBox('tag_items', 'Tag items with detected language?', false) @main_tab.appendTextField('top_level_tag', 'Tag Name', 'Detected Languages') @main_tab.enabledOnlyWhenChecked('top_level_tag', 'tag_items') end |
#add_translation ⇒ Object (private)
Adds optional translation options to main tab of dialog.
50 51 52 53 54 55 56 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 50 def add_translation @main_tab.appendSeparator('Translation Options') @main_tab.appendCheckBox('translate', 'Translate items?', true) super @main_tab.enabledOnlyWhenChecked('translation_language', 'translate') @main_tab.enabledOnlyWhenChecked('translation_annotate', 'translate') end |
#detect(item) ⇒ Object (private)
Detects language of an item and annotates.
61 62 63 64 65 66 67 68 69 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 61 def detect(item) @progress.setMainStatusAndLogIt('Detecting Language') lang = detect_language(get_original_text(item)) return nil if lang.nil language = "#{@langs[lang]} (#{lang})" item.getCustomMetadata.putText(@settings['custom_metadata_field_name'], language) if @settings['apply_custom_metadata'] item.addTag("#{@settings['top_level_tag']}|#{language}") if @settings['tag_items'] end |
#detect_language(text) ⇒ String? (private)
Detects language of text using EasyTranslate gem.
75 76 77 78 79 80 81 82 83 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 75 def detect_language(text) return nil if text.empty? langs = easy_detect(text) # Google returns "und" (undefined) if the language has not been detected return nil if langs.nil? || langs.eql?('und') langs.to_s end |
#easy_detect(text) ⇒ Object (private)
Detects language of text using EasyTranslate gem.
88 89 90 91 92 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 88 def easy_detect(text) EasyTranslate.detect(text) rescue EasyTranslate::EasyTranslateException => ex @progress.logMessage("ERROR: #{ex.}") end |
#easy_translate(text) ⇒ Object (private)
Translates text using EasyTranslate gem.
97 98 99 100 101 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 97 def easy_translate(text) EasyTranslate.translate(text, format: 'text', to: @langs.key(@settings['translation_language'])) rescue EasyTranslate::EasyTranslateException => ex @progress.logMessage("ERROR: #{ex.}") end |
#languages ⇒ Hash (private)
Langague options available.
106 107 108 109 110 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 106 def languages langs = EasyTranslate::LANGUAGES langs.each_value(&:capitalize!) langs end |
#option_title ⇒ String (private)
The enabled options.
115 116 117 118 119 120 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 115 def option_title = [] << 'Detecting Languages' if @settings['detecting'] << 'Translating Text' if @settings['translate'] .join(' and ') end |
#process(item) ⇒ Object (private)
Detects and/or translates items, depending on the enabled options.
125 126 127 128 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 125 def process(item) detect(item) if @settings['detecting'] translate(item) if @settings['translate'] end |
#progress_dialog ⇒ Object (private)
Progress dialog loop for processing items.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 131 def progress_dialog ProgressDialog.forBlock do |pd| super(pd, option_title) $current_case.with_write_access do @items.each_with_index do |item, index| break if advance(index, "Item GUID: #{item.getGuid}").nil? process(item) end end pd.setCompleted end end |
#run(items) ⇒ Object
Runs Translator on the items.
28 29 30 31 32 33 34 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 28 def run(items) return nil if super(items).nil? EasyTranslate.api_key = @settings['api_key'] @settings['detecting'] = @settings['apply_custom_metadata'] || @settings['tag_items'] progress_dialog end |
#translate(item) ⇒ Object (private)
Translates item using EasyTranslate gem.
148 149 150 151 152 153 154 155 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 148 def translate(item) @progress.setMainStatusAndLogIt('Translating') text = get_original_text(item) return nil if text.empty? translated = easy_translate(text) super(item, translated) unless translated.nil? || translated.empty? end |
#validate_input(values) ⇒ true, false (private)
Validation function for input.
Checks for API key.
Checks an option was checked.
Checks tag or field name was entered, if required.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'Ruby/translation.nuixscript/Translators/google_easy_translate.rb', line 164 def validate_input(values) if values['api_key'].strip.empty? CommonDialogs.showWarning("Please provide a #{NAME} API Key.") return false end unless values['tag_items'] || values['apply_custom_metadata'] || values['translate'] CommonDialogs.showWarning('Please select an option.') return false end if values['apply_custom_metadata'] && values['custom_metadata_field_name'].strip.empty? CommonDialogs.showWarning('Please provide a Custom Metadata Field Name.') return false end if values['tag_items'] && values['top_level_tag'].strip.empty? CommonDialogs.showWarning('Please provide a Top-level tag.') return false end true end |