Class: Jekyll::L10n::Translator

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

Overview

Translation Orchestrator - Applies PO file translations to localized pages

The Translator is the main entry point for applying translations to pages. It’s invoked during the post_render phase of the Jekyll build (via a Jekyll hook) for each localized page variant. The translator loads appropriate translations from PO files and applies them to the page’s HTML content.

The translation workflow:

  1. Check if the page is localized and has required metadata

  2. Load translations from PO files (page-specific and compendium)

  3. Apply translations to text nodes and attributes in HTML

  4. Handle missing translations with configured fallback modes

  5. Preserve special elements like external link icons

Key responsibilities:

  • Load translations for the page’s locale

  • Apply translations to HTML with DOM manipulation

  • Handle fallback modes (english, marker, empty)

  • Preserve special formatting and elements (icons, badges, etc.)

  • Log translation progress and errors

This runs in phase 3 of the Jekyll build pipeline (Post-Render Phase). See the “Build Pipeline” section in lib/jekyll-l10n.rb for the complete workflow.

Examples:

Usage (typically invoked via Jekyll hook)

# Automatically invoked for localized pages in post_render phase
translator = Translator.new(localized_page)
translator.translate

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Translator

Initialize the translator

Parameters:

  • page (Jekyll::Page)

    The page to translate (should be a LocalizedPage)



54
55
56
57
# File 'lib/jekyll-l10n/translation/translator.rb', line 54

def initialize(page)
  @page = page
  @site = page.site
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



49
50
51
# File 'lib/jekyll-l10n/translation/translator.rb', line 49

def page
  @page
end

Instance Method Details

#translatevoid

Note:

Only translates pages with localized: true and matching locale/original_url

Note:

Gracefully handles missing translations with configured fallback mode

This method returns an undefined value.

Apply translations to the page

Main entry point for translation. Checks if the page should be translated, loads the appropriate translations from PO files, applies them to the HTML, and updates the page’s output.

This method is called automatically by Jekyll’s post_render hook for each LocalizedPage during the build process.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jekyll-l10n/translation/translator.rb', line 71

def translate
  return unless should_translate?

  locale = @page.data['locale']
  original_url = @page.data['original_url']
  baseurl = @site.config['baseurl'] || ''

  original_page = find_and_log_original_page(original_url)
  return unless original_page

  translations = load_and_log_translations(locale, original_url, original_page)
  return if translations.nil? || translations.empty?

  apply_translations_to_page(original_page, translations, locale, baseurl)
end