class I18n::Tasks::Translators::DeeplTranslator
Constants
- BATCH_SIZE
-
max allowed texts per request
- SPECIFIC_TARGETS
-
those languages must be specified with their sub-kind e.g en-us
Public Class Methods
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 12 def initialize(*) begin require 'deepl' rescue LoadError raise ::I18n::Tasks::CommandError, "Add gem 'deepl-rb' to your Gemfile to use this command" end super configure_api_key! end
Calls superclass method
I18n::Tasks::Translators::BaseTranslator::new
Protected Instance Methods
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 75 def no_results_error_message I18n.t('i18n_tasks.deepl_translate.errors.no_results') end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 50 def options_for_html { tag_handling: 'xml' } end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 54 def options_for_plain { preserve_formatting: true, tag_handling: 'xml', html_escape: true } end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 44 def options_for_translate_values(**options) extra_options = @i18n_tasks.translation_config[:deepl_options]&.symbolize_keys || {} extra_options.merge({ ignore_tags: %w[i18n] }).merge(options) end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 60 def replace_interpolations(value) value.gsub(INTERPOLATION_KEY_RE, '<i18n>\0</i18n>') end
@param [String] value @return [String] ‘hello, %{name}’ => ‘hello, <i18n>%{name}</i18n>’
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 67 def restore_interpolations(untranslated, translated) return translated if untranslated !~ INTERPOLATION_KEY_RE translated.gsub(%r{</?i18n>}, '') rescue StandardError => e raise_interpolation_error(untranslated, translated, e) end
@param [String] untranslated @param [String] translated @return [String] ‘hello, <i18n>%{name}</i18n>’ => ‘hello, %{name}’
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 24 def translate_values(list, from:, to:, **options) results = [] list.each_slice(BATCH_SIZE) do |parts| res = DeepL.translate( parts, to_deepl_source_locale(from), to_deepl_target_locale(to), options_with_glossary(options, from, to) ) if res.is_a?(DeepL::Resources::Text) results << res.text else results += res.map(&:text) end @progress_bar.progress += parts.size end results end
Private Instance Methods
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 119 def all_ready_glossaries @all_ready_glossaries ||= DeepL.glossaries.list end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 101 def configure_api_key! api_key = @i18n_tasks.translation_config[:deepl_api_key] host = @i18n_tasks.translation_config[:deepl_host] version = @i18n_tasks.translation_config[:deepl_version] fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.deepl_translate.errors.no_api_key') if api_key.blank? DeepL.configure do |config| config.auth_key = api_key config.host = host unless host.blank? config.version = version unless version.blank? end end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 123 def find_glossary(from, to) config_glossary_ids = @i18n_tasks.translation_config[:deepl_glossary_ids] return unless config_glossary_ids all_ready_glossaries.find do |glossary| glossary.ready \ && glossary.source_lang == from \ && glossary.target_lang == to \ && config_glossary_ids.include?(glossary.id) end end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 114 def options_with_glossary(options, from, to) glossary = find_glossary(from, to) glossary ? { glossary_id: glossary.id }.merge(options) : options end
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 82 def to_deepl_source_locale(locale) locale.to_s.split('-', 2).first.upcase end
Convert ‘es-ES’ to ‘ES’, en-us to EN
Source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 87 def to_deepl_target_locale(locale) locale_aliases = @i18n_tasks.translation_config[:deepl_locale_aliases] locale = locale_aliases[locale.to_s.downcase] || locale if locale_aliases.is_a?(Hash) 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
Convert ‘es-ES’ to ‘ES’ but warn about locales requiring a specific variant