lib/i18n/tasks/translators/deepl_translator.rb in i18n-tasks-1.0.12 vs lib/i18n/tasks/translators/deepl_translator.rb in i18n-tasks-1.0.13

- old
+ new

@@ -2,10 +2,15 @@ require 'i18n/tasks/translators/base_translator' module I18n::Tasks::Translators class DeeplTranslator < BaseTranslator + # max allowed texts per request + BATCH_SIZE = 50 + # those languages must be specified with their sub-kind e.g en-us + SPECIFIC_TARGETS = %w[en pt].freeze + def initialize(*) begin require 'deepl' rescue LoadError raise ::I18n::Tasks::CommandError, "Add gem 'deepl-rb' to your Gemfile to use this command" @@ -15,28 +20,34 @@ end protected def translate_values(list, from:, to:, **options) - result = DeepL.translate(list, to_deepl_compatible_locale(from), to_deepl_compatible_locale(to), options) - if result.is_a?(DeepL::Resources::Text) - [result.text] - else - result.map(&:text) + results = [] + list.each_slice(BATCH_SIZE) do |parts| + res = DeepL.translate(parts, to_deepl_source_locale(from), to_deepl_target_locale(to), options) + if res.is_a?(DeepL::Resources::Text) + results << res.text + else + results += res.map(&:text) + end end + results end def options_for_translate_values(**options) - { ignore_tags: %w[i18n] }.merge(options) + extra_options = @i18n_tasks.translation_config[:deepl_options]&.symbolize_keys || {} + + extra_options.merge({ ignore_tags: %w[i18n] }).merge(options) end def options_for_html { tag_handling: 'xml' } end def options_for_plain - { preserve_formatting: true } + { preserve_formatting: true, tag_handling: 'xml', html_escape: true } end # @param [String] value # @return [String] 'hello, %{name}' => 'hello, <i18n>%{name}</i18n>' def replace_interpolations(value) @@ -58,12 +69,24 @@ I18n.t('i18n_tasks.deepl_translate.errors.no_results') end private - # Convert 'es-ES' to 'ES' - def to_deepl_compatible_locale(locale) + # Convert 'es-ES' to 'ES', en-us to EN + def to_deepl_source_locale(locale) locale.to_s.split('-', 2).first.upcase + end + + # Convert 'es-ES' to 'ES' but warn about locales requiring a specific variant + def to_deepl_target_locale(locale) + loc, sub = locale.to_s.split('-') + if SPECIFIC_TARGETS.include?(loc) + # Must see how the deepl api evolves, so this could be an error in the future + warn_deprecated I18n.t('i18n_tasks.deepl_translate.errors.specific_target_missing') unless sub + locale.to_s.upcase + else + loc.upcase + end end def configure_api_key! api_key = @i18n_tasks.translation_config[:deepl_api_key] host = @i18n_tasks.translation_config[:deepl_host]