Sha256: 4eb13e4ade84d26be91f699d7b27c001ddeefc62816d1ea6ce3ff6023c768f1d

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'easy_translate'
require 'i18n/tasks/translators/base_translator'

module I18n::Tasks::Translators
  class GoogleTranslator < BaseTranslator
    protected

    def translate_values(list, **options)
      EasyTranslate.translate(list, options)
    end

    def options_for_translate_values(from:, to:, **options)
      options.merge(
        api_key: api_key,
        from: to_google_translate_compatible_locale(from),
        to: to_google_translate_compatible_locale(to)
      )
    end

    def options_for_html
      { html: true }
    end

    def options_for_plain
      { format: 'text' }
    end

    def no_results_error_message
      I18n.t('i18n_tasks.google_translate.errors.no_results')
    end

    private

    SUPPORTED_LOCALES_WITH_REGION = %w[zh-CN zh-TW].freeze

    # Convert 'es-ES' to 'es'
    def to_google_translate_compatible_locale(locale)
      return locale unless locale.include?('-') && !SUPPORTED_LOCALES_WITH_REGION.include?(locale)
      locale.split('-', 2).first
    end

    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]
          @i18n_tasks.warn_deprecated(
            'Please rename Google Translate API Key from `api_key` to `google_translate_api_key`.'
          )
          key ||= translation_config[:api_key]
        end
        fail CommandError, I18n.t('i18n_tasks.google_translate.errors.no_api_key') if key.blank?
        key
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-tasks-0.9.23 lib/i18n/tasks/translators/google_translator.rb