class I18n::Tasks::Translators::GoogleTranslator
Constants
- NEWLINE_PLACEHOLDER
Public Class Methods
new(*)
click to toggle source
Calls superclass method
I18n::Tasks::Translators::BaseTranslator::new
# File lib/i18n/tasks/translators/google_translator.rb, line 8 def initialize(*) begin require 'easy_translate' rescue LoadError raise ::I18n::Tasks::CommandError, "Add gem 'easy_translate' to your Gemfile to use this command" end super end
Protected Instance Methods
no_results_error_message()
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 46 def no_results_error_message I18n.t('i18n_tasks.google_translate.errors.no_results') end
options_for_html()
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 38 def options_for_html { html: true } end
options_for_plain()
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 42 def options_for_plain { format: 'text' } end
options_for_translate_values(from:, to:, **options)
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 30 def options_for_translate_values(from:, to:, **options) options.merge( api_key: api_key, from: from, to: to ) end
translate_values(list, **options)
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 19 def translate_values(list, **options) restore_newlines( EasyTranslate.translate( replace_newlines_with_placeholder(list, options[:html]), options, format: :text ), options[:html] ) end
Private Instance Methods
api_key()
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 52 def api_key @api_key ||= begin key = @i18n_tasks.translation_config[:google_translate_api_key] # fallback with deprecation warning if @i18n_tasks.translation_config[:api_key] warn_deprecated( 'Please rename Google Translate API Key from `api_key` to `google_translate_api_key`.' ) key ||= translation_config[:api_key] end fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.google_translate.errors.no_api_key') if key.blank? key end end
replace_newlines_with_placeholder(list, html)
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 68 def replace_newlines_with_placeholder(list, html) return list unless html list.map do |value| value.gsub("\n", NEWLINE_PLACEHOLDER) end end
restore_newlines(translations, html)
click to toggle source
# File lib/i18n/tasks/translators/google_translator.rb, line 76 def restore_newlines(translations, html) return translations unless html translations.map do |translation| translation.gsub("#{NEWLINE_PLACEHOLDER} ", "\n") end end