Class: Jekyll::L10n::LocalizedPage
- Inherits:
-
Page
- Object
- Page
- Jekyll::L10n::LocalizedPage
- Defined in:
- lib/jekyll-l10n/jekyll/localized_page.rb
Overview
Localized Page - Represents a Jekyll page in a specific locale
LocalizedPage extends Jekyll::Page to represent a page content variant for a specific language. It inherits content and metadata from the source page while maintaining locale-specific URLs and data attributes. The locale is included as a URL prefix (e.g., /es/path/to/page/).
Key responsibilities:
-
Maintain both original and localized URLs and paths
-
Inherit content and front matter from source page
-
Set locale and lang attributes for Liquid templates
-
Generate locale-prefixed output paths
During the post-render phase, LocalizedPage content is translated using translations from PO files. The translation process happens in the post_render Jekyll hook.
Instance Attribute Summary collapse
-
#lang ⇒ String
Alias for locale, used in Liquid templates.
-
#locale ⇒ String
The BCP 47 locale code for this page variant (e.g., ‘es’, ‘pt_BR’).
-
#original_page ⇒ Jekyll::Page
Reference to the source page before localization.
-
#original_url ⇒ String
The URL of the source page before locale prefix was added.
Instance Method Summary collapse
-
#destination(dest = nil) ⇒ String
Get the destination file path for this localized page.
-
#initialize(site:, base:, dir:, page:, locale:) ⇒ LocalizedPage
constructor
Initialize a new localized page variant.
-
#to_liquid ⇒ Hash<String, Object>
Convert this localized page to Liquid template data.
-
#url ⇒ String
Get the full URL for this localized page.
-
#url_placeholders ⇒ Hash<Symbol, String>
Get the URL placeholder substitutions for this localized page.
Constructor Details
#initialize(site:, base:, dir:, page:, locale:) ⇒ LocalizedPage
All parameters are keyword arguments and must be passed by name
Initialize a new localized page variant
Creates a page instance that represents the given page in a specific locale. Copies all essential attributes from the source page and sets up locale-specific metadata and URLs.
Note: This method copies the @output attribute from the source page. In production, by Jekyll during the Render phase. This initialization ensures LocalizedPage has the same @output as its source page, which may be useful in test scenarios or if LocalizedPage is created after rendering.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 62 def initialize(site:, base:, dir:, page:, locale:) @site = site @base = base @dir = dir @name = page.name @ext = page.ext @output_ext = page.output_ext @locale = locale @lang = locale @original_url = page.url @original_page = page @path = page.path @relative_path = page.relative_path @content = page.content @output = page.output setup_localized_data(page, locale) end |
Instance Attribute Details
#lang ⇒ String
Alias for locale, used in Liquid templates
40 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 40 attr_accessor :locale, :lang, :original_page, :original_url |
#locale ⇒ String
The BCP 47 locale code for this page variant (e.g., ‘es’, ‘pt_BR’)
40 41 42 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 40 def locale @locale end |
#original_page ⇒ Jekyll::Page
Reference to the source page before localization
40 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 40 attr_accessor :locale, :lang, :original_page, :original_url |
#original_url ⇒ String
The URL of the source page before locale prefix was added
40 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 40 attr_accessor :locale, :lang, :original_page, :original_url |
Instance Method Details
#destination(dest = nil) ⇒ String
Get the destination file path for this localized page
Computes the full file system path where this page’s content will be written. For HTML pages with URL ‘/es/path/to/page/’, this returns ‘dest/es/path/to/page/index.html’. For assets with URL ‘/es/assets/style.css’, this returns ‘dest/es/assets/style.css’.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 118 def destination(dest = nil) dest ||= @site.config['destination'] url_path = url # Only use index.html for HTML pages if @output_ext == '.html' url_path += '/' unless url_path.end_with?('/') "#{dest}#{url_path}index.html" else # For assets, preserve the file extension "#{dest}#{url_path}" end end |
#to_liquid ⇒ Hash<String, Object>
Convert this localized page to Liquid template data
Overrides Jekyll::Page’s to_liquid to include locale and lang in template context. This makes the page’s locale available to Liquid templates for conditional rendering and URL generation.
139 140 141 142 143 144 145 146 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 139 def to_liquid computed_url = url result = super result['url'] = computed_url result['locale'] = @locale result['lang'] = @lang result end |
#url ⇒ String
Get the full URL for this localized page
Returns the page’s URL with a locale prefix. For example, if the source page URL is ‘/path/to/page/’, this returns ‘/es/path/to/page/’ for locale ‘es’. For HTML pages, URLs end with a trailing slash. For assets, the file extension is preserved.
102 103 104 105 106 107 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 102 def url url = "/#{@locale}#{@original_url}" # Only add trailing slash for directory URLs (no file extension) url += '/' unless url.end_with?('/') || url =~ /\.[a-zA-Z0-9]+$/ url end |
#url_placeholders ⇒ Hash<Symbol, String>
Get the URL placeholder substitutions for this localized page
Overrides Jekyll::Page’s url_placeholders to include the locale prefix in path generation. This ensures that Jekyll’s permalink logic respects the locale prefix.
88 89 90 91 92 |
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 88 def url_placeholders placeholders = super placeholders[:path] = "/#{@locale}#{placeholders[:path]}" if placeholders[:path] placeholders end |