Class: Jekyll::L10n::Translator
- Inherits:
-
Object
- Object
- Jekyll::L10n::Translator
- 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:
-
Check if the page is localized and has required metadata
-
Load translations from PO files (page-specific and compendium)
-
Apply translations to text nodes and attributes in HTML
-
Handle missing translations with configured fallback modes
-
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.
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
-
#initialize(page) ⇒ Translator
constructor
Initialize the translator.
-
#translate ⇒ void
Apply translations to the page.
Constructor Details
#initialize(page) ⇒ Translator
Initialize the translator
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
#page ⇒ Object (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
#translate ⇒ void
Only translates pages with localized: true and matching locale/original_url
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 |