Skip to content

Commit

Permalink
Add progress bar for translations (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
markedmondson authored Nov 8, 2024
1 parent b327b26 commit eefc659
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions i18n-tasks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Gem::Specification.new do |s|
s.add_dependency 'parser', '>= 3.2.2.1'
s.add_dependency 'rails-i18n'
s.add_dependency 'rainbow', '>= 2.2.2', '< 4.0'
s.add_dependency 'ruby-progressbar', '~> 1.8', '>= 1.8.1'
s.add_dependency 'terminal-table', '>= 1.5.1'
s.add_development_dependency 'bundler', '~> 2.0', '>= 2.0.1'
s.add_development_dependency 'overcommit', '~> 0.58.0'
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module Data
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/try'

require 'ruby-progressbar'
require 'rainbow'
require 'erubi'

Expand Down
6 changes: 5 additions & 1 deletion lib/i18n/tasks/translators/base_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def initialize(i18n_tasks)
# @return [I18n::Tasks::Tree::Siblings] translated forest
def translate_forest(forest, from)
forest.inject @i18n_tasks.empty_forest do |result, root|
translated = translate_pairs(root.key_values(root: true), to: root.key, from: from)
pairs = root.key_values(root: true)

@progress_bar = ProgressBar.create(total: pairs.flatten.size, format: '%a <%B> %e %c/%C (%p%%)')

translated = translate_pairs(pairs, to: root.key, from: from)
result.merge! Data::Tree::Siblings.from_flat_pairs(translated)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/i18n/tasks/translators/deepl_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def translate_values(list, from:, to:, **options)
else
results += res.map(&:text)
end

@progress_bar.progress += parts.size
end
results
end
Expand Down
6 changes: 5 additions & 1 deletion lib/i18n/tasks/translators/google_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ def initialize(*)
protected

def translate_values(list, **options)
restore_newlines(
result = restore_newlines(
EasyTranslate.translate(
replace_newlines_with_placeholder(list, options[:html]),
options,
format: options[:html] ? :html : :text
),
options[:html]
)

@progress_bar.progress += result.size

result
end

def options_for_translate_values(from:, to:, **options)
Expand Down
4 changes: 3 additions & 1 deletion lib/i18n/tasks/translators/openai_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def translate_values(list, from:, to:)

list.each_slice(BATCH_SIZE) do |batch|
translations = translate(batch, from, to)
result = JSON.parse(translations)
results << result

results << JSON.parse(translations)
@progress_bar.progress += result.size
end

results.flatten
Expand Down
4 changes: 3 additions & 1 deletion lib/i18n/tasks/translators/watsonx_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ def translate_values(list, from:, to:)

list.each_slice(BATCH_SIZE) do |batch|
translations = translate(batch, from, to)
result = JSON.parse(translations)
results << result

results << JSON.parse(translations)
@progress_bar.progress += results.size
end

results.flatten
Expand Down
6 changes: 5 additions & 1 deletion lib/i18n/tasks/translators/yandex_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def initialize(*)
protected

def translate_values(list, **options)
list.map { |item| translator.translate(item, options) }
result = list.map { |item| translator.translate(item, options) }

@progress_bar.progress += result.size

result
end

def options_for_translate_values(from:, to:, **options)
Expand Down

0 comments on commit eefc659

Please sign in to comment.