Class: Jekyll::L10n::ExtractionResultSaver

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/extraction/result_saver.rb

Overview

Saves extraction results to PO files and optionally translates compendia.

ExtractionResultSaver orchestrates the post-extraction process: writing page-specific PO files for all configured locales, merging with existing translations, optionally updating compendia, and triggering automatic translation via LibreTranslate if enabled.

Key responsibilities:

  • Save page-specific PO files for each locale

  • Merge new entries with existing translations

  • Update compendium files from page-specific extractions

  • Trigger automatic translation of compendia

  • Report extraction statistics

Examples:

saver = ExtractionResultSaver.new(site)
stats = saver.save_results(config, entries, 'docs/index.html')
saver.translate_compendia(config) if config.libretranslate_enabled?

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ ExtractionResultSaver

Initialize a new ExtractionResultSaver.

Parameters:

  • site (Jekyll::Site)

    Jekyll site object



34
35
36
37
38
# File 'lib/jekyll-l10n/extraction/result_saver.rb', line 34

def initialize(site)
  @site = site
  with_locales_data = SiteConfigAccessor.extract_locales_data(@site)
  @site_config = PageLocalesConfig.new({ 'with_locales_data' => with_locales_data })
end

Instance Method Details

#save_results(config, entries, page_path) ⇒ Hash

Save extraction results to PO files.

Saves extracted strings to PO files for each configured locale, merging with existing translations to preserve manual edits. Optionally merges new entries into compendium files. Returns statistics about the save operation.

Parameters:

  • config (PageLocalesConfig)

    Localization configuration

  • entries (Array<Hash>)

    Array of extracted entries with :msgid, :msgstr, :reference

  • page_path (String)

    Page path for file organization (e.g., ‘docs/index.html’)

Returns:

  • (Hash)

    Statistics hash with keys:

    • :files_processed [Integer] Number of pages processed (always 1)

    • :strings_extracted [Integer] Number of extracted strings

    • :po_files_created [Integer] Number of PO files created/updated



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll-l10n/extraction/result_saver.rb', line 53

def save_results(config, entries, page_path)
  LoggerFormatter.debug_if_enabled('ExtractionResultSaver', "Processing page: #{page_path}")

  po_manager = PoFileManager.new(@site, config.locales_dir)
  po_files_created = save_po_files(po_manager, config, entries, page_path)

  {
    files_processed: 1,
    strings_extracted: entries.length,
    po_files_created: po_files_created
  }
end

#translate_compendia(config) ⇒ void

This method returns an undefined value.

Translate compendia using LibreTranslate.

If LibreTranslate is enabled in config, translates all empty entries in compendium files for configured locales. Called after extraction and compendium merging to fill in translations automatically.

Parameters:

  • config (PageLocalesConfig)

    Localization configuration with LibreTranslate settings



74
75
76
# File 'lib/jekyll-l10n/extraction/result_saver.rb', line 74

def translate_compendia(config)
  CompendiumTranslator.new(@site).translate_compendia(config)
end