Class: Jekyll::L10n::PoEntryConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/utils/po_entry_converter.rb

Overview

Converts between translation hash and GetText POEntry formats.

PoEntryConverter handles bidirectional conversion between simple translation hashes and GetText::POEntry objects used by the ruby-gettext gem. It preserves msgstr values, reference comments, and fuzzy flags during conversion.

Key responsibilities:

  • Convert translation hashes to POEntry objects

  • Convert POEntry objects back to hash format

  • Preserve metadata (references, fuzzy flags) during conversion

  • Handle both hash and array return formats

Examples:

hash = { "Hello" => { msgstr: "Hola", reference: "file.html:10" } }
entries = PoEntryConverter.hash_to_po_entry_array(hash)
# entries is array of GetText::POEntry objects

Class Method Summary collapse

Class Method Details

.hash_to_po_entries(hash) ⇒ Hash

Convert translation hash to POEntry hash.

Converts simple translation hash { msgid => msgstr } or metadata format { msgid => { msgstr: “…”, reference: “…”, fuzzy: false } } to a hash of GetText::POEntry objects keyed by msgid.

Parameters:

  • hash (Hash)

    Translation hash

Returns:

  • (Hash)

    Hash of { msgid => POEntry }



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll-l10n/utils/po_entry_converter.rb', line 32

def self.hash_to_po_entries(hash)
  return {} if hash.nil? || hash.empty?

  hash.each_with_object({}) do |(key, value), result|
    entry = ::GetText::POEntry.new(:normal)
    entry.msgid = key

    if value.is_a?(Hash)
      entry.msgstr = value[:msgstr]
      entry.add_comment(value[:reference]) if value[:reference]
      entry.flag = 'fuzzy' if value[:fuzzy]
    else
      entry.msgstr = value
    end

    result[key] = entry
  end
end

.hash_to_po_entry_array(hash) ⇒ Array<GetText::POEntry>

Convert translation hash to array of POEntry objects.

Converts hash format to array of GetText::POEntry objects, preserving references and fuzzy flags.

Parameters:

  • hash (Hash)

    Translation hash

Returns:

  • (Array<GetText::POEntry>)

    Array of POEntry objects



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jekyll-l10n/utils/po_entry_converter.rb', line 58

def self.hash_to_po_entry_array(hash)
  return [] if hash.nil? || hash.empty?

  hash.map do |msgid, data|
    entry = ::GetText::POEntry.new(:normal)
    entry.msgid = msgid

    if data.is_a?(Hash)
      entry.msgstr = data[:msgstr]
      entry.add_comment(data[:reference]) if data[:reference]
      entry.flag = 'fuzzy' if data[:fuzzy]
    else
      entry.msgstr = data
    end

    entry
  end
end

.po_entries_to_array_of_hashes(entries) ⇒ Array<Hash>

Convert POEntry objects to array of hashes.

Each entry becomes a hash with :msgid and :msgstr (and optional :reference).

Parameters:

  • entries (Array<GetText::POEntry>)

    Array of POEntry objects

Returns:

  • (Array<Hash>)

    Array of { msgid: “…”, msgstr: “…”, reference: “…” }



98
99
100
101
102
103
104
105
106
# File 'lib/jekyll-l10n/utils/po_entry_converter.rb', line 98

def self.po_entries_to_array_of_hashes(entries)
  return [] if entries.nil? || entries.empty?

  entries.map do |entry|
    hash = { msgid: entry.msgid, msgstr: entry.msgstr }
    hash[:reference] = entry.extracted_comment if entry.extracted_comment && !entry.extracted_comment.empty?
    hash
  end
end

.po_entries_to_hash(entries) ⇒ Hash

Convert POEntry objects to translation hash.

Parameters:

  • entries (Array<GetText::POEntry>)

    Array of POEntry objects

Returns:

  • (Hash)

    Hash of { msgid => { msgstr: “…”, reference: “…” } }



81
82
83
84
85
86
87
88
89
90
# File 'lib/jekyll-l10n/utils/po_entry_converter.rb', line 81

def self.po_entries_to_hash(entries)
  return {} if entries.nil? || entries.empty?

  entries.to_h do |entry|
    [entry.msgid, {
      msgstr: entry.msgstr.to_s,
      reference: entry.extracted_comment
    }]
  end
end