Class: Jekyll::L10n::PoFileLoader

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

Overview

Loads PO files from disk with automatic caching.

PoFileLoader handles loading PO files and managing the global in-memory cache to avoid redundant disk I/O. It constructs proper file paths for locale and page combinations, checks the cache first, and reads from disk only when needed.

Key responsibilities:

  • Check global cache for already-loaded files

  • Construct file paths for locale/page combinations

  • Load PO files from disk when not cached

  • Parse PO file content into translation hashes

  • Cache parsed translations for reuse

  • Handle file-not-found gracefully (return empty hash)

  • Log errors during loading

Examples:

translations = PoFileLoader.load('_site', '_locales', 'es', 'docs/index.html')
# Returns cached or newly-loaded translations for that page/locale

See Also:

Class Method Summary collapse

Class Method Details

.load(source, locales_dir, locale, page_path) ⇒ Hash

Load a PO file with caching.

First checks the global PoFileManager cache for already-loaded translations. If not found, constructs the file path, loads from disk if file exists, parses the PO file, caches the result, and returns. Returns empty hash if file doesn’t exist or if an error occurs during loading.

Parameters:

  • source (String)

    Site source directory

  • locales_dir (String)

    Locales directory name (e.g., ‘_locales’)

  • locale (String)

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

  • page_path (String, nil)

    Optional page path for page-specific file. If nil, loads compendium.

Returns:

  • (Hash)

    Translation hash { msgid => msgstr } or empty hash if not found



43
44
45
46
47
48
49
50
51
52
# File 'lib/jekyll-l10n/po_file/loader.rb', line 43

def self.load(source, locales_dir, locale, page_path)
  cache_key = "#{locale}:#{page_path}"
  cached = PoFileManager.cache[cache_key]
  return cached if cached

  po_path = PoPathBuilder.build(source, locales_dir, locale, page_path)
  return {} unless File.exist?(po_path)

  load_and_cache(cache_key, po_path)
end

.load_and_cache(cache_key, po_path) ⇒ Object



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

def self.load_and_cache(cache_key, po_path)
  translations = PoFileReader.parse(po_path)
  PoFileManager.cache[cache_key] = translations
  translations
rescue StandardError => e
  Jekyll.logger.warn 'Localization', "Error loading PO file #{po_path}: #{e.message}"
  {}
end