Skip to content

feat: support i18n_subsites and fix sitemap format - #48

Open
diegofd wants to merge 1 commit into
pelican-plugins:mainfrom
diegofd:i18n-support
Open

feat: support i18n_subsites and fix sitemap format#48
diegofd wants to merge 1 commit into
pelican-plugins:mainfrom
diegofd:i18n-support

Conversation

@diegofd

@diegofd diegofd commented Jan 2, 2026

Copy link
Copy Markdown

Add i18n_subsites support for proper hreflang generation

Problem

The sitemap plugin doesn't properly support the i18n_subsites Pelican plugin. When using i18n_subsites, each language runs as a separate Pelican instance with its own SITEURL (e.g., https://example.com for the main language and https://example.com/en for subsites). This caused several issues:

  1. Missing hreflang links: Translations weren't linked because i18n_subsites doesn't populate obj.translations - each language version is processed independently
  2. Incorrect URLs: The sitemap used relative URLs that didn't account for the different SITEURL per language
  3. Malformed paths: Translation URLs from Pelican's native mechanism contained ../ segments that weren't normalized

Solution

This PR adds proper i18n_subsites support by:

  1. Tracking language context per page: Store SITEURL and DEFAULT_LANG from each page's context during queue_page(), preserving the language-specific settings from each i18n_subsites run

  2. Slug-based translation grouping: Group articles/pages by their slug across all languages. Since the same content in different languages shares the same slug, this correctly links translations even when i18n_subsites processes them separately

  3. Full URL generation: Build absolute URLs using each page's own SITEURL, with proper normalization of ../ path segments using posixpath.normpath

  4. Dual translation mechanism: Support both:

    • Slug-based grouping (for i18n_subsites)
    • Pelican's native obj.translations (for standard multi-language setups)

Changes

  • Updated XML_URL and XML_TRANSLATION templates to use full URLs
  • Added get_full_url() helper with URL path normalization
  • Added add_to_url_map() to build URL map with slug tracking
  • Added link_translations() to merge translations from both mechanisms
  • Added format_hreflang() to generate hreflang XML links
  • Track current_lang and current_siteurl per queued page

Example output

Before (missing hreflang):

<url>
<loc>https://example.com/en/about/</loc>
</url>

After (proper bidirectional hreflang):

<url>
<loc>https://example.com/en/about/</loc>
<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/about/"/>
<xhtml:link rel="alternate" hreflang="es" href="https://example.com/about/"/>
</url>

@diegofd
diegofd marked this pull request as ready for review January 2, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant