Class: Jekyll::L10n::LibreTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/translation/libre_translator.rb

Overview

Translates compendium entries using the LibreTranslate API.

LibreTranslator integrates with the LibreTranslate API to automatically translate untranslated entries in compendium PO files. It handles batching, retries with exponential backoff, progress logging, and API error handling. Translations are written directly to POEntry objects in-place.

Key responsibilities:

  • Send translation requests to LibreTranslate API

  • Batch translations for efficiency

  • Handle API timeouts and failures with retries

  • Log translation progress at configurable intervals

  • Update POEntry msgstr with translated text

  • Parse JSON API responses

Examples:

translator = LibreTranslator.new(config)
translator.translate_compendium(po_entries, 'es')
# po_entries now have msgstr filled from API translations

Defined Under Namespace

Classes: TranslationError

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ LibreTranslator

Initialize a new LibreTranslator.

Parameters:

  • config (PageLocalesConfig)

    Configuration with LibreTranslate settings:

    • libretranslate_api_url [String] API endpoint URL

    • libretranslate_api_key [String, nil] Optional API key

    • libretranslate_timeout [Integer] Request timeout in seconds

    • libretranslate_batch_size [Integer] Entries per batch request

    • libretranslate_retry_attempts [Integer] Max retry attempts

    • libretranslate_retry_delay [Integer] Delay between retries in seconds

    • libretranslate_progress_interval [Integer] Log progress every N entries



45
46
47
# File 'lib/jekyll-l10n/translation/libre_translator.rb', line 45

def initialize(config)
  @config = config
end

Instance Method Details

#translate_compendium(po_entries, target_locale) ⇒ void

This method returns an undefined value.

Translate a compendium to a target locale.

Identifies untranslated entries (empty msgstr) and sends them to LibreTranslate API for translation. Updates POEntry objects in-place with translated text. Handles batching, retries, and progress logging.

Parameters:

  • po_entries (Array<GetText::POEntry>)

    Array of PO entries from compendium

  • target_locale (String)

    Target locale code (e.g., ‘es’, ‘fr’)

Raises:



60
61
62
63
64
65
66
67
68
# File 'lib/jekyll-l10n/translation/libre_translator.rb', line 60

def translate_compendium(po_entries, target_locale)
  translatable_count, empty_entries = count_translatable_entries(po_entries)
  return if empty_entries.empty?

  log_translation_progress(empty_entries.length, translatable_count, target_locale)
  start_time = Time.now
  process_translation_batches(empty_entries, target_locale, start_time)
  log_translation_complete(empty_entries.length, target_locale, start_time)
end