Class: Jekyll::L10n::TranslationResolver
- Inherits:
-
Object
- Object
- Jekyll::L10n::TranslationResolver
- Defined in:
- lib/jekyll-l10n/utils/translation_resolver.rb
Overview
Resolves translations for text nodes with fallback to block-level translations.
TranslationResolver looks up translations for normalized text, first trying direct text node matches, then falling back to block-level translations when text is part of a larger block element with its own translation. This enables translating entire paragraphs as single units instead of word-by-word.
Key responsibilities:
-
Look up direct translation for text node
-
Fall back to block-level translation if available
-
Return appropriate translation or nil if none found
Class Method Summary collapse
-
.contains_protected_elements?(node) ⇒ Boolean
Check if an element contains protected child elements that block translations.
- .content_element?(node) ⇒ Boolean
-
.resolve(node, text, translations) ⇒ String?
Resolve a translation for a text node.
-
.try_block_level_translation(node, text, translations) ⇒ String?
Attempt block-level translation for text nodes that are part of larger blocks.
Class Method Details
.contains_protected_elements?(node) ⇒ Boolean
Check if an element contains protected child elements that block translations.
Protected elements (script, style) cannot have their surrounding text translated at the block level for security and functionality reasons. <pre> is not protected here — HtmlTextUtils.remove_code_blocks strips it before extraction so code content never reaches PO msgids, and HtmlTranslator preserves <pre> verbatim across translation injection.
113 114 115 116 117 118 119 |
# File 'lib/jekyll-l10n/utils/translation_resolver.rb', line 113 def self.contains_protected_elements?(node) return false unless node.element? # Block block-level translation for script and style (security/functionality). protected_elements = %w[script style] node.children.any? { |child| child.element? && protected_elements.include?(child.name) } end |
.content_element?(node) ⇒ Boolean
73 74 75 76 77 78 |
# File 'lib/jekyll-l10n/utils/translation_resolver.rb', line 73 def self.content_element?(node) return false unless node return false unless node.element? HtmlElements::CONTENT_ELEMENTS.include?(node.name) end |
.resolve(node, text, translations) ⇒ String?
Resolve a translation for a text node.
Attempts direct lookup of the normalized text in translations hash. If not found, checks if the text is part of a block element with a block-level translation and returns that.
34 35 36 37 38 39 40 41 |
# File 'lib/jekyll-l10n/utils/translation_resolver.rb', line 34 def self.resolve(node, text, translations) return nil unless node && text && translations direct_translation = translations[text] return direct_translation if direct_translation try_block_level_translation(node, text, translations) end |
.try_block_level_translation(node, text, translations) ⇒ String?
Attempt block-level translation for text nodes that are part of larger blocks.
Checks if a text node is part of a larger block element (like a paragraph) that has a complete translation. Only returns block translation if the text node alone doesn’t have a direct translation but the entire block does.
Security consideration: Returns nil if the block contains protected elements (script, style) to prevent unsafe translation application.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jekyll-l10n/utils/translation_resolver.rb', line 61 def self.try_block_level_translation(node, text, translations) ancestor = find_content_element_ancestor(node) return nil unless ancestor return nil if contains_protected_elements?(ancestor) block_text = BlockTextExtractor.extract(ancestor) return nil unless block_text && block_text != text translations[block_text] end |