Class: BetterTranslate::Strategies::DeepStrategy

Inherits:
BaseStrategy
  • Object
show all
Defined in:
lib/better_translate/strategies/deep_strategy.rb

Overview

Deep translation strategy

Translates each string individually with detailed progress tracking. Used for smaller files (< 50 strings) to provide more granular progress.

Examples:

strategy = DeepStrategy.new(config, provider, tracker)
translated = strategy.translate(strings, "it", "Italian")

Instance Attribute Summary

Attributes inherited from BaseStrategy

#config, #progress_tracker, #provider

Instance Method Summary collapse

Methods inherited from BaseStrategy

#initialize

Constructor Details

This class inherits a constructor from BetterTranslate::Strategies::BaseStrategy

Instance Method Details

#translate(strings, target_lang_code, target_lang_name) ⇒ Hash

Translate strings individually

Examples:

translated = strategy.translate({ "greeting" => "Hello" }, "it", "Italian")
#=> { "greeting" => "Ciao" }

Parameters:

  • strings (Hash)

    Flattened hash of strings to translate

  • target_lang_code (String)

    Target language code

  • target_lang_name (String)

    Target language name

Returns:

  • (Hash)

    Translated strings (flattened)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/better_translate/strategies/deep_strategy.rb', line 26

def translate(strings, target_lang_code, target_lang_name)
  # @type var translated: Hash[String, String]
  translated = {}
  total = strings.size

  strings.each_with_index do |(key, value), index|
    progress_tracker.update(
      language: target_lang_name,
      current_key: key,
      progress: ((index + 1).to_f / total * 100.0).round(1).to_f
    )

    translated[key] = provider.translate_text(value, target_lang_code, target_lang_name)
  end

  translated
end