Class: Jekyll::L10n::PageTranslationLoader

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

Overview

Loads and merges page-specific and compendium translations.

PageTranslationLoader combines compendium (site-wide) translations with page-specific translations for a given locale and URL. It loads both translation sources from PO files, merges them (page-specific takes precedence), and returns the combined translation hash for use by HtmlTranslator.

Key responsibilities:

  • Load compendium translations for a locale

  • Convert page URLs to PO file paths

  • Load page-specific translations

  • Merge compendium and page-specific translations

  • Filter empty translations (untranslated entries)

  • Log loading progress at debug level

Examples:

config = PageLocalesConfig.new(page.data)
translations = PageTranslationLoader.load(site, 'es', '/docs/index.html', config)
# Returns merged translations for that page in Spanish

Class Method Summary collapse

Class Method Details

.construct_po_page_path(url) ⇒ Object



60
61
62
# File 'lib/jekyll-l10n/translation/page_translation_loader.rb', line 60

def self.construct_po_page_path(url)
  UrlPathBuilder.url_to_po_page_path(url)
end

.load(site, locale, original_url, config) ⇒ Hash

Load and merge translations for a page in a specific locale.

Loads the compendium (site-wide) translations and page-specific translations, merges them (page-specific entries override compendium), and returns the combined hash. Filters out untranslated entries (empty msgstr).

Parameters:

  • site (Jekyll::Site)

    Jekyll site object

  • locale (String)

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

  • original_url (String)

    Original page URL (e.g., ‘/docs/index.html’)

  • config (PageLocalesConfig)

    Localization configuration for the page

Returns:

  • (Hash)

    Merged translation hash { msgid => msgstr }, filtered to only include non-empty translations



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jekyll-l10n/translation/page_translation_loader.rb', line 44

def self.load(site, locale, original_url, config)
  po_manager = PoFileManager.new(site, config.locales_dir)
  page_path = construct_po_page_path(original_url)

  compendium = po_manager.load_compendium(locale)
  log_compendium_loaded(locale, compendium)

  page_specific = po_manager.load_po_file(locale, page_path)
  log_page_specific_loaded(page_path, page_specific)

  translations = compendium.merge(page_specific)
  log_translations_merged(translations)

  translations.reject { |_msgid, msgstr| msgstr.empty? }
end