Class: Jekyll::L10n::LocalizedPage

Inherits:
Page
  • Object
show all
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.

Examples:

Creating a localized page variant

page = LocalizedPage.new(site: site, base: site.source, dir: '/es',
                          page: source_page, locale: 'es')
# Resulting URL: /es/path/to/page/
# Resulting locale: es

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, base:, dir:, page:, locale:) ⇒ LocalizedPage

Note:

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.

Parameters:

  • site (Jekyll::Site)

    (keyword) The Jekyll site object

  • base (String)

    (keyword) The base directory (typically site.source)

  • dir (String)

    (keyword) The directory path (will be prefixed with locale, e.g., ‘/es/path’)

  • page (Jekyll::Page)

    (keyword) The source page to localize

  • locale (String)

    (keyword) The BCP 47 locale code (e.g., ‘es’, ‘pt_BR’, ‘zh_CN’)



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

#langString

Alias for locale, used in Liquid templates

Returns:

  • (String)


40
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 40

attr_accessor :locale, :lang, :original_page, :original_url

#localeString

The BCP 47 locale code for this page variant (e.g., ‘es’, ‘pt_BR’)

Returns:

  • (String)


40
41
42
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 40

def locale
  @locale
end

#original_pageJekyll::Page

Reference to the source page before localization

Returns:

  • (Jekyll::Page)


40
# File 'lib/jekyll-l10n/jekyll/localized_page.rb', line 40

attr_accessor :locale, :lang, :original_page, :original_url

#original_urlString

The URL of the source page before locale prefix was added

Returns:

  • (String)


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’.

Parameters:

  • dest (String, nil) (defaults to: nil)

    The destination directory (defaults to nil, which uses site config destination)

Returns:

  • (String)

    The full file system path for the output file



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_liquidHash<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.

Returns:

  • (Hash<String, Object>)

    Hash of page data for Liquid templates, including locale



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

#urlString

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.

Returns:

  • (String)

    The locale-prefixed URL with optional trailing slash (e.g., ‘/es/path/to/page/’ or ‘/es/assets/style.css’)



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_placeholdersHash<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.

Returns:

  • (Hash<Symbol, String>)

    Hash of placeholders with locale-prefixed path



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