Class: BetterTranslate::Strategies::BaseStrategy Abstract

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

Overview

This class is abstract.

Subclasses must implement #translate

Base class for translation strategies

Examples:

Creating a custom strategy

class MyStrategy < BaseStrategy
  def translate(strings, target_lang_code, target_lang_name)
    # Custom translation logic
  end
end

Direct Known Subclasses

BatchStrategy, DeepStrategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, provider, progress_tracker) ⇒ BaseStrategy

Initialize the strategy

Examples:

strategy = BaseStrategy.new(config, provider, tracker)

Parameters:



36
37
38
39
40
# File 'lib/better_translate/strategies/base_strategy.rb', line 36

def initialize(config, provider, progress_tracker)
  @config = config
  @provider = provider
  @progress_tracker = progress_tracker
end

Instance Attribute Details

#configConfiguration (readonly)

Returns Configuration object.

Returns:



19
20
21
# File 'lib/better_translate/strategies/base_strategy.rb', line 19

def config
  @config
end

#progress_trackerProgressTracker (readonly)

Returns Progress tracker.

Returns:



25
26
27
# File 'lib/better_translate/strategies/base_strategy.rb', line 25

def progress_tracker
  @progress_tracker
end

#providerProviders::BaseHttpProvider (readonly)

Returns Translation provider.

Returns:



22
23
24
# File 'lib/better_translate/strategies/base_strategy.rb', line 22

def provider
  @provider
end

Instance Method Details

#translate(strings, target_lang_code, target_lang_name) ⇒ Hash

Translate strings

Examples:

translated = strategy.translate(strings, "it", "Italian")

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)

Raises:

  • (NotImplementedError)

    Must be implemented by subclasses



53
54
55
# File 'lib/better_translate/strategies/base_strategy.rb', line 53

def translate(strings, target_lang_code, target_lang_name)
  raise NotImplementedError, "#{self.class} must implement #translate"
end