Class: Jekyll::L10n::PoFileMerger

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/po_file/merger.rb

Overview

Merges multiple PO files for a locale into a single translation hash.

PoFileMerger combines page-specific PO files in a locale directory into a single merged translation hash. Used when creating compendia by combining all extracted strings across pages for a locale.

Key responsibilities:

  • Find all PO files for a locale

  • Parse PO files with reference metadata

  • Merge into single translation hash

  • Preserve references for debugging

  • Handle parsing errors gracefully

Examples:

merged = PoFileMerger.merge_for_locale('_site', '_locales', 'es')
# Returns combined translations from all _locales/es/**/*.po files

See Also:

Class Method Summary collapse

Class Method Details

.merge_file_into(po_file, merged) ⇒ void

This method returns an undefined value.

Merge a single PO file into an existing merged hash.

Parses the PO file with references and adds entries that aren’t already in the merged hash. The hash can be empty, and it is modified in place.

Parameters:

  • po_file (String)

    Path to PO file

  • merged (Hash)

    Existing merged hash (can be empty, modified in place)



70
71
72
73
74
75
76
77
# File 'lib/jekyll-l10n/po_file/merger.rb', line 70

def self.merge_file_into(po_file, merged)
  translations = PoFileReader.parse_with_references(po_file)
  translations.each do |msgid, entry|
    merged[msgid] ||= entry
  end
rescue StandardError => e
  Jekyll.logger.warn 'Localization', "Error merging PO file #{po_file}: #{e.message}"
end

.merge_files(po_files) ⇒ Hash<String, Hash>

Merge a list of PO files.

Parses each file and merges into single hash. Later files don’t override entries from earlier files (first occurrence wins).

Parameters:

  • po_files (Array<String>)

    Paths to PO files to merge

Returns:

  • (Hash<String, Hash>)

    Merged translation hash where keys are msgid strings and values are hashes with :msgstr and :reference keys



54
55
56
57
58
59
60
# File 'lib/jekyll-l10n/po_file/merger.rb', line 54

def self.merge_files(po_files)
  merged = {}
  po_files.each do |po_file|
    merge_file_into(po_file, merged)
  end
  merged
end

.merge_for_locale(source, locales_dir, locale) ⇒ Hash<String, Hash>

Merge all PO files for a locale.

Finds all PO files in the locale subdirectory, parses them with references, and merges into a single hash. First occurrence of each msgid is kept.

Parameters:

  • source (String)

    Site source directory

  • locales_dir (String)

    Locales directory name

  • locale (String)

    Locale code (e.g., ‘es’, ‘fr’)

Returns:

  • (Hash<String, Hash>)

    Merged translation hash where keys are msgid strings and values are hashes with :msgstr and :reference keys



38
39
40
41
42
43
44
# File 'lib/jekyll-l10n/po_file/merger.rb', line 38

def self.merge_for_locale(source, locales_dir, locale)
  locale_dir = File.join(source, locales_dir, locale)
  return {} unless File.directory?(locale_dir)

  po_files = Dir.glob(File.join(locale_dir, '**', '*.po')).sort
  merge_files(po_files)
end